marshal library

Descriptor-driven protobuf message marshaling (encoding).

Encodes a message represented as Map<String, Object?> into protobuf binary bytes, using a field descriptor list to determine field numbers, types, and encoding rules.

Each field descriptor is a Map<String, Object?> with:

  • 'name' : field name (String)
  • 'number' : field number (int)
  • 'type' : protobuf type string, e.g. 'TYPE_INT32', 'TYPE_STRING'
  • 'label' : 'LABEL_REPEATED' for repeated fields; absent or other value for singular fields
  • 'typeName' : for TYPE_MESSAGE / TYPE_ENUM, the fully-qualified message or enum type name (optional for scalar types)
  • 'mapEntry' : if present and true, indicates this message type is a map entry (has key = 1 and value = 2)
  • 'keyType' : for map fields, the protobuf type of the key
  • 'valueType': for map fields, the protobuf type of the value
  • 'messageDescriptor' : for TYPE_MESSAGE fields, the nested field descriptor list for the submessage

Proto3 rules applied:

  • Singular scalar fields with default values (0, false, "", empty list/map) are not serialized.
  • Repeated scalar fields use packed encoding by default.
  • Map fields are serialized as repeated message fields with key=1, value=2.

References:

Functions

encodeGroupField(List<int> buffer, int fieldNumber, List<int> body) List<int>
Encodes a message field using DELIMITED (group) wire format: a START_GROUP tag (wire type 3), the submessage body, then a matching END_GROUP tag (wire type 4). This is the editions message_encoding = DELIMITED representation (and the proto2 group encoding). Unlike length-prefixed (wire type 2) the body is NOT length-prefixed — it is terminated by the END_GROUP tag.
marshal(Map<String, Object?> message, List<Map<String, Object?>> descriptor) List<int>
Marshals a message (message) to protobuf binary bytes using descriptor.
marshalField(List<int> buffer, int fieldNumber, String type, Object? value, {bool repeated = false, bool explicitPresence = false, bool delimited = false, List<Map<String, Object?>>? messageDescriptor}) List<int>
Marshals a single scalar field value and appends it to buffer.
marshalMapField(List<int> buffer, int fieldNumber, Map<Object?, Object?> mapValue, String keyType, String valueType, {List<Map<String, Object?>>? valueDescriptor}) List<int>
Marshals a map field as repeated key-value pair messages.
marshalRepeated(List<int> buffer, int fieldNumber, String type, List<Object?> values) List<int>
Marshals a repeated scalar field using packed encoding and appends it to buffer.
sizeOfMessage(Map<String, Object?> message, List<Map<String, Object?>> descriptor) int
Calculates the encoded byte size of message without producing output.
wireTypeForFieldType(String type) int
Returns the wire type for a protobuf field type string.