A Protobuf message is a schema-defined structured data type. Declare it in a .proto file with named, typed fields and unique numeric identifiers that consumers use to recognize fields across compatible versions. section 10.1.1
A practical message declaration follows these rules:
- Give every field a unique identifier and preserve assigned numbers permanently when maintaining compatibility. section 10.1.1
- Write each field with its type, name, and numeric identifier; use comments to document how consumers should use it. section 10.1.1
- Use messages as service request and response payloads, including wrapper messages when an endpoint needs future-compatible changes. section 10.1.1
Habit entity
A habit message can contain a string name with identifier 1 and an int32 weekly_frequency with identifier 2. A CreateHabitRequest can reuse those ideas, while CreateHabitResponse returns a Habit message. section 10.1.1
Listing 10.3 habit.proto: Defining the Habit message
// Habit represents an objective one wants to complete
// a given number of times per week.
message Habit { #1
// Name of the habit, cannot be empty
string name = 1; #2
// Frequency, expressed in times per week.
int32 weekly_frequency = 2;
}
Inspect the complete Habit declaration, including field comments, types, and identifiers. section 10.1.1
