protobufjs / protobuf.js

Protocol Buffers for JavaScript & TypeScript.
Other
9.85k stars 1.41k forks source link

Does protobufjs allow generating .proto files as output? #1502

Open dorgalam opened 3 years ago

dorgalam commented 3 years ago

I'm trying to create .proto files from some dataset, it looks like my best option is to transform that dataset into a JSON object that is the same as the resulting JSON as this library's toJson method, so that I could use the fromJson method to create protobujs' classes.

Now, a blocker for using this method is the fact that there is no 'toProto' method (name and signature TBD) that can actually take your classes, and transform them into .proto files on my filesystem.

Did I miss such a method? Does it exist? If it doesn't exist, do you think that it is something you want in your API? If it really doesn't exist in your library, then I will probably create such a mechanism myself, and would love to contribute such functionality.

Thanks!

alexander-fenster commented 3 years ago

Hi @dorgalam,

If you want a code that would generate *.proto for you, I believe there is no such functionality (neither here in protobuf.js, nor in the official protobuf implementation). Can you maybe tell more about why you might need to generate proto IDLs? Normally, the IDLs are static and everything else is generated based of them, but I understand there may be different use cases.

dorgalam commented 3 years ago

Hi @alexander-fenster! I'm working on an api that allows building idls with ui and such, so both creating protos, is necessary. The reason I'm not using the descriptors to create classes directly, is to make use of other pipelines that actually use protos, not their products.

I understand that this isn't in the official api, do you think it will be relevant to add such a capability to protobufjs?

alexander-fenster commented 3 years ago

I'm not an owner of this library - @dcodeIO could decide if it fits here.

Note that generating proto IDLs in general (not just for your use case) may actually be quite hard since they should include imports (the result of IDL generation will likely consist of multiple files - at least one file per package), options (file-level, service-level, message-level, and field-level options; the types of options is not stored in JSON), syntax line (proto2 or proto3), and maybe more weird things that didn't come to my mind right now.

So my personal opinion here is that it might make sense to have this as a separate tool that accepts JSON in this library's format and produces proto IDLs.

dorgalam commented 3 years ago

@alexander-fenster I ran the parse method on a couple of different inputs, and it appears like it does contain syntax, imports, service/method/message level options (what we use). Hopefully this means it won't prove as difficult 🤞

I'll start working on it anyway, and we'll wait for @dcodeIO's opinion on whether or not this should be in this lib.

Appreciate your help 🙏

dcodeIO commented 3 years ago

Looks like a fine extension, similar to the descriptor extension. This way around, one can install it where necessary only without affecting code size for other users.

dorgalam commented 3 years ago

Great. When I have something at hand, I'll PR it here. Thanks!

JoshuaWise commented 2 years ago

Any updates on this functionality? I have a use-case where I need to parse a proto file, rewrite the import statements, and then de-compile the modified AST into a new proto file. Using naive string replacement seems unreasonable, due to the possibility of escape sequences, and the fact that adjacent string literals are concatenated during parsing.