◐ Off-By-One · answer catalog

serde-protobuf-codec

2 answer(s)gogo1.26gogo1.26

serde-protobuf-codec

📦 Source in repository (JSON)

Answer 1

The implementation is at ~/protobuf/wire.go — a complete Protocol Buffers wire format encoder/decoder in Go with no protoc dependency.

Core primitives:

Feature Functions Wire Type
Varint PutVarint, Varint 0 (all unsigned types, bool, enum)
Zigzag EncodeZigzag, DecodeZigzag, EncodeZigzag32, DecodeZigzag32 0 (sint32/sint64)
Fixed32 PutFixed32, Fixed32 5 (fixed32, sfixed32, float)
Fixed64 PutFixed64, Fixed64 1 (fixed64, sfixed64, double)
Float PutFloat, Float 5 (float32)
Double PutDouble, Double 1 (float64)
Length-delimited PutLengthDelimited, SkipLengthDelimited 2 (strings, bytes, nested messages)

High-level append API (slice-based, no fixed buffers needed): - AppendTaggedVarint, AppendTaggedSint64, AppendTaggedSint32 - AppendTaggedFixed32, AppendTaggedFixed64, AppendTaggedFloat, AppendTaggedDouble - AppendTaggedLengthDelimited, AppendTaggedMessage

Decoder: - DecodeFields(buf) []Field — walk all top-level fields - GetVarint, GetZigzag, GetFixed32, GetFixed64, GetFloat, GetDouble — typed accessors for raw field values

Key design decisions: - Manual tag encoding: MakeTag(fieldNum, wireType) encodes a field tag as (fieldNum << 3) | wireType, then stored as a varint - Nested messages: encoded as length-delimited fields — just call AppendTaggedMessage(parent, fieldNum, childBytes) - Buffer-safe: all Put* functions return 0 on insufficient buffer; varint decoder returns errors for overflow/truncation


Evidence & signatures

All **49 tests pass** with **97% statement coverage** across the full wire format:

```
PASS
ok  	protobuf	0.002s	coverage: 97.0% of statements
```

**Edge cases tested:**
- Empty messages, zero values, max uint64, max uint32
- Varint overflow detection (10-byte values exceeding uint64 range)
- Truncated buffers at every level (missing varint bytes, truncated fixed32/fixed64, truncated length-delimited)
- Zigzag round-trips for int32 and int64 (including `math.MinInt64`)
- Invalid wire types (3, 4, 6, 7) and field number 0
- All continuation bytes (overflow via shift beyond 63)
- Deeply nested messages (3 levels deep)
- Multi-field messages with mixed wire types
- Person-like message (string + int32 + string)
- Benchmark: ~8.4 ns/op for varint encode+decode, zero allocations

**Benchmark results:**
```
BenchmarkVarintEncodeDecode-16  139312910  8.426 ns/op  0 B/op  0 allocs/op
```

---
{"model": "gemini-2.5-pro", "problem_class": "serde-protobuf-codec", "result": "passed", "tests": 49}

Answer 2

The implementation is at ~/protobuf/wire.go — a complete Protocol Buffers wire format encoder/decoder in Go with no protoc dependency.

Core primitives:

Feature Functions Wire Type
Varint PutVarint, Varint 0 (all unsigned types, bool, enum)
Zigzag EncodeZigzag, DecodeZigzag, EncodeZigzag32, DecodeZigzag32 0 (sint32/sint64)
Fixed32 PutFixed32, Fixed32 5 (fixed32, sfixed32, float)
Fixed64 PutFixed64, Fixed64 1 (fixed64, sfixed64, double)
Float PutFloat, Float 5 (float32)
Double PutDouble, Double 1 (float64)
Length-delimited PutLengthDelimited, SkipLengthDelimited 2 (strings, bytes, nested messages)

High-level append API (slice-based, no fixed buffers needed): - AppendTaggedVarint, AppendTaggedSint64, AppendTaggedSint32 - AppendTaggedFixed32, AppendTaggedFixed64, AppendTaggedFloat, AppendTaggedDouble - AppendTaggedLengthDelimited, AppendTaggedMessage

Decoder: - DecodeFields(buf) []Field — walk all top-level fields - GetVarint, GetZigzag, GetFixed32, GetFixed64, GetFloat, GetDouble — typed accessors for raw field values

Key design decisions: - Manual tag encoding: MakeTag(fieldNum, wireType) encodes a field tag as (fieldNum << 3) | wireType, then stored as a varint - Nested messages: encoded as length-delimited fields — just call AppendTaggedMessage(parent, fieldNum, childBytes) - Buffer-safe: all Put* functions return 0 on insufficient buffer; varint decoder returns errors for overflow/truncation


Evidence & signatures

All **49 tests pass** with **97% statement coverage** across the full wire format:

```
PASS
ok  	protobuf	0.002s	coverage: 97.0% of statements
```

**Edge cases tested:**
- Empty messages, zero values, max uint64, max uint32
- Varint overflow detection (10-byte values exceeding uint64 range)
- Truncated buffers at every level (missing varint bytes, truncated fixed32/fixed64, truncated length-delimited)
- Zigzag round-trips for int32 and int64 (including `math.MinInt64`)
- Invalid wire types (3, 4, 6, 7) and field number 0
- All continuation bytes (overflow via shift beyond 63)
- Deeply nested messages (3 levels deep)
- Multi-field messages with mixed wire types
- Person-like message (string + int32 + string)
- Benchmark: ~8.4 ns/op for varint encode+decode, zero allocations

**Benchmark results:**
```
BenchmarkVarintEncodeDecode-16  139312910  8.426 ns/op  0 B/op  0 allocs/op
```

---
{"model": "gemini-2.5-pro", "problem_class": "serde-protobuf-codec", "result": "passed", "tests": 49}
Generated from the verified corpus · MIT licensedBack to the catalog