MCPcopy Create free account
hub / github.com/segmentio/encoding / Parse

Function Parse

proto/message.go:190–234  ·  view source on GitHub ↗
(m []byte)

Source from the content-addressed store, hash-verified

188}
189
190func Parse(m []byte) (FieldNumber, WireType, RawValue, RawMessage, error) {
191 tag, n, err := decodeVarint(m)
192 if err != nil {
193 return 0, 0, nil, m, fmt.Errorf("decoding protobuf field number: %w", err)
194 }
195 m = m[n:]
196 f, t := DecodeTag(tag)
197
198 switch t {
199 case Varint:
200 _, n, err := decodeVarint(m)
201 if err != nil {
202 return f, t, nil, m, fmt.Errorf("decoding varint field %d: %w", f, err)
203 }
204 if len(m) < n {
205 return f, t, nil, m, fmt.Errorf("decoding varint field %d: %w", f, io.ErrUnexpectedEOF)
206 }
207 return f, t, RawValue(m[:n]), m[n:], nil
208
209 case Varlen:
210 l, n, err := decodeVarint(m) // length
211 if err != nil {
212 return f, t, nil, m, fmt.Errorf("decoding varlen field %d: %w", f, err)
213 }
214 if uint64(len(m)-n) < l {
215 return f, t, nil, m, fmt.Errorf("decoding varlen field %d: %w", f, io.ErrUnexpectedEOF)
216 }
217 return f, t, RawValue(m[n : n+int(l)]), m[n+int(l):], nil
218
219 case Fixed32:
220 if len(m) < 4 {
221 return f, t, nil, m, fmt.Errorf("decoding fixed32 field %d: %w", f, io.ErrUnexpectedEOF)
222 }
223 return f, t, RawValue(m[:4]), m[4:], nil
224
225 case Fixed64:
226 if len(m) < 8 {
227 return f, t, nil, m, fmt.Errorf("decoding fixed64 field %d: %w", f, io.ErrUnexpectedEOF)
228 }
229 return f, t, RawValue(m[:8]), m[8:], nil
230
231 default:
232 return f, t, nil, m, fmt.Errorf("invalid wire type: %d", t)
233 }
234}
235
236// Scan calls fn for each protobuf field in the message b.
237//

Callers 7

TestAppendVarintFunction · 0.70
TestAppendVarlenFunction · 0.70
TestAppendFixed32Function · 0.70
TestAppendFixed64Function · 0.70
assertParseFunction · 0.70
ScanFunction · 0.70
RewriteMethod · 0.70

Calls 3

decodeVarintFunction · 0.85
DecodeTagFunction · 0.85
RawValueTypeAlias · 0.70

Tested by 5

TestAppendVarintFunction · 0.56
TestAppendVarlenFunction · 0.56
TestAppendFixed32Function · 0.56
TestAppendFixed64Function · 0.56
assertParseFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…