Open barkanido opened 5 years ago
@barkanido I had faced similar issue when the protobuf message was made of nested proto messages and if the nested message was being enriched with data and assoc-in
was not yielding desired result.
To overcome this problem, I came up with following Clojure protocol which converts protobuf object to map:
(defn as-map [f o]
(reduce (fn [m [k v]]
(assoc m k (f v)))
{} o))
(defprotocol ProtoBufToClojureMap
(->clj-map [o]))
(extend-protocol ProtoBufToClojureMap
protobuf.impl.flatland.core.FlatlandProtoBuf
(->clj-map [o]
(as-map ->clj-map o))
protobuf.PersistentProtocolBufferMap
(->clj-map [o]
(as-map ->clj-map o))
java.lang.Object
(->clj-map [o] o))
Above protocol converts the protobuf map into a Clojure map, allowing you to do all sort of map operations
Usage is pretty straight forward as well:
(def alice-as-clj-map (->clj-map alice))
You can covert it back to protobuf object for publishing by using the standard library functions.
@oubiwann Please let me know if this seems fit enough for PR. This may sit in some utility name-space.
@oubiwann I have create a PR to resolve this Issue. This PR resolvesthe issue and :
assoc-in
, update-in
,merge
and merge-with
to protobuf.impl.flatland.core.FlatlandProtoBuf
and protobuf.PersistentProtocolBufferMap
allowing users to work with constructed objects as if they were regular Clojure maps.Please review it in your time 😄
PS: As a well maintained Protobuf opertaional alternative, I would recommend using AppsFlyer/pronto
though.
Hey folks, I am evaluating this and testing some simple scenarios of mutation. What I am trying to test is:
now, given this schema:
and this test code:
tests fail with:
Can you explain why
update-in
andmerge
aren't supported and what I (probably) have done wrong? Thanks!Maybe, put differently, it looks like
PersistentProtocolBufferMap
isn't 100% compatible with a Clojure map: