protobuf-javascript icon indicating copy to clipboard operation
protobuf-javascript copied to clipboard

[JavaScript] Getters returning nulls instead of default values for primitives

Open travikk opened this issue 6 years ago • 4 comments

What language does this apply to?

JavaScript

Describe the problem you are trying to solve.

Currently, all generated getters for primitives in _pb.js files call to jspb.Message.getFieldWithDefault and there seem to be no way to tell generator to simply call jspb.Message.getField instead. This is particularly problematic when using proto2 syntax with required/optional numeric fields. If an optional field is not set, it will default to "0".

Describe the solution you'd like

Ideally there would be an option passed to the js_out parameter of protoc where we could say whether or not to use defaults.

Describe alternatives you've considered

One could potentially leverage hasMyField() to determine if the field has been set or not, but it would make the mapping layer between proto and internal app model even more verbose, as this would need to be done on all optional fields, which we have quite a few.

travikk avatar Jan 28 '20 10:01 travikk

This problem has been fixed on the plugin we are maintaining for typescript. https://github.com/thesayyn/protoc-gen-ts

thesayyn avatar Apr 21 '21 19:04 thesayyn

This problem has been fixed on the plugin we were maintaining for typescript. https://github.com/thesayyn/protoc-gen-ts

Why'd you put that in the past tense? Are you no longer maintaining it? I am just in the process of adopting protobufs for a JS project (and am already kind of regretting the decision), and your plugin is the one ray of light in the whole experience so far.

ideasculptor avatar Sep 08 '21 09:09 ideasculptor

Why'd you put that in the past tense? Are you no longer maintaining it? I am just in the process of adopting protobufs for a JS project (and am already kind of regretting the decision), and your plugin is the one ray of light in the whole experience so far.

That’s a problem when you have English as a second language. My bad. it should have been are. Sorry about that and No we are still maintaining it.

thesayyn avatar Sep 08 '21 10:09 thesayyn

Our plan is to add alternate accessors that allow you to treat absence as undefined values.

dibenede avatar Sep 09 '22 22:09 dibenede