Skip to content

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

Open
@travikk

Description

@travikk

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions