-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation on defining an encoder in terms of its mapper? #22
Comments
you can do it with the following code (dint test it, might have small bugs): (defmapper mapper [ObjectReference]
:encoders {ObjectReference
{:from-proto (fn [^ObjectReference proto-obj]
(let [obj-type (.getObjectType proto-obj)]
(cond-> {:object-type obj-type}
(= obj-type "some condition") (assoc :object-id (.getObjectId proto-obj)))))
:to-proto (fn [clj-map]
(let [b (ObjectReference/newBuilder)
obj-type (:object-type clj-map)]
(.setObjectType b obj-type)
(when (= obj-type "some condition")
(.setObjectId b (:object-id clj-map)))
(.build b)))}}) |
I didn't manage to get a mapper with a custom encoder for a toplevel protobuf type, like in the comment above ( |
I confirm for top level proto the encoders doesn't have an effect |
Will do, thanks |
Hi all!
I've got an issue where I've got some proto that looks like this:
I want to define a decoder on this object that moves it to a map so that I can do decoding on the
object_id
if theobject_type
matches a condition. The docs show how you might define an encoder/decoder pair where the non-proto side is a scalar value, but it isn't clear to me what to do if it's a map.Do I define a separate mapper so that I can return a proto-map within the encoder? Does it work to define a decoder in terms of the mapper it's being defined on? Is there another better approach that I'm not thinking of? I'd be happy to contribute the answer to the readme.
EDIT:
I suppose one answer is that this is an inappropriate place to do this sort of transformation, and it'd be better to do it on the clojure constructs that we're moving into proto before it hits the mapper.
The text was updated successfully, but these errors were encountered: