[GO] Summary of how to write .proto files used in gRPC

--In gRPC, data is exchanged by serializing in the Protocol Buffers format. --You can define the schema in the .proto file and generate code using the tool. Therefore, if this .proto file can be shared by each client and server, it is possible to proceed with development without any deviation in specifications. --In this article, I will briefly summarize the syntax of .proto files.

Whole sample

//Version definition
syntax = "proto3";

//Package definition
package sample;

// import
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";

//Service and RPC method definition
service SampleService {
    rpc Sample (SampleRequest) returns (SampleResponse);

    //Server streaming RPC
    rpc SampleServerStreamMethod (SampleRequest) returns (stream SampleResponse);

    //Client Streaming RPC
    rpc SampleClientStreamMethod (stream SampleRequest) returns (SampleResponse)

    //Bidirectional streaming RPC
    rpc SampleBidirectionalMethod (stream SampleRequest) returns (stream SampleResponse)
}

message SampleRequest {
    string name = 1;
}

message SampleResponse {
    Sample sample = 1;
}


//Message type
//The number on the right is the "tag number"
message Sample {
    //Scalar type
    //There are numbers, strings, booleans, byte arrays
    int32 id = 1;
    string name = 2;
    bool isBool = 3;

    //deprecated Specify fields that are deprecated and deprecated
    string duplicated_field = 4 [deprecated = true]

    //reserved identifier Discontinued tag number
    reserved 7, 8, 10 to 12;

    //List (array)
    //Multidimensional array cannot be defined
    repeated SampleList sample_list = 5;

    //Map (associative array)
    map<string, string> sample_map = 6;

    //Any one from multiple
    oneof message {
        string one = 1;
        string other = 2;
    }

    // Well Known Types
    google.protobuf.Duration sample_duration = 9;
    google.protobuf.Timestamp create_time = 13;

    //Enum
    enum SampleEnum {
        UNKNOWN = 0;
        TEST1 = 1;
        TEST2 = 2;
        TEST3 = 3;
    }

}

Version definition

syntax = "proto3";

Package definition

--You can set the package name to avoid name conflicts, such as when using messages defined in other .proto files.

package sample;

import --Use when you want to use the message type defined in another .proto file --Here, we are importing a .proto file created by Google.

import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";

Services and RPC methods

--API definition body --Define a service called SampleService --Defines a method called Sample that takes SampleRequest as an argument and returns SampleResponse. --If you want to stream, add stream to the argument and return value. --One-way streaming --Server streaming RPC (set stream as return value) --The server side returns multiple responses asynchronously --Client Streaming RPC (set stream as an argument) --Send multiple requests asynchronously from the client side, and return one response on the server side --Bidirectional streaming --Add stream to both argument and return value

service SampleService {
    rpc Sample (SampleRequest) returns (SampleResponse);

    //Server streaming RPC
    rpc SampleServerStreamMethod (SampleRequest) returns (stream SampleResponse);

    //Client Streaming RPC
    rpc SampleClientStreamMethod (stream SampleRequest) returns (SampleResponse);

    //Bidirectional streaming RPC
    rpc SampleBidirectionalMethod (stream SampleRequest) returns (stream SampleResponse);
}

Scalar type

--Numeric value, character string, boolean value, byte array are as data types -Details of data types and correspondence of data types after compiling in each language

Data type Default value
string Empty string
bytes Empty array
bool false
Numerical value 0
enum The first defined value. Must be 0
Message type Implementation dependent
repeated Empty array

Message type

--Type with multiple fields --Tag number --The number to the right of the field. --Must be unique within the same message --reserved identifier --Enter the tag number that was discontinued due to field deletion etc. --deprecated option --Specify deprecated and deprecated fields

message Sample {
    //Scalar type
    //There are numbers, strings, booleans, byte arrays
    int32 id = 1;
    string name = 2;
    bool isBool = 3;

    //deprecated Specify fields that are deprecated and deprecated
    string duplicated_field = 4 [deprecated = true]

    //reserved identifier Discontinued tag number
    reserved 7, 8, 10 to 12;

  //~ Omitted ~
}

List (array)

--An array can be defined by prefixing the type with repeated --Can be used for both scalar type and message type --Multidimensional arrays cannot be defined

    //List (array)
    //Multidimensional array cannot be defined
    repeated SampleList sample_list = 5;

Map (associative array)

--Only integer values, strings, and boolean values can be used for keys. --map cannot be an array

    map<string, string> sample_map = 6;

Enum

--Enums can be defined by prefixing the type with enum

    //Enum
    enum SampleEnum {
        UNKNOWN = 0;
        TEST1 = 1;
        TEST2 = 2;
        TEST3 = 3;
    }

oneof --By adding oneof, you can define one from multiple things. --Here it is a message type that returns ʻone or ʻother

    oneof message {
        string one = 1;
        string other = 2;
    }

Well Known Types --Google-defined message type --There are things such as the date and time / period and the fact that the return value is not returned

    google.protobuf.Duration sample_duration = 9;
    google.protobuf.Timestamp create_time = 13;

Code generation

--Based on the .proto file defined above, generate boilerplate code using the protoc command for each language on both sides of the client server.

reference

-Starting gRPC

Recommended Posts

Summary of how to write .proto files used in gRPC
Summary of how to import files in Python 3
Summary of how to write AWS Lambda
Summary of how to use MNIST in Python
Summary of how to use pandas.DataFrame.loc
How to write soberly in pandas
Summary of how to use pyenv-virtualenv
Summary of how to use csvkit
How to display a specified column of files in Linux (awk)
[Python] Summary of how to use pandas
Summary of methods often used in pandas
Summary of frequently used commands in matplotlib
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to read CSV files in Pandas
How to write this process in Perl?
How to write Ruby to_s in Python
Summary of what was used in 100 Pandas knocks (# 1 ~ # 32)
Summary of tools used in Command Line vol.8
Summary of tools used in Command Line vol.5
How to check / extract files in RPM package
How to get the files in the [Python] folder
How to write async and await in Vue.js
How to write a named tuple document in 2020
How to find out what kind of files are stored in S3 in Python
How to write files that you should be careful about in all languages
How to get a list of files in the same directory with python
20th Offline Real-time How to Write Problems in Python
[Blender] Summary of how to install / update / uninstall add-ons
How to write a list / dictionary type of Python3
How to load files in Google Drive with Google Colaboratory
How to display multiple images of galaxies in tiles
How to use variables in systemd Unit definition files
How to download files from Selenium in Python in Chrome
How to add page numbers to PDF files (in Python)
How to upload files in Django generic class view
[Python] Summary of how to specify the color of the figure
How to reference static files in a Django project
Summary of how to share state with multiple functions
How to write custom validations in the Django REST Framework
How to find the optimal number of clusters in k-means
How to get rid of server custom emoji in message.content
Offline real-time how to write Python implementation example of E14
[python] Summary of how to retrieve lists and dictionary elements
[Updated from time to time] Summary of design patterns in Java
[Python] How to write an if statement in one sentence.
How to enable Read / Write of net.Conn with context with golang
[Linux] [C / C ++] Summary of how to get pid, ppid, tid
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
How to get a list of built-in exceptions in python
How to develop in Python
How to get an overview of your data in Pandas
How to get a quadratic array of squares in a spiral!
Command to list all files in order of file name
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
Summary of Chapter 2 of Introduction to Design Patterns Learned in Java Language
How to handle multiple versions of CUDA in the same environment
How to determine the existence of a selenium element in Python
[Hugo] Summary of how to add pages to sites built with Learn