Examples #
Integer Encoding Examples #
These cover all combinations of bounds mode, nullability, and edge cases.
See Integer Encoding for the full rules.
| Schema | Value | Mode | Nullable? | Raw e | Wire | Encoding (hex) |
|---|---|---|---|---|---|---|
int |
42 |
two's comp | no | 42 | 42 | 2A |
int |
-42 |
two's comp | no | -42 | -42 | 56 |
int |
1000 |
two's comp | no | 1000 | 1000 | E8 07 |
int[0...100] |
30 |
unsigned | no | 30 | 30 | 1E |
int[...50] |
42 |
unsigned, max-only | no | 8 | 8 | 08 |
int[-10...10] |
-4 |
unsigned | no | 6 | 6 | 06 |
?int[0...100] |
30 |
unsigned | yes | 30 | 31 | 1F |
?int[0...100] |
null | unsigned | yes | — | 0 | 00 |
?int[-10...10] |
-4 |
unsigned | yes | 6 | 7 | 07 |
?int |
-5 |
two's comp | yes | -5 | -6 | 7A |
?int |
null | two's comp | yes | — | -1 | 7F |
Walkthrough: int[...50] value 42 #
- Only max declared → unsigned, max-only mode:
f(e) = max - e 42 = 50 - e, soe = 50 - 42 = 8- Non-nullable, no offset
- Varint encode 8:
00001000=0x08
Walkthrough: ?int[-10...10] value -4 #
- Both bounds declared → unsigned,
f(e) = min + e -4 = -10 + e, soe = -4 - (-10) = 6- Nullable unsigned: offset by +1, wire =
6 + 1 = 7 - Varint encode 7:
00000111=0x07
Walkthrough: ?int value -5 #
- No bounds → two's complement:
f(e) = e e = -5- Nullable two's comp: negative values offset by -1, wire =
-5 - 1 = -6 - Varint encode -6: 7-bit two's comp of -6 =
1111010, byte =01111010=0x7A
Full Record Example #
Given this schema:
name: "person",
hash: "sha3-256",
root: struct{
name: string[3:30],
tags: array{blob[:20]}[:3],
age: int[0...100],
bio: ?string[:300],
role: opt{"admin", "moderator", "user"},
farm: ref(55j8P8SVHid9YwPjcT6W8FVgAP2pLTTAKgFCSgDCqUdr)
}
Encoding a record with values name="Alice", tags=[0xDEAD, 0xBE], age=30, bio=null, role="moderator" (index 1), and the farm ref targets a schema with root type string[:50], holding the value "star of dew".
Struct fields are sorted before encoding. The schema declares them as name, tags, age, bio, role, farm, but they encode in this sorted order: age (3), bio (3), farm (4), name (4), role (4), tags (4) — by ascending length, then big-endian byte value.
Step-by-step #
Schema ID #
The schema ID is the hash of the schema (sha3-256, 32 bytes), encoded as blob[16:256]:
blob[16:256]→ length prefix isint[16...256], unsigned:f(e) = 16 + e- For 32 bytes:
32 = 16 + e, soe = 16 - Varint:
00010000=0x10 - Followed by 32 raw hash bytes
10 // wire=16 (decoded 32)
[32 bytes of schema hash] // sha3-256 hash of the schema
age: int[0...100], 30 #
- Unsigned:
f(e) = 0 + e,e = 30 - Non-nullable, no offset
- Varint:
1E
1E
bio: ?string[:300], null #
- Nullable unsigned: null = wire
0 - Varint 0:
00
00
farm: ref(...), "star of dew" #
- Target schema root type:
string[:50] - Encoded as
string[:50]: length prefixint[0...50], unsigned:f(e) = 0 + e - 11 bytes:
e = 11 - Varint:
0B, followed by UTF-8 bytes73 74 61 72 20 6F 66 20 64 65 77
0B 73 74 61 72 20 6F 66 20 64 65 77
name: string[3:30], "Alice" (5 bytes) #
- Length prefix:
int[3...30](content min=3, max=30) - Unsigned:
f(e) = 3 + e. For 5 bytes:5 = 3 + e,e = 2 - Non-nullable, no offset
- Varint 2:
02 - UTF-8 data:
41 6C 69 63 65
02 41 6C 69 63 65
role: opt{"admin", "moderator", "user"}, "moderator" #
- 3 options → bounds
int[0...2] - Index 1: unsigned
f(e) = 0 + e,e = 1 - Varint:
01
01
tags: array{blob[:20]}[:3], [0xDEAD, 0xBE] #
Element count: 2
- Array count bounds:
int[max(0,0)...3]=int[0...3], unsigned:f(e) = 0 + e,e = 2 - Varint 2:
02
First blob (0xDEAD, 2 bytes):
blob[:20]→ length prefixint[0...20], unsigned:f(e) = 0 + e,e = 2- Varint 2:
02, data:DE AD
Second blob (0xBE, 1 byte):
- Length prefix:
e = 1, varint:01, data:BE
02 // 2 elements
02 DE AD // blob[0]: length=2, data
01 BE // blob[1]: length=1, data
Complete Record #
10 // schema ID: wire=16 (decoded 32)
[32 bytes of schema hash] // schema ID (sha3-256)
1E // age: 30
00 // bio: null
0B 73 74 61 72 20 6F 66 20 64 65 77 // farm: wire=11 (decoded 11), "star of dew"
02 41 6C 69 63 65 // name: wire=2 (decoded 5), "Alice"
01 // role: moderator (index 1 of 3)
02 // tags: 2 elements
02 DE AD // blob[0]: length=2, data=DE AD
01 BE // blob[1]: length=1, data=BE
Total size: 1 + 32 + 1 + 1 + 12 + 6 + 1 + 1 + 3 + 2 = 60 bytes.