The same field number can appear many times in a message, which is how repeated fields are stored. Today you group the flat field list by number so each field maps to the list of values seen for it.
Group the parsed field list into a map from field number to the list of its values.
Protobuf has no dedicated “array” on the wire. A repeated field is simply the same field number written more than once, each occurrence a full tag-and-value. Grouping the flat field list into a map from number to a list of occurrences turns that wire reality into something usable: a field that appears three times has three entries, a field that appears once has one, and a field that never appears is missing entirely.
Preserving order within each number’s list matters. For a genuinely repeated field the order is the array order the sender intended. For a non-repeated (scalar) field that mistakenly appears twice, protobuf says the last one wins - and you can only honor that if you kept them in order. This grouping is deliberately dumb: it does not yet know which fields are meant to be repeated. That judgment needs a schema, which arrives in the final chapter; here you just organize the raw truth.
// append each field's value under its number, preserving orderfunc GroupByNumber(fields []Field) map[int][]Field {out := map[int][]Field{}for _, f := range fields {out[f.Number] = append(out[f.Number], f)}return out}