encodeMessageField function

List<int> encodeMessageField(
  1. List<int> buffer,
  2. int fieldNumber,
  3. List<int> messageBytes
)

Encode an embedded message field (wire type 2): tag + varint(msg_len) + msg_bytes.

The messageBytes must be pre-encoded by the caller (i.e. the caller marshals the submessage into bytes first, then passes them here).

Proto3 rule: does not emit anything if messageBytes is empty.

Returns buffer with the field bytes appended.

Implementation

List<int> encodeMessageField(
  List<int> buffer,
  int fieldNumber,
  List<int> messageBytes,
) {
  if (messageBytes.isEmpty) return buffer;
  encodeTag(buffer, fieldNumber, _wireTypeLEN);
  encodeBytes(buffer, messageBytes);
  return buffer;
}