// 17 bytes, field 1 to 4
08 2a
12 04 6d 6f 6f 6e
1a 05 0a 01 61 10 01
20 01
Protocol Buffers · MoonBit
Protocol Buffers for MoonBit.
Binary, JSON and text encodings, the descriptor model, and a protoc plugin that writes ordinary structs — no hidden dynamic message.
moon add moonbitstack/moonproto
message Envelope { int32 id = 1; string name = 2; map<string, int32> counts = 3; Color color = 4; }
08 2a 12 04 6d 6f 6f 6e 1a 05 0a 01 61 10 01 20 01
| 1 id | 42 |
| 2 name | "moon" |
| 3 counts | {"a": 1} |
| 4 color | BLUE |
One message, three encodings
Every encoder walks the same descriptor through the same trait, so a message type implements the mapping once.
{
"id": 42,
"name": "moon",
"counts": {
"a": 1
},
"color": "BLUE"
}
id: 42 name: "moon" counts { key: "a" value: 1 } color: BLUE
What is in the box
One module, thirteen packages. Import the ones a program needs.
Binary
Varints, fixed-width numbers, length-delimited values and groups. Unknown fields are kept and written back.
JSON
64-bit integers as strings, enums by name, bytes in base64, and the well-known type forms.
Text
The full grammar: groups, lists, separators, octal and hexadecimal literals, comments.
Generated structs
protoc-gen-mbt writes a struct whose fields are the protobuf fields. Repeated becomes Array, map becomes Map, oneof becomes an enum.
Descriptors at run time
A .proto parser builds the descriptor model in memory, so a schema can be read without a code generation step.
One trait
Encode, decode, size, merge, JSON and text all reach a message through reflect.Message.