ts-proto
ts-proto copied to clipboard
Create message from already encoded nested children
Hi guys. I have the following schema and I would like to be able to create a Message from already encoded Error objects.
syntax = "proto3";
message Error {
string message = 1;
int32 code = 2;
}
message Message {
repeated Error errors = 1;
}
The typescript code would looks like it:
const errors = [{message: 'Error 1', code: 1}, {message: 'Error 2', code: 2}];
const errorsEncoded = errors.map(e => Error.encode(e));
.... here I store the encoded version of the errors....
const message = Message.encode({errors: errorsEncoded});
So as you can see, the errors have already been encoded when creating the Message and I don't want to run the encoding process again to create my encoded Message.
Is it possible to do that?
Ah interesting, no, we don't support that.
I could see supporting by something like:
- Have
Error.encodereturn a taggedBuffer & { encodes: 'Error'} - Have the
Message.encodetype take aDeepPartial-esque type that allows eitherError | (Buffer & { encodes: 'Error' })` - Have the
Message.encodecode do aninstanceofto detect params that are already buffered
This would not be the easiest PR / feature to implement, but it's doable if you really want/need it.