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

tricky on handle to encode/decode on Any #39

Open
netpyoung opened this issue Jan 9, 2019 · 0 comments
Open

tricky on handle to encode/decode on Any #39

netpyoung opened this issue Jan 9, 2019 · 0 comments

Comments

@netpyoung
Copy link

I want to put some message to Any.
but the problem occurs.

let me assume. we have that kind of message.

message A {
   Any any;
}

messsage B {
   int b;
}

So I can writing small encode/decode part for B

(let [message-b {:b 10}
      encoded (->> (protobuf/create B message-b)
                   (protobuf/->bytes)
                   (assoc {} :value)
                   (protobuf/create Any))
      decoded (->> encoded
                   :value
                   (protobuf/bytes-> (protobuf/create B))
                   (into {}))]
  (= message-b decoded))

but When I combine A with B . there is some tricky problem.

(let [message-b {:b 10}
      encoded (->> (protobuf/create B message-b)
                   (protobuf/->bytes)
                   (assoc {} :value)
                   (protobuf/create Any)
                   (assoc {} :any)
                   (protobuf/create A)
                   (protobuf/->bytes))

      decoded (->> encoded
                   (protobuf/bytes-> (protobuf/create A))
                   :any
                   (protobuf/create A)
                   :value
                   ;; at this point. value's type is bytestring so It need to call `.toByteArray`
                   (.toByteArray)       ; <<< a little tricky
                   (protobuf/bytes-> (protobuf/create B))
                   (into {}))]
  (= message-b decoded))

So I need to fix encode/decode part like that.

(let [message-b {:b 10}
      encoded (->> (protobuf/create B message-b)
                   (protobuf/->bytes)
                   ;; I inserted toByteArray on decoder. but it's type is `byte[]`
                   (com.google.protobuf.ByteString/copyFrom) ;; So It need to convert to bytestring.
                   (assoc {} :value)
                   (protobuf/create Any))
      decoded (->> encoded
                   :value
                   (.toByteArray) ;; so I inserted.
                   (protobuf/bytes-> (protobuf/create B))
                   (into {}))]
  (= message-b decoded))

It's works.
but com.google.protobuf.ByteString/copyFrom on this code likes tricky skill.
Is any other good suggestion of this? or Is anything what i missed?

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

1 participant