Protocol Buffer Types

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

String

string some_string = 1;

The default value is empty string (""). The field contains UTF-8 encoded or 7-bit ASCII text.

Boolean

bool some_bool = 1;

The default value is false.

Scalar Number Types

int32

int32 some_int = 1;

The default value is 0.

uint32

sint32

fixed32

sfixed32

int64

uint64

sint64

fixed64

sfixed64

float

double

Bytes

bytes some_bytes = 1;

The default value is the empty byte array.

List (Array)

Use pluralized names for repeated fields.

repeated <some_other_type> <field_name> = <tag>;

Example:

repeated int32 sizes = 1;
repeated string names = 1;

The default value is the empty list.

enum

The enum must start with the tag 0. The default value is the first value. Always start an enum

enums can be defined at the top level of a .proto file or they can be embedded.

Use PascalCase (with an initial capital) for enum type names and CAPITALS_WITH_UNDERSCORES for value names. Each enum value should end with a semicolon, not a comma. Prefer prefixing enum values instead of surrounding them in an enclosing message. Since some languages don’t support an enum being defined inside a “struct” type, this ensures a consistent approach across binding languages. The zero value enum should have the suffix UNSPECIFIED, because a server or application that gets an unexpected enum value will mark the field as unset in the proto instance. The field accessor will then return the default value, which for enum fields is the first enum value.

enum FooBar {
  FOO_BAR_UNSPECIFIED = 0;
  FOO_BAR_FIRST_VALUE = 1;
  FOO_BAR_SECOND_VALUE = 2;
}
message Something {
    FooBar foo_bar = 1;
}

To see what Go code is generated for an enum, go to:

Protocol Buffers Go Code Generation | enums

Other Common Types

Google Common Types

https://github.com/googleapis/googleapis/tree/master/google/type

date

https://github.com/googleapis/googleapis/blob/master/google/type/date.proto

datetime

dayofweek

https://github.com/googleapis/googleapis/blob/master/google/type/dayofweek.proto

decimal

expr

fraction

interval

money

https://github.com/googleapis/googleapis/blob/master/google/type/money.proto

month

phone_number

postal_address

timeofday