代码之家  ›  专栏  ›  技术社区  ›  JDev

proto3:一种在字段级添加元数据的方法

  •  0
  • JDev  · 技术社区  · 6 年前

    我的原始文件看起来像

    syntax = "proto3";
    
    package com.address;
    option java_package = "com.address";
    
    
    message AddressesMessage {
     int32 id = 1;
     string address_line_1 = 4;
     string address_line_2 = 5;
     string city = 7;
     string postal_code = 9;
     string country = 10;
    }
    

    插件正在为我生成类,但是现在我想在字段级别添加一些元数据信息。就像

    syntax = "proto3";
    
    package com.address;
    option java_package = "com.address";
    
    
    message AddressesMessage {
     int32 id = 1 [ (meta) = { isfact: false }];
     string address_line_1 = 4;
     string address_line_2 = 5;
     string city = 7;
     string postal_code = 9;
     string country = 10;
    }
    

    这可能吗?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Marc Gravell    6 年前

    是的,可以通过 custom options ,但是:你需要 定义

    类似于(对于proto2模式,未经测试):

    syntax = "proto2";
    import "google/protobuf/descriptor.proto";
    package MetaPackage;
    message MyMeta {
      optional bool isFact = 1;
    }
    extend google.protobuf.FieldOptions {
      optional MyMeta meta = 80412; // numbering: search for "One last thing" in the link above
    }
    

    再加上:

    import "MyMeta.proto";
    

    应该 工作。不过,访问元数据是另一个主题!请参见上面的链接。