Saving proto comments when creating C # with protobuf-net

We use protobuf-net to handle our protocol buffer needs in a C # application. Since we share our .proto files with other non-managed applications, we generate our code from .proto files (without using the first protobuf-net code approach). To DRY as soon as possible, we store a lot of interface documentation inside the .proto files themselves. We will generate C # code using protogen.exe, caused by the goal of building the project.

Now, is there a way (automatically) to pass these comments into compiled C # code?

Basically, when using .proto:

// This message is used to request a resource from the server message GetResource { // The identifier of the requested resource required string resourceId = 1; } 

... I would like something like this (IExtensible methods omitted for readability):

 /// <summary> /// This message is used to request a resource from the server /// </summary> [global::System.Serializable,global::ProtoBuf.ProtoContract( Name=@ "GetResource")] public partial class GetResource : global::ProtoBuf.IExtensible { public GetResource() {} private string _resourceId; /// <summary> /// The identifier of the requested resource /// [Required] <-- Would be nice... /// </summary> [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@ "resourceId", DataFormat = global::ProtoBuf.DataFormat.Default)] public string ResourceId { get { return _resourceId; } set { _resourceId = value; } } } 
+4
source share
2 answers

I currently believe the answer is no. As far as I know, "protoc" (the Google tool for parsing .proto files, which is used under the hood) silently rejects comments - so there is nothing readable. If a custom parser was written, then yes, it would be possible, but there is also an ambiguity of the language regarding which comments apply to which lines, for example:

 // this probably relates to resourceId required string resourceId = 1; required int foo = 2; // but... is this foo? or bar? // and what about this? // what does this relate to? and why? // and this? what are the rules? required int bar = 3; 

So, for two different reasons: not at the moment. All the proposals reviewed, though ... especially if they come with a custom parser, include :)

Please note that for this reason AFAIK, this information is missing from most (all?) Implementations. I am glad that they corrected me.

+2
source

In fact, the current version supports comments. It can be enabled using --include_source_info.

Comments are available in the descriptor. Location [n] .leading_comments and trailing_comments: https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto

I added the corresponding protobuf-net properties. Location Class:

 private string _leading_comments = ""; [global::ProtoBuf.ProtoMember(3, IsRequired = false, Name = @"leading_comments", DataFormat = global::ProtoBuf.DataFormat.Default)] [global::System.ComponentModel.DefaultValue("")] public string leading_comments { get { return _leading_comments; } set { _leading_comments = value; } } private string _trailing_comments = ""; [global::ProtoBuf.ProtoMember(4, IsRequired = false, Name = @"trailing_comments", DataFormat = global::ProtoBuf.DataFormat.Default)] [global::System.ComponentModel.DefaultValue("")] public string trailing_comments { get { return _trailing_comments; } set { _trailing_comments = value; } } 

And added --include_source_info to call the duct (ProtoBuf.CodeGenerator.InputFileLoader)

And places with comments were added to the generated XML:

 <?xml version="1.0" encoding="utf-16"?> <FileDescriptorSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <file> <FileDescriptorProto> <name>Test.proto</name> <dependency /> <message_type> <DescriptorProto> <name>Test2</name> <field> <FieldDescriptorProto> <name>IntValue</name> <number>1</number> <type>TYPE_INT32</type> </FieldDescriptorProto> </field> <extension /> <nested_type /> <enum_type /> <extension_range /> </DescriptorProto> </message_type> <enum_type /> <service /> <extension /> <source_code_info> <location> ... <Location> <path> <int>4</int> <int>0</int> <int>2</int> <int>0</int> </path> <span> <int>1</int> <int>0</int> <int>28</int> </span> <trailing_comments> some comment </trailing_comments> </Location> ... </location> </source_code_info> </FileDescriptorProto> </file> </FileDescriptorSet> 

source.proto:

 message Test2{ optional int32 IntValue = 1;// some comment } 

But I am not good at xslt for updating ProtoGen / csharp.xslt for including comments in the generated CS file

+4
source

All Articles