protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
375 stars 67 forks source link

Add GenerateClassFromObject to Javascript gen #6

Closed psigen closed 2 years ago

psigen commented 2 years ago

Adds the fromObject helper to the public API of generated pb classes.

This closes a 5-year-old open issue: protocolbuffers/protobuf-javascript#96 by rebasing this 1-year-old open PR: https://github.com/protocolbuffers/protobuf/pull/8488 based on this 4-year-old workaround: protocolbuffers/protobuf-javascript#96

From the original issue:

The comment for jspb.Message.GENERATE_FROM_OBJECT states:

 *     NOTE: By default no protos actually have a fromObject method. You need to
 *     add the jspb.generate_from_object options to the proto definition to
 *     activate the feature.

However, no such option is currently implemented in the open sourced version. I see the code which generates output for this option (GenerateClassFromObject) but it is invoked 0 times.

~/protobuf$ grep -r GenerateClassFromObject .
./src/google/protobuf/compiler/js/js_generator.cc:void Generator::GenerateClassFromObject(const GeneratorOptions& options,
./src/google/protobuf/compiler/js/js_generator.h:  void GenerateClassFromObject(const GeneratorOptions& options,
iampava commented 2 years ago

Any updates on this?

psigen commented 2 years ago

Well, in doing some testing, I am not sure if this PR is actually going to work.

The issue is that the internal fromObject follows its own rules and doesn't seem to correspond to a straightforward JS object, instead it is structuring things in its own way. The resulting input object isn't necessarily intuitive to use.

For the record, I have given up on this implementation and have been using protoc-gen-ts with much more success. It generates intuitive TypeScript objects and typing with minimal fuss and has to/fromObject conversions that so far seem quite effective and straightforward.

https://github.com/thesayyn/protoc-gen-ts