Documentation on defining an encoder in terms of its mapper?
Hi all!
I've got an issue where I've got some proto that looks like this:
message ObjectReference {
string object_type = 1;
string object_id = 2;
}
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 the object_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.
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 (ObjectReference is a mapped class and has a custom encoder)
I confirm for top level proto the encoders doesn't have an effect I suggest you will open another issue as in general it does work for an inner message
Will do, thanks