Define dictionary in protocol buffer

前端 未结 2 1026
再見小時候
再見小時候 2021-01-05 23:48

I\'m new to both protocol buffers and C++, so this may be a basic question, but I haven\'t had any luck finding answers. Basically, I want the functionality of a dictionary

相关标签:
2条回答
  • 2021-01-06 00:20

    In proto3, it's:

    extend google.protobuf.EnumValueOptions {
      string name = 12345;
    }
    
    enum UnitType {
      KM_PER_HOUR = 0 [(name) = "km/h"];
      MI_PER_HOUR = 1 [(name) = "mph"];
    }
    

    and to access it in Java:

    UnitType.KM_PER_HOUR.getValueDescriptor().getOptions().getExtension(MyOuterClass.name);
    
    0 讨论(0)
  • 2021-01-06 00:27

    You could use custom options to associate a string with each enum member: https://developers.google.com/protocol-buffers/docs/proto#options

    It would look like this in the .proto:

    extend google.protobuf.FieldOptions {
      optional string name = 12345;
    }
    
    enum UnitType {
        KmPerHour = 1 [(name) = "km/h"];
        MiPerHour = 2 [(name) = "mph"];
    }
    

    Beware, though, that some third-party protobuf libraries don't understand these options.

    0 讨论(0)
提交回复
热议问题