encodeTag function

List<int> encodeTag(
  1. List<int> buffer,
  2. int fieldNumber,
  3. int wireType
)

Encodes a protobuf field tag and appends the varint-encoded bytes to buffer.

A field tag combines the fieldNumber and wireType into a single varint: tagValue = (fieldNumber << 3) | wireType

Standard wire types:

  • 0: VARINT (int32, int64, uint32, uint64, sint32, sint64, bool, enum)
  • 1: I64 (fixed64, sfixed64, double)
  • 2: LEN (string, bytes, embedded messages, packed repeated fields)
  • 5: I32 (fixed32, sfixed32, float)

The varint encoding is inlined here (identical to encodeVarint from wire_varint.dart) to avoid cross-file imports, which simplifies Ball compilation.

Returns the same buffer list with the tag bytes appended.

Implementation

List<int> encodeTag(List<int> buffer, int fieldNumber, int wireType) {
  int tagValue = (fieldNumber << 3) | wireType;
  // Inline varint encoding (LEB128).
  do {
    int byte = tagValue & 0x7F;
    tagValue = tagValue >>> 7;
    if (tagValue != 0) {
      byte = byte | 0x80;
    }
    buffer.add(byte);
  } while (tagValue != 0);
  return buffer;
}