Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "from_object" parameter that adds fromObject() signatures #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nicksieger
Copy link

  • Plugin parameter can be comma-separated
  • If parameter list contains "from_object", the generator adds a static fromObject()
    method to each message.

Currently, fromObject is only added by protoc-gen-js if you custom-build it (see
protocolbuffers/protobuf-javascript#162 for an example patch).

- Plugin parameter can be comma-separated
- If parameter list contains "from_object", the generator adds a static fromObject()
  method to each message.

Currently, fromObject is only added by protoc-gen-js if you custom-build it (see
protocolbuffers/protobuf-javascript#162 for an example patch).
@agreatfool
Copy link
Owner

Thanks a lot for your pr, let me have a look :)

@agreatfool
Copy link
Owner

agreatfool commented Nov 30, 2023

Hi @nicksieger , I've made some investigation. Just like what you said, seems the release version has no such feature for now.

I tested the latest version with

$ protoc --version
libprotoc 3.20.3

$ grpc_tools_node_protoc --version
libprotoc 3.19.1

$ protoc \
--js_out=import_style=commonjs,binary,from_object:./src/grpcjs/proto \
--grpc_out=grpc_js:./src/grpcjs/proto \
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` \
-I ./proto \
proto/*.proto

Seems no fromObject methods generated in the js files.

I'd like to test and apply your pr when it's officially released.
Or could you please tell me how to build the protoc-gen-js you mentioned manually? I'm not familiar with this.

@nicksieger
Copy link
Author

Sure, here's a Dockerfile snippet, or you can run these commands manually on Debian or Ubuntu.

FROM node:18-bookworm
ENV DEBIAN_FRONTEND=noninteractive
WORKDIR /root
RUN apt update && apt install -y bazel-bootstrap protobuf-compiler && rm -rf /var/lib/apt/lists/*
RUN curl -sL https://github.com/protocolbuffers/protobuf-javascript/archive/refs/tags/v3.21.2.tar.gz | tar zxf -
# Apply patch to enable 'fromClass' generation from https://github.com/protocolbuffers/protobuf-javascript/pull/162
RUN cd protobuf-javascript-3.21.2 && \
  curl -sL https://github.com/protocolbuffers/protobuf-javascript/commit/a3c4522bbdab5784de0361049437242980df07d0.diff | patch -u -p1 && \
  bazel build :plugin_files && cp bazel-bin/generator/protoc-gen-js /usr/local/bin

@nicksieger
Copy link
Author

FYI, I'm using regular protoc instead of grpc_tools_node_protoc, the latter seems to bundle its own version of proto-gen-js.

protoc \
    --plugin=protoc-gen-js="$(which protoc-gen-js)" \
    --js_out=import_style=commonjs,binary:./protos \
    --plugin=protoc-gen-grpc="$(which grpc_tools_node_protoc_plugin)" \
    --grpc_out=grpc_js:./protos \
    --plugin=protoc-gen-ts="$(which protoc-gen-ts)" \
    --ts_out=grpc_js,from_object:./protos \
    my-service.proto

@agreatfool
Copy link
Owner

FYI, I'm using regular protoc instead of grpc_tools_node_protoc, the latter seems to bundle its own version of proto-gen-js.

protoc \
    --plugin=protoc-gen-js="$(which protoc-gen-js)" \
    --js_out=import_style=commonjs,binary:./protos \
    --plugin=protoc-gen-grpc="$(which grpc_tools_node_protoc_plugin)" \
    --grpc_out=grpc_js:./protos \
    --plugin=protoc-gen-ts="$(which protoc-gen-ts)" \
    --ts_out=grpc_js,from_object:./protos \
    my-service.proto

I have 2 questions:

  • I saw in the example, you are using --js_out=import_style=commonjs,binary:./protos, seems there is no from_object option enabled? Will there any fromObject function generated in js codes?
  • What's the way you installed the protoc-gen-js? Do you installed it by npm package? I saw one package could help to install this command globally

@nicksieger
Copy link
Author

  1. There is no option to enable fromObject in protoc-gen-js, the only way is to build a patched version from source. I added it as an opt-in option here in protoc-gen-ts since the fromObject methods won't be generated without the patched build.
  2. The last RUN command is the one that builds protoc-gen-js and copies it to /usr/local/bin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants