Skip to content
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

Open
reify-tanner-stirrat opened this issue Jul 18, 2023 · 4 comments
Open

Comments

@reify-tanner-stirrat
Copy link

reify-tanner-stirrat commented Jul 18, 2023

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.

@sol-sh
Copy link
Contributor

sol-sh commented Oct 18, 2023

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)))}})

@mping-exo
Copy link

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)

@sol-sh
Copy link
Contributor

sol-sh commented Apr 4, 2024

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

@mping-exo
Copy link

Will do, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants