Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to use ProtoBuffer in Golang

IXXO.IO
December 06, 2020

How to use ProtoBuffer in Golang

IXXO.IO

December 06, 2020
Tweet

More Decks by IXXO.IO

Other Decks in Technology

Transcript

  1. With protocol buffers, you write a .proto description of the

    data structure you wish to store. From that, the protocol buffer compiler creates a class that implements automatic encoding and parsing of the protocol buffer data with an efficient binary format. The generated class provides getters and setters for the fields that make up a protocol buffer and takes care of the details of reading and writing the protocol buffer as a unit. Importantly, the protocol buffer format supports the idea of extending the format over time in such a way that the code can still read data encoded with the old format. Protocol buffers are the flexible and efficient. Protocol buffers
  2. JSON vs Protocol Buffer •JSON is a text data format

    independent of the platform. •Protobuf uses binary message format that allows programmers to specify a schema for the data. It also includes a set of rules and tools to define and exchange these messages. A schema for a particular use of protocol buffers associates data types with field names, using integers to identify each field. •As JSON is textual, its integers and floats can be slow to encode and decode. JSON is not designed for numbers. Also, Comparing strings in JSON can be slow. •Protobuf is easier to bind to objects and faster. •Protocol buffers currently support generated code in Java, Python, Objective-C, and C++. With proto3 language version, one can also work with Dart, Go, Ruby, and C#, with more languages to come. •JSON is widely accepted by almost all programming languages and highly popular.
  3. { "quiz": { "sport": { "q1": { "question": "Which is

    correct team name in NBA?", "options": [ "Golden State Warriros", "Huston Rocket" ], "answer": "Huston Rocket" } }, "maths": { "q1": { "question": "5 + 7 = ?", "options": ["10","11","12","13"], "answer": "12" } } } message Point { required int32 x = 1; required int32 y = 2; optional string label = 3; } message Line { required Point start = 1; required Point end = 2; optional string label = 3; } message Polyline { repeated Point point = 1; optional string label = 2; } JSON vs Protocol Buffer
  4. Advantages of Protobuf: •Simpler, faster, smaller in size. •RPC support:

    Server RPC interfaces can be declared as part of protocol files. •Structure validation: Having a predefined and larger structure, when compared to JSON, set of data types, messages serialized on Protobuf can be automatically validated by the code that is responsible to exchange them.
  5. •Define message formats in a .proto file. •Use the protocol

    buffer compiler. •Use the Go protocol buffer API to write and read messages. Important stages of a Protocol Buffer
  6. proto3 Defining A Message syntax = "proto3"; message SearchRequest {

    string query = 1; int32 page_number = 2; int32 result_per_page = 3; } Specifying Field Types In the above example, all the fields are scalar types: two integers (page_number and result_per_page) and a string (query). However, you can also specify composite types for your fields, including enumerations and other message types
  7. The smallest field number you can specify is 1, and

    the largest is 229 - 1, or 536,870,911 Notethat field numbers in the range 1 through 15 take one byte to encode, including the field number and the field's. Field numbers in the range 16 through 2047 take two bytes. So you should reserve the numbers 1 through 15 for very frequently occurring message elements. Remember to leave some room for frequently occurring elements that might be added in the future.
  8. Specifying Field Rules Message fields can be one of the

    following: •singular: a well-formed message can have zero or one of this field (but not more than one). And this is the default field rule for proto3 syntax. •repeated: this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved. In proto3, repeated fields of scalar numeric types use packed encoding by default.
  9. Adding More Message Types Multiple message types can be defined

    in a single .proto file. This is useful if you are defining multiple related messages – so, for example, if you wanted to define the reply message format that corresponds to your SearchResponse message type, you could add it to the same .proto: message SearchRequest { string query = 1; int32 page_number = 2; int32 result_per_page = 3; } message SearchResponse { ... }
  10. Reserved Fields If you update a message type by entirely

    removing a field, or commenting it out, future users can reuse the field number when making their own updates to the type. This can cause severe issues if they later load old versions of the same .proto, including data corruption, privacy bugs, and so on. One way to make sure this doesn't happen is to specify that the field numbers (and/or names, which can also cause issues for JSON serialization) of your deleted fields are reserved. The protocol buffer compiler will complain if any future users try to use these field identifiers. message Foo { reserved 2, 15, 9 to 11; reserved "foo", "bar"; }
  11. What's Generated From Your .proto? When you run the protocol

    buffer compiler on a .proto, the compiler generates the code in your chosen language you'll need to work with the message types you've described in the file, including getting and setting field values, serializing your messages to an output stream, and parsing your messages from an input stream. •For C++, the compiler generates a .h and .cc file from each .proto, with a class for each message type described in your file. •For Java, the compiler generates a .java file with a class for each message type, as well as a special Builder classes for creating message class instances. •Python is a little different – the Python compiler generates a module with a static descriptor of each message type in your .proto, which is then used with a metaclass to create the necessary Python data access class at runtime. •For Go, the compiler generates a .pb.go file with a type for each message type in your file.
  12. Default Values When a message is parsed, if the encoded

    message does not contain a particular singular element, the corresponding field in the parsed object is set to the default value for that field. These defaults are type-specific: •For strings, the default value is the empty string. •For bytes, the default value is empty bytes. •For bools, the default value is false. •For numeric types, the default value is zero. •For enums, the default value is the first defined enum value, which must be 0. •For message fields, the field is not set. Its exact value is language-dependent •The default value for repeated fields is empty
  13. Enumerations When you're defining a message type, you might want

    one of its fields to only have one of a pre- defined list of values. In the following example we've added an enum called Corpus with all the possible values, and a field of type Corpus: message SearchRequest { string query = 1; int32 page_number = 2; int32 result_per_page = 3; enum Corpus { UNIVERSAL = 0; WEB = 1; IMAGES = 2; LOCAL = 3; NEWS = 4; PRODUCTS = 5; VIDEO = 6; } Corpus corpus = 4; }
  14. message MyMessage1 { enum EnumAllowingAlias { option allow_alias = true;

    UNKNOWN = 0; STARTED = 1; RUNNING = 1; } } message MyMessage2 { enum EnumNotAllowingAlias { UNKNOWN = 0; STARTED = 1; // RUNNING = 1; // Uncommenting this line will cause a compile error inside Google and a warning message outside. } }
  15. Reserved Values If you update an enum type by entirely

    removing an enum entry, or commenting it out, future users can reuse the numeric value when making their own updates to the type. This can cause severe issues if they later load old versions of the same .proto, including data corruption, privacy bugs, and so on. One way to make sure this doesn't happen is to specify that the numeric values (and/or names, which can also cause issues for JSON serialization) of your deleted entries are reserved. The protocol buffer compiler will complain if any future users try to use these identifiers. You can specify that your reserved numeric value range goes up to the maximum possible value using the max keyword. enum Foo { reserved 2, 15, 9 to 11, 40 to max; reserved "FOO", "BAR"; }
  16. Using Other Message Types You can use other message types

    as field types. For example, let's say you wanted to include Result messages in each SearchResponse message – to do this, you can define a Result message type in the same .proto and then specify a field of type Result in SearchResponse: message SearchResponse { repeated Result results = 1; } message Result { string url = 1; string title = 2; repeated string snippets = 3; }
  17. Nested Types You can define and use message types inside

    other message types, as in the following example – here the Result message is defined inside the SearchResponse message: message SearchResponse { message Result { string url = 1; string title = 2; repeated string snippets = 3; } repeated Result results = 1; }
  18. message Outer { // Level 0 message MiddleAA { //

    Level 1 message Inner { // Level 2 int64 ival = 1; bool booly = 2; } } message MiddleBB { // Level 1 message Inner { // Level 2 int32 ival = 1; bool booly = 2; } } }
  19. Updating A Message Type If an existing message type no

    longer meets all your needs – for example, you'd like the message format to have an extra field – but you'd still like to use code created with the old format, don't worry! It's very simple to update message types without breaking any of your existing code. Just remember the following rules: •Don't change the field numbers for any existing fields. •If you add new fields, any messages serialized by code using your "old" message format can still be parsed by your new generated code. You should keep in mind the default values for these elements so that new code can properly interact with messages generated by old code. Similarly, messages created by your new code can be parsed by your old code: old binaries simply ignore the new field when parsing. See the Unknown Fields section for details. •Fields can be removed, as long as the field number is not used again in your updated message type. You may want to rename the field instead, perhaps adding the prefix "OBSOLETE_", or make the field number reserved, so that future users of your .proto can't accidentally reuse the number. •int32, uint32, int64, uint64, and bool are all compatible – this means you can change a field from one of these types to another without breaking forwards- or backwards-compatibility. If a number is parsed from the wire which doesn't fit in the corresponding type, you will get the same effect as if you had cast the number to that type in C++ (e.g. if a 64-bit number is read as an int32, it will be truncated to 32 bits).
  20. •sint32 and sint64 are compatible with each other but are

    not compatible with the other integer types. •string and bytes are compatible as long as the bytes are valid UTF-8. •Embedded messages are compatible with bytes if the bytes contain an encoded version of the message. •fixed32 is compatible with sfixed32, and fixed64 with sfixed64. •For string, bytes, and message fields, optional is compatible with repeated. Given serialized data of a repeated field as input, clients that expect this field to be optional will take the last input value if it's a primitive type field or merge all input elements if it's a message type field. Note that this is not generally safe for numeric types, including bools and enums. Repeated fields of numeric types can be serialized in the packed format, which will not be parsed correctly when an optional field is expected. •enum is compatible with int32, uint32, int64, and uint64 in terms of wire format (note that values will be truncated if they don't fit). However be aware that client code may treat them differently when the message is deserialized: for example, unrecognized proto3 enum types will be preserved in the message, but how this is represented when the message is deserialized is language-dependent. Int fields always just preserve their value. •Changing a single value into a member of a new oneof is safe and binary compatible. Moving multiple fields into a new oneof may be safe if you are sure that no code sets more than one at a time. Moving any fields into an existing oneof is not safe.
  21. Any The Any message type lets you use messages as

    embedded types without having their .proto definition. An Any contains an arbitrary serialized message as bytes, along with a URL that acts as a globally unique identifier for and resolves to that message's type. To use the Any type, you need to import google/protobuf/any.proto. import "google/protobuf/any.proto"; message ErrorStatus { string message = 1; repeated google.protobuf.Any details = 2; }
  22. Oneof If you have a message with many fields and

    where at most one field will be set at the same time, you can enforce this behavior and save memory by using the oneof feature. Oneof fields are like regular fields except all the fields in a oneof share memory, and at most one field can be set at the same time. Setting any member of the oneof automatically clears all the other members. You can check which value in a oneof is set (if any) using a special case() or WhichOneof() method, depending on your chosen language. message SampleMessage { oneof test_oneof { string name = 4; SubMessage sub_message = 9; } }
  23. Oneof Features •Setting a oneof field will automatically clear all

    other members of the oneof. So if you set several oneof fields, only the last field you set will still have a value. •If the parser encounters multiple members of the same oneof on the wire, only the last member seen is used in the parsed message. •A oneof cannot be repeated. •Reflection APIs work for oneof fields. •If you set a oneof field to the default value (such as setting an int32 oneof field to 0), the "case" of that oneof field will be set, and the value will be serialized on the wire.
  24. Defining Services If you want to use your message types

    with an RPC (Remote Procedure Call) system, you can define an RPC service interface in a .proto file and the protocol buffer compiler will generate service interface code and stubs in your chosen language. So, for example, if you want to define an RPC service with a method that takes your SearchRequest and returns a SearchResponse, you can define it in your .proto file as follows: service SearchService { rpc Search(SearchRequest) returns (SearchResponse); }
  25. Options Individual declarations in a .proto file can be annotated

    with a number of options. Options do not change the overall meaning of a declaration, but may affect the way it is handled in a particular context. The complete list of available options is defined in google/protobuf/descriptor.proto. Some options are file-level options, meaning they should be written at the top-level scope, not inside any message, enum, or service definition. Some options are message-level options, meaning they should be written inside message definitions. Some options are field-level options, meaning they should be written inside field definitions. Options can also be written on enum types, enum values, oneof fields, service types, and service methods; however, no useful options currently exist for any of these.
  26. syntax = "proto3"; package tutorial; import "google/protobuf/timestamp.proto"; option go_package =

    "github.com/protocolbuffers/protobuf/examples/go/tutorialpb"; message Person { string name = 1; int32 id = 2; // Unique ID number for this person. string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2;} message PhoneNumber { string number = 1; PhoneType type = 2; } repeated PhoneNumber phones = 4; google.protobuf.Timestamp last_updated = 5; } message AddressBook { repeated Person people = 1; } Defining your protocol format
  27. Compiling your protocol buffers go install google.golang.org/protobuf/cmd/protoc-gen-go protoc -I=$SRC_DIR --go_out=$DST_DIR

    $SRC_DIR/addressbook.proto This generates github.com/protocolbuffers/protobuf/examples/go/tutorialpb/addressbook.pb.go in your specified destination directory.
  28. The Protocol Buffer API Generating addressbook.pb.go gives you the following

    useful types: •An AddressBook structure with a People field. •A Person structure with fields for Name, Id, Email and Phones. •A Person_PhoneNumber structure, with fields for Number and Type. •The type Person_PhoneType and a value defined for each value in the Person.PhoneType enum. p := pb.Person{ Id: 1234, Name: "John Doe", Email: "[email protected]", Phones: []*pb.Person_PhoneNumber{ {Number: "555-4321", Type: pb.Person_HOME}, }, }
  29. Writing a Message The whole purpose of using protocol buffers

    is to serialize your data so that it can be parsed elsewhere. In Go, you use the proto library's Marshal function to serialize your protocol buffer data. A pointer to a protocol buffer message's struct implements the proto.Message interface. Calling proto.Marshal returns the protocol buffer, encoded in its wire format. For example, we use this function in the add_person command: book := &pb.AddressBook{} // ... // Write the new address book back to disk. out, err := proto.Marshal(book) if err != nil { log.Fatalln("Failed to encode address book:", err) } if err := ioutil.WriteFile(fname, out, 0644); err != nil { log.Fatalln("Failed to write address book:", err) }
  30. Reading a Message To parse an encoded message, you use

    the proto library's Unmarshal function. Calling this parses the data in buf as a protocol buffer and places the result in pb. So to parse the file in the list_people command, we use: // Read the existing address book. in, err := ioutil.ReadFile(fname) if err != nil { log.Fatalln("Error reading file:", err) } book := &pb.AddressBook{} if err := proto.Unmarshal(in, book); err != nil { log.Fatalln("Failed to parse address book:", err) }
  31. Package md5 Package md5 implements the MD5 hash algorithm as

    defined in RFC 1321. NOTE-MD5 is cryptographically broken and should not be used for secure applications. Constants The blocksize of MD5 in bytes. const BlockSize = 64 The size of an MD5 checksum in bytes. const Size = 16
  32. func New func New() hash.Hash package main import ( "crypto/md5"

    "fmt" "io" ) func main() { h := md5.New() io.WriteString(h, "The fog is getting thicker!") io.WriteString(h, "And Leon's getting laaarger!") fmt.Printf("%x", h.Sum(nil)) }
  33. package main import ( "crypto/md5" "fmt" ) func main() {

    data := []byte("These pretzels are making me thirsty.") fmt.Printf("%x", md5.Sum(data)) } func Sum func Sum(data []byte) [Size]byte