Feature Request: make protocol buffer AST/parser SDK

791 views
Skip to first unread message

Gerardo Mora

unread,
Feb 16, 2021, 7:12:15 PM2/16/21
to Protocol Buffers
Hello everyone, I would like to create an application that analyzes a .proto file and dynamically creates GraphQL queries, I don't want to parse the .proto files because it will outdated and probably buggy at some point, but I was reading the source code and the only available thing is create a plugin that ouputs a file after the processing which is not idea.

Is there an API/SDK available for it?  

Marc Gravell

unread,
Feb 16, 2021, 7:24:14 PM2/16/21
to Gerardo Mora, Protocol Buffers
You can use protoc to output the compiled schema (one of the command-line-options - something "file descriptor set"), and deserialize the resultant binary file as a FileDescriptorSet instance, deserializing via your choice of language via descriptor.proto - any use?

As an aside, I also have a separate implementation of a .proto parser, implemented in C#/.NET (so it can be used in 100% managed .NET code). Not sure if that is any use to you, but if so: let me know.

On Wed, 17 Feb 2021, 00:12 Gerardo Mora, <gmoras...@gmail.com> wrote:
Hello everyone, I would like to create an application that analyzes a .proto file and dynamically creates GraphQL queries, I don't want to parse the .proto files because it will outdated and probably buggy at some point, but I was reading the source code and the only available thing is create a plugin that ouputs a file after the processing which is not idea.

Is there an API/SDK available for it?  

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/f78681b8-47f2-44ac-955a-73b828f1b08dn%40googlegroups.com.

Derek Perez

unread,
Feb 16, 2021, 7:28:58 PM2/16/21
to Marc Gravell, Gerardo Mora, Protocol Buffers
It sounds like you may wanna build a protoc plugin like so:

The AST for protobuf is actually in protobuf itself! It's very easy to work with, and is the backbone of all the code generators.

Gerardo Mora

unread,
Feb 16, 2021, 7:34:28 PM2/16/21
to Derek Perez, Marc Gravell, Protocol Buffers
I'm gonna use golang for my project, and will be open source, so eventually I will show you the software if you guys are interested!
I would like that the parser will run in the same thread of the request, when a microservice registers itself.

I'm gonna read all approaches and see, thanks guys!

PS 
if there are more ideas all are welcomed.

--
May the source be with you!!!

Josh Humphries

unread,
Feb 16, 2021, 9:27:50 PM2/16/21
to Gerardo Mora, Derek Perez, Marc Gravell, Protocol Buffers
If you are using Go, you can use this protoparse package to parse proto source:
https://pkg.go.dev/github.com/jhump/protoreflect/desc/protoparse

It provides functions for parsing and linking into descriptors. But it also provides an option to parse into a richer AST. The descriptor format is not a particularly good AST because it is lossy. So if you wanted to write a formatter, for example, the descriptor loses a lot of token position and comment information, which may prevent you from perfectly reconstructing the original source text. The ast sub-package of protoparse is not lossy and includes position information, including whitespace and comments, for everything lexed token.
https://pkg.go.dev/github.com/jhump/protoreflect/desc/protoparse/ast

----
Josh Humphries
jh...@bluegosling.com


Gerardo Mora

unread,
Feb 17, 2021, 11:18:59 AM2/17/21
to Protocol Buffers
excellent option, I will use it!

Lim Afriyadi

unread,
Jul 11, 2022, 11:39:54 PM7/11/22
to Protocol Buffers
I've created a PoC to automate GraphQL API creation (code first approach) from protobuf using protobuf extension to define graphql query / mutation name. I'm using protogen package to iterate protobuf file definition. Maybe you would like to take a look https://github.com/ncrypthic/graphql-grpc-edge.
Reply all
Reply to author
Forward
0 new messages