Closed telackey closed 4 years ago
This new codec-protobuf force you to have a root type for the encode/decode and type_url is only used for inner objects of type Any
.
Maybe if I know better what is the context to doing this we can find a workaround.
The first problem is just that it is unexpected, since an object should decode to the same thing it was before encoding.
In practical usage, it can show up a lot of ways, but the simplest is if I want to use a message I received in another message.
message A {
a = 1;
...
}
message B {
b = 1;
Any payload = 2;
...
}
const a = { __type_url: 'my.A', a: 'a' }
send(codec.encode(a));
const incoming = codec.decode(a);
const b = {
b: 'b',
payload: incoming
};
send(codec.encode(b)); //ERROR missing __type_url.
In practice, this is problem for any of the 'Envelope' stuff. If you just want to see it break quickly, go here: https://github.com/wirelineio/incubator/blob/master/packages/credentials/src/party/party.test.js#L104
And add a codecLoop
around pseudo.
I understand. Yes, I think the problem here is related with having a rootTypeUrl
is limiting us to use the codec-protobuf as normal encoder/decoder. Encode/decode any type of our schema.
I will prefer to always encode/decode using the __type_url as in your example and also returning it with the result.
And another thing, since we are in JavaScript I recomend to change __type_url
to something like __typeurl
like in apollo with the __typename
@richburdon
It should be possible to "loop" a valid object through the codec, and the output be identical to the input. That is:
But in practice, the top __type_url is stripped and after looks like: