Protobuf: Support optional data object for JavaScript generated Message constructor

Created on 26 Feb 2019  Â·  4Comments  Â·  Source: protocolbuffers/protobuf

What language does this apply to?
Generated JavaScript

Describe the problem you are trying to solve.
The current way of creating a new message in JavaScript is a bit unnatural and awkward, since setters and getters is not a common pattern in JavaScript. This makes spreading adoption for things like grpc-web in web communities harder.

Example of current state:

// set value using setter
const request = new EchoRequest();
request.setMessage('hello');

// set value using data array
const request = new EchoRequest(['hello']);

The former is unnatural for JavaScript and the latter is not very useful as you'd need to look up the order of fields in the proto schema.

Describe the solution you'd like

My suggestion is to support an object being passed to the constructor, like:

const request = new EchoRequest({ message: 'hello' });

This is both natural and useful for JS devs. And it's similar to existing ways of sending JSON encoded data using e.g. fetch or axios:

axios.post('https://site.com/', {
  message: 'hello'
})

fetch('https://site.com/', {
  method: 'POST',
  body: JSON.stringify({ message: 'hello' })
});

Describe alternatives you've considered

I haven't been able to come up with any alternatives.

Additional context

My understanding from looking at the Message type in protobuf is that it concerns itself not really with what fields are named but rather how they are numbered (ordered?). So I'm assuming that the change would be somewhere high up in the generated JS code. For example:

/**
 * Generated by JsPbCodeGenerator.
 * @param {Object=} opt_data Optional initial data object, 
 * <.....>
 */
proto.some.service.EchoRequest = function(opt_data_obj) {
  const opt_data = optDataObjToArray(opt_data_obj);
  jspb.Message.initialize(this, opt_data, 0, -1, null, null);
};

What do you think?

I tried to search if this had been discussed before but couldn't find anything so apologies if it's been asked before.

Cheers!

javascript

All 4 comments

This forces us to eagerly verify schema which would be problematic for code size.

@TeBoring could you elaborate on that? Cheers

The approach you mentioned will need field name, which will hurt code size.
On Tue, Mar 12, 2019 at 12:15 Jonatan Dahl notifications@github.com wrote:

@TeBoring https://github.com/TeBoring could you elaborate on that?
Cheers

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/protocolbuffers/protobuf/issues/5783#issuecomment-472143109,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AE9H5VJT2kER0dqgyhXk_Cx8QbktK_G1ks5vV_zLgaJpZM4bSi2Y
.

I understand! How much difference do you think it would be? If you had to make a guess. And also, how important do you think code size is for JS clients?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tang3w picture tang3w  Â·  39Comments

blowmage picture blowmage  Â·  26Comments

blowmage picture blowmage  Â·  73Comments

tbillington picture tbillington  Â·  29Comments

laszloagardi picture laszloagardi  Â·  40Comments