Skip to content

Implement #712: idiomatic protobuf map<K,V> support (read + write) - #719

Open
cowtowncoder wants to merge 3 commits into
2.21from
tatu-claude/2.21/712-protobuf-map-support
Open

Implement #712: idiomatic protobuf map<K,V> support (read + write)#719
cowtowncoder wants to merge 3 commits into
2.21from
tatu-claude/2.21/712-protobuf-map-support

Conversation

@cowtowncoder

Copy link
Copy Markdown
Member

Maps are encoded on the wire exactly like a repeated entry sub-message (key = tag 1, value = tag 2). This exposes them idiomatically as JSON Objects in both directions, at streaming and databind levels.

Schema (TypeResolver / ProtobufField): a map<K,V> field (or, from a protoc descriptor set, a repeated field whose entry message carries the map_entry option) is resolved into a synthetic entry message plus a repeated, map-flagged field. Key types are restricted to integral / bool / string.

Generator: a map field is written as a sequence of length-delimited entry sub-messages; each writeFieldName opens an entry (key rendered from the JSON field name per the key type) and primes the value field.

Parser: a map surfaces as START_OBJECT + FIELD_NAME(key)/value pairs + END_OBJECT, mirroring the unpacked-repeated-message machinery (with an entry context so end-offset tracking survives buffer reloads). Absent key/value decode to proto3 defaults; a value that precedes the key is reported clearly.

Descriptor path (FileDescriptorSet): propagate the map_entry message option so .desc-loaded maps get the same treatment as .proto ones. NOTE: this changes the shape of .desc-loaded maps, which previously surfaced as an array of {key,value} entries.

Also flips the two #708 tests that asserted maps throw, and updates the now-stale notes in ProtobufSchemaPreprocessor. Schema generation from POJO Map fields (ProtoBufSchemaVisitor.expectMapFormat) remains unsupported; left for a follow-up.

Maps are encoded on the wire exactly like a `repeated` entry sub-message
(key = tag 1, value = tag 2). This exposes them idiomatically as JSON Objects
in both directions, at streaming and databind levels.

Schema (TypeResolver / ProtobufField): a `map<K,V>` field (or, from a protoc
descriptor set, a `repeated` field whose entry message carries the `map_entry`
option) is resolved into a synthetic entry message plus a repeated, map-flagged
field. Key types are restricted to integral / bool / string.

Generator: a map field is written as a sequence of length-delimited entry
sub-messages; each writeFieldName opens an entry (key rendered from the JSON
field name per the key type) and primes the value field.

Parser: a map surfaces as START_OBJECT + FIELD_NAME(key)/value pairs +
END_OBJECT, mirroring the unpacked-repeated-message machinery (with an entry
context so end-offset tracking survives buffer reloads). Absent key/value decode
to proto3 defaults; a value that precedes the key is reported clearly.

Descriptor path (FileDescriptorSet): propagate the `map_entry` message option so
.desc-loaded maps get the same treatment as .proto ones. NOTE: this changes the
shape of .desc-loaded maps, which previously surfaced as an array of
{key,value} entries.

Also flips the two #708 tests that asserted maps throw, and updates the now-stale
notes in ProtobufSchemaPreprocessor. Schema generation from POJO Map fields
(ProtoBufSchemaVisitor.expectMapFormat) remains unsupported; left for a follow-up.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant