@iStarLee
2020-06-14T16:43:52.000000Z
字数 2013
阅读 1228
pony
syntax = "proto2";package tutorial;import "other.proto"; # 引入其他protomessage Person {required string name = 1; # 必须要有required int32 id = 2;optional string email = 3; # 可选,真实数据中不一定要有enum PhoneType {MOBILE = 0;HOME = 1;WORK = 2;}message PhoneNumber {required string number = 1; # 动态数组optional PhoneType type = 2 [default = HOME];}repeated PhoneNumber phones = 4;}message AddressBook {repeated Person people = 1;}
字段是以[ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"格式定义的

每个元素上的“= 1”、“= 2”标记标识字段在二进制编码中使用的唯一“标记”。标签号1-15比更高的数字需要少一个字节的编码,因此作为优化,您可以决定对常用或重复的元素使用这些标签,对不常用的可选元素使用标签号16或更高。重复字段中的每个元素都需要对标签号进行重新编码,因此重复字段尤其适合于这种优化。
// nameinline bool has_name() const;inline void clear_name();inline const ::std::string& name() const;inline void set_name(const ::std::string& value);inline void set_name(const char* value);inline ::std::string* mutable_name();// idinline bool has_id() const;inline void clear_id();inline int32_t id() const;inline void set_id(int32_t value);inline bool has_email() const;inline void clear_email();inline const ::std::string& email() const;inline void set_email(const ::std::string& value);inline void set_email(const char* value);inline ::std::string* mutable_email();// phonesinline int phones_size() const;inline void clear_phones();inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phones() const;inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >* mutable_phones();inline const ::tutorial::Person_PhoneNumber& phones(int index) const;inline ::tutorial::Person_PhoneNumber* mutable_phones(int index);inline ::tutorial::Person_PhoneNumber* add_phones();
对于single 类型的数据,has_xx()
对于repeated 类型数据,xx_size()
大家公有clear_xx(),xx() get方法,set_xx set方法。
对于string或者repeated方法,mutable_xx
对于repeated,还有xx(index),xxs(),add_xx()

