【问题标题】:Preserving proto comments when generating C# with protobuf-net使用 protobuf-net 生成 C# 时保留 proto 注释
【发布时间】:2013-02-12 10:57:04
【问题描述】:

我们正在使用 protobuf-net 来处理 C# 应用程序中的协议缓冲区需求。由于我们与其他非托管应用程序共享我们的 .proto 文件,因此我们从 .proto 文件生成我们的代码(不使用代码优先的 protobuf-net 方法)。为了尽可能保持DRY,我们在 .proto 文件中保留了大量的接口文档。我们通过项目构建目标调用的 protogen.exe 生成 C# 代码。

现在,有什么方法可以(自动)将这些 cmets 传输到已编译的 C# 代码中?

基本上,给定一个像这样的 .proto:

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

...我想要这样的东西(为了便于阅读,省略了 IExtensible 方法):

/// <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; }
    }
}

【问题讨论】:

  • 据我所知,现在这适用于 proto 3.0.0 中的 protoc。 Itz 适用于 C# 和 Java,但不适用于 C++ AFAIKT
  • 是的,终于!像魅力一样工作。

标签: c# .net protocol-buffers protobuf-net documentation-generation


【解决方案1】:

实际上当前版本确实支持 cmets。它可以通过 --include_source_info 启用。

descriptor.Location[n].leading_cmets 和 trailing_cmets 中有注释: https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto

我已经在 protobuf-net Location 类中添加了相应的属性:

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; }
}

并在protoc调用(ProtoBuf.CodeGenerator.InputFileLoader)中添加了--include_source_info

并将带有 cmets 的位置添加到生成的 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>

源.proto:

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

但我不擅长更新 ProtoGen/csharp.xslt 以将 cmets 包含到生成的 CS 文件中

【讨论】:

【解决方案2】:

目前,我相信答案是“不”。据我所知,“protoc”(Google 用于解析 .proto 文件的工具,在后台使用)默默地丢弃了 cmets - 因此没有任何可读取的内容。如果编写了自定义解析器,那么是可能的,但是对于哪些 cmets 适用于哪些行也存在语言歧义,例如:

// 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;

所以有两个不同的原因:目前,没有。不过,所有建议都考虑在内...特别是如果它们带有自定义解析器:)

请注意,由于这个原因,大多数(全部?)实现都缺少 AFAIK 的此信息。不过,我很高兴得到纠正。

【讨论】:

  • 语言歧义可以通过定义一个约定并遵守它来解决。我想有人应该对 protoc 提出功能请求,以在输出中包含 cmets,除非它已经在列表中。自定义解析器写起来都很烦人,而且我见过的大多数都没有完全实现 .proto 语法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多