Protocol buffers

Protocol buffers

Protocol buffers are a compact binary format for messages between programs, described in .proto files; gRPC services and many APIs use them. luma proto reads a .proto file and writes Luma code for its messages:

luma proto shop.proto
wrote shop.luma

For a file like this one:

syntax = "proto3";
package shop;

message Item {
  string name = 1;
  int64 cents = 2;
  repeated string tags = 3;
}

enum Status {
  NEW = 0;
  PAID = 1;
}

message Order {
  int32 id = 1;
  repeated Item items = 2;
  Status status = 3;
  optional string note = 4;
}

shop.luma has an obj type for each message, and a lock for each value of an enum:

pub lock STATUS_NEW: int = 0
pub lock STATUS_PAID: int = 1

pub obj Item {
    name: str = ""
    cents: int = 0
    tags: [str] = []
    fn encode(): [byte] { ... }
    fn decode(data: [byte]) { ... }
}

pub obj Order {
    id: int = 0
    items: [Item] = []
    status: int = 0 // Status: STATUS_...
    note: ?str
    ...
}

Using them

Import the file, and use the types like any other. encode() gives a message’s bytes; decode(data) reads bytes into a new object:

import "shop"

o: shop.Order = shop.Order { id: 7, status: shop.STATUS_PAID }
o.items[] = shop.Item { name: "tea", cents: 450, tags: ["green"] }
data: [byte] = o.encode()

back: shop.Order = shop.Order {}
back.decode(data)
print(back.items[0].name)     // tea

Data that isn’t such a message raises error 100, invalid protobuf data: ..., which try catches. Fields decode doesn’t know are skipped, so a program keeps working when the sender has newer fields.

The types

proto3Luma
int32, int64, uint32, uint64, sint32, sint64, fixed32, fixed64, sfixed32, sfixed64int (a uint64 over 2^63 is negative, as in Java)
double, floatfloat
bool, stringbool, str
bytes[byte]
an enumint, with a pub lock for each value: STATUS_PAID (a value already named STATUS_... keeps its name)
a message?Item: nil when it isn’t there
repeated T, map<K, V>[T], {K: V}
optional T?T: nil when it isn’t there
oneofeach of its fields a ?T; set one of them
  • A message inside a message is Outer_Inner: Order.Line is shop.Order_Line.
  • A field named like a Luma keyword gets a _: match is match_ (in the data it is the same field: fields go by number).
  • A field that is its default (0, "", false, empty) isn’t sent, as proto3 says.
  • Comments before messages, fields and enums come along into the Luma code.

Well-known types

Google’s well-known types come with Luma: import "google/protobuf/timestamp.proto"; works without the file, and luma proto writes their Luma next to yours (google/protobuf/timestamp.luma, …). Some of them are Luma types in your messages:

protoLuma
google.protobuf.Timestamp?datetimeon the local clock, exact to the millisecond: finer nanos are dropped
Int32Value, Int64Value, UInt32Value, UInt64Value?intnil when it isn’t there; 0 is there
DoubleValue, FloatValue, BoolValue, StringValue, BytesValue?float, ?float, ?bool, ?str, ?[byte]the same
Struct, Value, ListValue?jsona whole number is an int, as JSON has it
message Event {
  google.protobuf.Timestamp at = 1;
  google.protobuf.Int32Value retries = 2;
  google.protobuf.Struct data = 3;
}
e: ev.Event = ev.Event { at: now(), retries: 0 }
d: json = {"source": "sensor", "level": 3}
e.data = d

A Timestamp is a moment in UTC: Luma turns it into the local clock’s datetime and back, as convert does with a Unix time (a time that happens twice when the clocks go back is one of them). In a list or map they are [datetime], {str: datetime}.

Duration, Empty, Any and FieldMask are messages as any other: duration.Duration { seconds: 90 }, empty.Empty {} (an rpc that takes or gives nothing), any.Any { type_url: ..., value: m.encode() }, field_mask.FieldMask { paths: ["name"] }. So are Api, Type and the rest of their files. descriptor.proto (proto2) isn’t there.

Imports

A .proto file that imports another (import "common/money.proto";) gets import "common/money", and luma proto writes that file’s Luma too, next to its .proto. Its types are money.Money.

Services

For each service, luma proto writes a client and a server: see gRPC. Every file also gets proto_files(), its descriptors for reflection.

Details

  • luma proto writes plain Luma: the encoding is in the file itself, on buffers. Change the .proto and run luma proto again; don’t change the .luma.
  • The bytes are protobuf’s: other languages read what Luma writes, and the other way around. Repeated numbers are written packed, and read packed or not.
  • Only proto3: not proto2, editions, extensions or groups.
  • Messages are converted whole: the program has them all in memory.