Parse .proto files

202 views
Skip to first unread message

Ran Volkovich

unread,
Oct 4, 2023, 5:02:49 AM10/4/23
to Protocol Buffers
Hi, 
There is a way to transform .proto file like 
syntax = "proto3";

package helloworld;

// My cool new message.
message MyMessage {
string some_string = 1;
repeated int64 some_numbers = 2;
}
to FileDescriptorProto class from com.google.protobuf jar (or any other proto lib that can represent the proto schema)
without to use protoc to generate descriptor before?

Marc Gravell

unread,
Oct 4, 2023, 6:42:06 AM10/4/23
to Ran Volkovich, Protocol Buffers
If .NET is an option, protobuf-net has this (in the protobuf-net.Reflection package):

var schema = """

    syntax = "proto3";

    package helloworld;

    // My cool new message
    message MyMessage {
      string some_string = 1;
      repeated int64 some_numbers = 2;
    }
    """;
var set = new FileDescriptorSet();
set.Add("some.proto", source: new StringReader(schema));
set.Process();

foreach (var file in set.Files)
{
    Console.WriteLine($"in {file.Name}");
    foreach (var msg in file.MessageTypes)
    {
        Console.WriteLine(msg.Name);
        foreach (var field in msg.Fields)
        {
            Console.WriteLine($"> {field.Name}");
        }
    }
    Console.WriteLine();
}


This is entirely managed code (i.e. no "shell to protoc"). The API is basically identical to the Google one - the idea is that the type system is binary compatible (i.e. you can serialize it as protobuf and you'd get the same thing that protoc would give, in binary mode)

--
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/ad559f9f-8112-4bbe-b0b8-43f4b64d6d60n%40googlegroups.com.


--
Regards,

Marc

Ran Volkovich

unread,
Oct 4, 2023, 7:04:56 AM10/4/23
to Protocol Buffers
Hi, 
thanks for the the answer.
this is great if i wont find any scala/java lib that does this i might use it thanks 

Gary Peck

unread,
Oct 4, 2023, 10:01:54 AM10/4/23
to Protocol Buffers
It's not Java/Scala, but another option is buf's proto parser written in Go: https://github.com/bufbuild/protocompile.

Gary

Ran Volkovich

unread,
Oct 4, 2023, 10:30:37 AM10/4/23
to Protocol Buffers
thanks ill take a look also on that 
Reply all
Reply to author
Forward
0 new messages