I’m working on an addin in C# to generate .proto files based on a UML model in Enterprise Architect.
I’m looking for a way to generate my own .proto files.
I found the FileDescriptorProto
from Google.Protobuf.Reflection
and I was able to create my own message definition.
// Define message structure
var fileDescriptor = new FileDescriptorProto();
fileDescriptor.Name = "test.proto";
fileDescriptor.Syntax = "proto3";
var message = new DescriptorProto();
message.Name = "TestMessage";
var field1 = new FieldDescriptorProto();
field1.Name = "id";
field1.Type = FieldDescriptorProto.Types.Type.Int32;
//add field to message
message.Field.Add(field1);
//add message to file
fileDescriptor.MessageType.Add(message);
Now unfortunately I don’t seem to find an easy way to write this information to a .proto file.
I tried :
fileDescriptor.WriteTo(new FileStream(filePath, FileMode.OpenOrCreate));
But that ended up with a weird
test.proto"
TestMessage
id(bproto3
Converted into hex it looks like this:
0A0A746573742E70726F746F22150A0B
546573744D65737361676512060A0269
642805620670726F746F33
ToString()
also didn’t do what I needed as it printed:
{
"name": "test.proto",
"messageType": [ {
"name": "TestMessage",
"field": [ {
"name": "id",
"type": "TYPE_INT32"
} ]
} ],
"syntax": "proto3"
}
What I’m expecting is something like
syntax = "proto3";
message TestMessage{
int32 id = 1;
}
1