ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

Create message from already encoded nested children

Open Vincz opened this issue 3 years ago • 1 comments

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?

Vincz avatar Feb 20 '22 08:02 Vincz

Ah interesting, no, we don't support that.

I could see supporting by something like:

  • Have Error.encode return a tagged Buffer & { encodes: 'Error'}
  • Have the Message.encode type take a DeepPartial-esque type that allows either Error | (Buffer & { encodes: 'Error' })`
  • Have the Message.encode code do an instanceof to 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.

stephenh avatar Feb 20 '22 14:02 stephenh