Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate C# from proto files and vice versa interpreting custom options
    text
    copied!<p>I'm using <a href="http://code.google.com/p/protobuf-net/" rel="nofollow">protobuf-net</a>, and I'm trying to:</p> <ol> <li>Generate a C# class from a .proto file </li> <li>Generate a .proto file from a C# class</li> </ol> <p>That's pretty easy using respectively:</p> <ol> <li><code>protogen.exe</code> tool</li> <li><code>Serializer&lt;T&gt;.GetProto()</code> method</li> </ol> <p>But the thing is that I need to support protobuffer <a href="http://code.google.com/apis/protocolbuffers/docs/proto.html#options" rel="nofollow">custom options</a> and it doesn't seem to be as straightforward as I though.</p> <p>Let me explain:</p> <ol> <li>From a proto file containing custom options applied on messages and fields, I want to generate a C# class decorated by .NET custom attributes.</li> <li>From a C# class decorated by custom attributes, I would like to generate a proto file where custom options are applied on messages and fields.</li> </ol> <p>Basically, given:</p> <pre><code>message person { option (my_message_option) = true; optional string firstname = 1 [(my_field_option) = 42]; optional string lastname = 2 [(my_field_option) = 12]; optional int age = 3; } </code></pre> <p>I want to generate:</p> <pre><code>[ProtoContract, MyMessageOption(true)] public class Person { [ProtoMember(1), MyFieldOption(42)] public string Firstname; [ProtoMember(2), MyFieldOption(12)] public string Firstname; [ProtoMember(3)] public string Firstname; } </code></pre> <p>...and vice versa.</p> <p>Notes : </p> <ul> <li>The custom option definitions (<code>my_message_option</code> and <code>my_field_option</code>) can already exist in a protofile (say, my_custom_options.proto), and the custom attributes classes can also exist somewhere (<code>MyMessageOptionAttribute</code> and <code>MyFieldOptionAttribute</code>).</li> <li>I tried to use protogen.exe and a custom xslt but protogen doesn't seem to have support for custom options.</li> </ul> <p>What's the preferred way to achieve that? <strong>The solution doesn't have to rely on protobuf-net.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload