changeset 260 | 445e01aede7e |
parent 256 | 6d9efbef00a9 |
259:db4911b0c721 | 260:445e01aede7e |
---|---|
15 "google.golang.org/protobuf/internal/genid" |
15 "google.golang.org/protobuf/internal/genid" |
16 "google.golang.org/protobuf/internal/pragma" |
16 "google.golang.org/protobuf/internal/pragma" |
17 "google.golang.org/protobuf/internal/set" |
17 "google.golang.org/protobuf/internal/set" |
18 "google.golang.org/protobuf/internal/strs" |
18 "google.golang.org/protobuf/internal/strs" |
19 "google.golang.org/protobuf/proto" |
19 "google.golang.org/protobuf/proto" |
20 pref "google.golang.org/protobuf/reflect/protoreflect" |
20 "google.golang.org/protobuf/reflect/protoreflect" |
21 "google.golang.org/protobuf/reflect/protoregistry" |
21 "google.golang.org/protobuf/reflect/protoregistry" |
22 ) |
22 ) |
23 |
23 |
24 // Unmarshal reads the given []byte into the given proto.Message. |
24 // Unmarshal reads the given []byte into the given proto.Message. |
25 // The provided message must be mutable (e.g., a non-nil pointer to a message). |
25 // The provided message must be mutable (e.g., a non-nil pointer to a message). |
101 head := fmt.Sprintf("syntax error (line %d:%d): ", line, column) |
101 head := fmt.Sprintf("syntax error (line %d:%d): ", line, column) |
102 return errors.New(head+f, x...) |
102 return errors.New(head+f, x...) |
103 } |
103 } |
104 |
104 |
105 // unmarshalMessage unmarshals into the given protoreflect.Message. |
105 // unmarshalMessage unmarshals into the given protoreflect.Message. |
106 func (d decoder) unmarshalMessage(m pref.Message, checkDelims bool) error { |
106 func (d decoder) unmarshalMessage(m protoreflect.Message, checkDelims bool) error { |
107 messageDesc := m.Descriptor() |
107 messageDesc := m.Descriptor() |
108 if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { |
108 if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { |
109 return errors.New("no support for proto1 MessageSets") |
109 return errors.New("no support for proto1 MessageSets") |
110 } |
110 } |
111 |
111 |
148 } |
148 } |
149 return d.unexpectedTokenError(tok) |
149 return d.unexpectedTokenError(tok) |
150 } |
150 } |
151 |
151 |
152 // Resolve the field descriptor. |
152 // Resolve the field descriptor. |
153 var name pref.Name |
153 var name protoreflect.Name |
154 var fd pref.FieldDescriptor |
154 var fd protoreflect.FieldDescriptor |
155 var xt pref.ExtensionType |
155 var xt protoreflect.ExtensionType |
156 var xtErr error |
156 var xtErr error |
157 var isFieldNumberName bool |
157 var isFieldNumberName bool |
158 |
158 |
159 switch tok.NameKind() { |
159 switch tok.NameKind() { |
160 case text.IdentName: |
160 case text.IdentName: |
161 name = pref.Name(tok.IdentName()) |
161 name = protoreflect.Name(tok.IdentName()) |
162 fd = fieldDescs.ByTextName(string(name)) |
162 fd = fieldDescs.ByTextName(string(name)) |
163 |
163 |
164 case text.TypeName: |
164 case text.TypeName: |
165 // Handle extensions only. This code path is not for Any. |
165 // Handle extensions only. This code path is not for Any. |
166 xt, xtErr = d.opts.Resolver.FindExtensionByName(pref.FullName(tok.TypeName())) |
166 xt, xtErr = d.opts.Resolver.FindExtensionByName(protoreflect.FullName(tok.TypeName())) |
167 |
167 |
168 case text.FieldNumber: |
168 case text.FieldNumber: |
169 isFieldNumberName = true |
169 isFieldNumberName = true |
170 num := pref.FieldNumber(tok.FieldNumber()) |
170 num := protoreflect.FieldNumber(tok.FieldNumber()) |
171 if !num.IsValid() { |
171 if !num.IsValid() { |
172 return d.newError(tok.Pos(), "invalid field number: %d", num) |
172 return d.newError(tok.Pos(), "invalid field number: %d", num) |
173 } |
173 } |
174 fd = fieldDescs.ByNumber(num) |
174 fd = fieldDescs.ByNumber(num) |
175 if fd == nil { |
175 if fd == nil { |
213 } |
213 } |
214 |
214 |
215 switch { |
215 switch { |
216 case fd.IsList(): |
216 case fd.IsList(): |
217 kind := fd.Kind() |
217 kind := fd.Kind() |
218 if kind != pref.MessageKind && kind != pref.GroupKind && !tok.HasSeparator() { |
218 if kind != protoreflect.MessageKind && kind != protoreflect.GroupKind && !tok.HasSeparator() { |
219 return d.syntaxError(tok.Pos(), "missing field separator :") |
219 return d.syntaxError(tok.Pos(), "missing field separator :") |
220 } |
220 } |
221 |
221 |
222 list := m.Mutable(fd).List() |
222 list := m.Mutable(fd).List() |
223 if err := d.unmarshalList(fd, list); err != nil { |
223 if err := d.unmarshalList(fd, list); err != nil { |
230 return err |
230 return err |
231 } |
231 } |
232 |
232 |
233 default: |
233 default: |
234 kind := fd.Kind() |
234 kind := fd.Kind() |
235 if kind != pref.MessageKind && kind != pref.GroupKind && !tok.HasSeparator() { |
235 if kind != protoreflect.MessageKind && kind != protoreflect.GroupKind && !tok.HasSeparator() { |
236 return d.syntaxError(tok.Pos(), "missing field separator :") |
236 return d.syntaxError(tok.Pos(), "missing field separator :") |
237 } |
237 } |
238 |
238 |
239 // If field is a oneof, check if it has already been set. |
239 // If field is a oneof, check if it has already been set. |
240 if od := fd.ContainingOneof(); od != nil { |
240 if od := fd.ContainingOneof(); od != nil { |
260 return nil |
260 return nil |
261 } |
261 } |
262 |
262 |
263 // unmarshalSingular unmarshals a non-repeated field value specified by the |
263 // unmarshalSingular unmarshals a non-repeated field value specified by the |
264 // given FieldDescriptor. |
264 // given FieldDescriptor. |
265 func (d decoder) unmarshalSingular(fd pref.FieldDescriptor, m pref.Message) error { |
265 func (d decoder) unmarshalSingular(fd protoreflect.FieldDescriptor, m protoreflect.Message) error { |
266 var val pref.Value |
266 var val protoreflect.Value |
267 var err error |
267 var err error |
268 switch fd.Kind() { |
268 switch fd.Kind() { |
269 case pref.MessageKind, pref.GroupKind: |
269 case protoreflect.MessageKind, protoreflect.GroupKind: |
270 val = m.NewField(fd) |
270 val = m.NewField(fd) |
271 err = d.unmarshalMessage(val.Message(), true) |
271 err = d.unmarshalMessage(val.Message(), true) |
272 default: |
272 default: |
273 val, err = d.unmarshalScalar(fd) |
273 val, err = d.unmarshalScalar(fd) |
274 } |
274 } |
278 return err |
278 return err |
279 } |
279 } |
280 |
280 |
281 // unmarshalScalar unmarshals a scalar/enum protoreflect.Value specified by the |
281 // unmarshalScalar unmarshals a scalar/enum protoreflect.Value specified by the |
282 // given FieldDescriptor. |
282 // given FieldDescriptor. |
283 func (d decoder) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) { |
283 func (d decoder) unmarshalScalar(fd protoreflect.FieldDescriptor) (protoreflect.Value, error) { |
284 tok, err := d.Read() |
284 tok, err := d.Read() |
285 if err != nil { |
285 if err != nil { |
286 return pref.Value{}, err |
286 return protoreflect.Value{}, err |
287 } |
287 } |
288 |
288 |
289 if tok.Kind() != text.Scalar { |
289 if tok.Kind() != text.Scalar { |
290 return pref.Value{}, d.unexpectedTokenError(tok) |
290 return protoreflect.Value{}, d.unexpectedTokenError(tok) |
291 } |
291 } |
292 |
292 |
293 kind := fd.Kind() |
293 kind := fd.Kind() |
294 switch kind { |
294 switch kind { |
295 case pref.BoolKind: |
295 case protoreflect.BoolKind: |
296 if b, ok := tok.Bool(); ok { |
296 if b, ok := tok.Bool(); ok { |
297 return pref.ValueOfBool(b), nil |
297 return protoreflect.ValueOfBool(b), nil |
298 } |
298 } |
299 |
299 |
300 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
300 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
301 if n, ok := tok.Int32(); ok { |
301 if n, ok := tok.Int32(); ok { |
302 return pref.ValueOfInt32(n), nil |
302 return protoreflect.ValueOfInt32(n), nil |
303 } |
303 } |
304 |
304 |
305 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
305 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
306 if n, ok := tok.Int64(); ok { |
306 if n, ok := tok.Int64(); ok { |
307 return pref.ValueOfInt64(n), nil |
307 return protoreflect.ValueOfInt64(n), nil |
308 } |
308 } |
309 |
309 |
310 case pref.Uint32Kind, pref.Fixed32Kind: |
310 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
311 if n, ok := tok.Uint32(); ok { |
311 if n, ok := tok.Uint32(); ok { |
312 return pref.ValueOfUint32(n), nil |
312 return protoreflect.ValueOfUint32(n), nil |
313 } |
313 } |
314 |
314 |
315 case pref.Uint64Kind, pref.Fixed64Kind: |
315 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
316 if n, ok := tok.Uint64(); ok { |
316 if n, ok := tok.Uint64(); ok { |
317 return pref.ValueOfUint64(n), nil |
317 return protoreflect.ValueOfUint64(n), nil |
318 } |
318 } |
319 |
319 |
320 case pref.FloatKind: |
320 case protoreflect.FloatKind: |
321 if n, ok := tok.Float32(); ok { |
321 if n, ok := tok.Float32(); ok { |
322 return pref.ValueOfFloat32(n), nil |
322 return protoreflect.ValueOfFloat32(n), nil |
323 } |
323 } |
324 |
324 |
325 case pref.DoubleKind: |
325 case protoreflect.DoubleKind: |
326 if n, ok := tok.Float64(); ok { |
326 if n, ok := tok.Float64(); ok { |
327 return pref.ValueOfFloat64(n), nil |
327 return protoreflect.ValueOfFloat64(n), nil |
328 } |
328 } |
329 |
329 |
330 case pref.StringKind: |
330 case protoreflect.StringKind: |
331 if s, ok := tok.String(); ok { |
331 if s, ok := tok.String(); ok { |
332 if strs.EnforceUTF8(fd) && !utf8.ValidString(s) { |
332 if strs.EnforceUTF8(fd) && !utf8.ValidString(s) { |
333 return pref.Value{}, d.newError(tok.Pos(), "contains invalid UTF-8") |
333 return protoreflect.Value{}, d.newError(tok.Pos(), "contains invalid UTF-8") |
334 } |
334 } |
335 return pref.ValueOfString(s), nil |
335 return protoreflect.ValueOfString(s), nil |
336 } |
336 } |
337 |
337 |
338 case pref.BytesKind: |
338 case protoreflect.BytesKind: |
339 if b, ok := tok.String(); ok { |
339 if b, ok := tok.String(); ok { |
340 return pref.ValueOfBytes([]byte(b)), nil |
340 return protoreflect.ValueOfBytes([]byte(b)), nil |
341 } |
341 } |
342 |
342 |
343 case pref.EnumKind: |
343 case protoreflect.EnumKind: |
344 if lit, ok := tok.Enum(); ok { |
344 if lit, ok := tok.Enum(); ok { |
345 // Lookup EnumNumber based on name. |
345 // Lookup EnumNumber based on name. |
346 if enumVal := fd.Enum().Values().ByName(pref.Name(lit)); enumVal != nil { |
346 if enumVal := fd.Enum().Values().ByName(protoreflect.Name(lit)); enumVal != nil { |
347 return pref.ValueOfEnum(enumVal.Number()), nil |
347 return protoreflect.ValueOfEnum(enumVal.Number()), nil |
348 } |
348 } |
349 } |
349 } |
350 if num, ok := tok.Int32(); ok { |
350 if num, ok := tok.Int32(); ok { |
351 return pref.ValueOfEnum(pref.EnumNumber(num)), nil |
351 return protoreflect.ValueOfEnum(protoreflect.EnumNumber(num)), nil |
352 } |
352 } |
353 |
353 |
354 default: |
354 default: |
355 panic(fmt.Sprintf("invalid scalar kind %v", kind)) |
355 panic(fmt.Sprintf("invalid scalar kind %v", kind)) |
356 } |
356 } |
357 |
357 |
358 return pref.Value{}, d.newError(tok.Pos(), "invalid value for %v type: %v", kind, tok.RawString()) |
358 return protoreflect.Value{}, d.newError(tok.Pos(), "invalid value for %v type: %v", kind, tok.RawString()) |
359 } |
359 } |
360 |
360 |
361 // unmarshalList unmarshals into given protoreflect.List. A list value can |
361 // unmarshalList unmarshals into given protoreflect.List. A list value can |
362 // either be in [] syntax or simply just a single scalar/message value. |
362 // either be in [] syntax or simply just a single scalar/message value. |
363 func (d decoder) unmarshalList(fd pref.FieldDescriptor, list pref.List) error { |
363 func (d decoder) unmarshalList(fd protoreflect.FieldDescriptor, list protoreflect.List) error { |
364 tok, err := d.Peek() |
364 tok, err := d.Peek() |
365 if err != nil { |
365 if err != nil { |
366 return err |
366 return err |
367 } |
367 } |
368 |
368 |
369 switch fd.Kind() { |
369 switch fd.Kind() { |
370 case pref.MessageKind, pref.GroupKind: |
370 case protoreflect.MessageKind, protoreflect.GroupKind: |
371 switch tok.Kind() { |
371 switch tok.Kind() { |
372 case text.ListOpen: |
372 case text.ListOpen: |
373 d.Read() |
373 d.Read() |
374 for { |
374 for { |
375 tok, err := d.Peek() |
375 tok, err := d.Peek() |
439 return d.unexpectedTokenError(tok) |
439 return d.unexpectedTokenError(tok) |
440 } |
440 } |
441 |
441 |
442 // unmarshalMap unmarshals into given protoreflect.Map. A map value is a |
442 // unmarshalMap unmarshals into given protoreflect.Map. A map value is a |
443 // textproto message containing {key: <kvalue>, value: <mvalue>}. |
443 // textproto message containing {key: <kvalue>, value: <mvalue>}. |
444 func (d decoder) unmarshalMap(fd pref.FieldDescriptor, mmap pref.Map) error { |
444 func (d decoder) unmarshalMap(fd protoreflect.FieldDescriptor, mmap protoreflect.Map) error { |
445 // Determine ahead whether map entry is a scalar type or a message type in |
445 // Determine ahead whether map entry is a scalar type or a message type in |
446 // order to call the appropriate unmarshalMapValue func inside |
446 // order to call the appropriate unmarshalMapValue func inside |
447 // unmarshalMapEntry. |
447 // unmarshalMapEntry. |
448 var unmarshalMapValue func() (pref.Value, error) |
448 var unmarshalMapValue func() (protoreflect.Value, error) |
449 switch fd.MapValue().Kind() { |
449 switch fd.MapValue().Kind() { |
450 case pref.MessageKind, pref.GroupKind: |
450 case protoreflect.MessageKind, protoreflect.GroupKind: |
451 unmarshalMapValue = func() (pref.Value, error) { |
451 unmarshalMapValue = func() (protoreflect.Value, error) { |
452 pval := mmap.NewValue() |
452 pval := mmap.NewValue() |
453 if err := d.unmarshalMessage(pval.Message(), true); err != nil { |
453 if err := d.unmarshalMessage(pval.Message(), true); err != nil { |
454 return pref.Value{}, err |
454 return protoreflect.Value{}, err |
455 } |
455 } |
456 return pval, nil |
456 return pval, nil |
457 } |
457 } |
458 default: |
458 default: |
459 unmarshalMapValue = func() (pref.Value, error) { |
459 unmarshalMapValue = func() (protoreflect.Value, error) { |
460 return d.unmarshalScalar(fd.MapValue()) |
460 return d.unmarshalScalar(fd.MapValue()) |
461 } |
461 } |
462 } |
462 } |
463 |
463 |
464 tok, err := d.Read() |
464 tok, err := d.Read() |
492 } |
492 } |
493 } |
493 } |
494 |
494 |
495 // unmarshalMap unmarshals into given protoreflect.Map. A map value is a |
495 // unmarshalMap unmarshals into given protoreflect.Map. A map value is a |
496 // textproto message containing {key: <kvalue>, value: <mvalue>}. |
496 // textproto message containing {key: <kvalue>, value: <mvalue>}. |
497 func (d decoder) unmarshalMapEntry(fd pref.FieldDescriptor, mmap pref.Map, unmarshalMapValue func() (pref.Value, error)) error { |
497 func (d decoder) unmarshalMapEntry(fd protoreflect.FieldDescriptor, mmap protoreflect.Map, unmarshalMapValue func() (protoreflect.Value, error)) error { |
498 var key pref.MapKey |
498 var key protoreflect.MapKey |
499 var pval pref.Value |
499 var pval protoreflect.Value |
500 Loop: |
500 Loop: |
501 for { |
501 for { |
502 // Read field name. |
502 // Read field name. |
503 tok, err := d.Read() |
503 tok, err := d.Read() |
504 if err != nil { |
504 if err != nil { |
518 break Loop |
518 break Loop |
519 default: |
519 default: |
520 return d.unexpectedTokenError(tok) |
520 return d.unexpectedTokenError(tok) |
521 } |
521 } |
522 |
522 |
523 switch name := pref.Name(tok.IdentName()); name { |
523 switch name := protoreflect.Name(tok.IdentName()); name { |
524 case genid.MapEntry_Key_field_name: |
524 case genid.MapEntry_Key_field_name: |
525 if !tok.HasSeparator() { |
525 if !tok.HasSeparator() { |
526 return d.syntaxError(tok.Pos(), "missing field separator :") |
526 return d.syntaxError(tok.Pos(), "missing field separator :") |
527 } |
527 } |
528 if key.IsValid() { |
528 if key.IsValid() { |
533 return err |
533 return err |
534 } |
534 } |
535 key = val.MapKey() |
535 key = val.MapKey() |
536 |
536 |
537 case genid.MapEntry_Value_field_name: |
537 case genid.MapEntry_Value_field_name: |
538 if kind := fd.MapValue().Kind(); (kind != pref.MessageKind) && (kind != pref.GroupKind) { |
538 if kind := fd.MapValue().Kind(); (kind != protoreflect.MessageKind) && (kind != protoreflect.GroupKind) { |
539 if !tok.HasSeparator() { |
539 if !tok.HasSeparator() { |
540 return d.syntaxError(tok.Pos(), "missing field separator :") |
540 return d.syntaxError(tok.Pos(), "missing field separator :") |
541 } |
541 } |
542 } |
542 } |
543 if pval.IsValid() { |
543 if pval.IsValid() { |
559 if !key.IsValid() { |
559 if !key.IsValid() { |
560 key = fd.MapKey().Default().MapKey() |
560 key = fd.MapKey().Default().MapKey() |
561 } |
561 } |
562 if !pval.IsValid() { |
562 if !pval.IsValid() { |
563 switch fd.MapValue().Kind() { |
563 switch fd.MapValue().Kind() { |
564 case pref.MessageKind, pref.GroupKind: |
564 case protoreflect.MessageKind, protoreflect.GroupKind: |
565 // If value field is not set for message/group types, construct an |
565 // If value field is not set for message/group types, construct an |
566 // empty one as default. |
566 // empty one as default. |
567 pval = mmap.NewValue() |
567 pval = mmap.NewValue() |
568 default: |
568 default: |
569 pval = fd.MapValue().Default() |
569 pval = fd.MapValue().Default() |
573 return nil |
573 return nil |
574 } |
574 } |
575 |
575 |
576 // unmarshalAny unmarshals an Any textproto. It can either be in expanded form |
576 // unmarshalAny unmarshals an Any textproto. It can either be in expanded form |
577 // or non-expanded form. |
577 // or non-expanded form. |
578 func (d decoder) unmarshalAny(m pref.Message, checkDelims bool) error { |
578 func (d decoder) unmarshalAny(m protoreflect.Message, checkDelims bool) error { |
579 var typeURL string |
579 var typeURL string |
580 var bValue []byte |
580 var bValue []byte |
581 var seenTypeUrl bool |
581 var seenTypeUrl bool |
582 var seenValue bool |
582 var seenValue bool |
583 var isExpanded bool |
583 var isExpanded bool |
617 // Both type_url and value fields require field separator :. |
617 // Both type_url and value fields require field separator :. |
618 if !tok.HasSeparator() { |
618 if !tok.HasSeparator() { |
619 return d.syntaxError(tok.Pos(), "missing field separator :") |
619 return d.syntaxError(tok.Pos(), "missing field separator :") |
620 } |
620 } |
621 |
621 |
622 switch name := pref.Name(tok.IdentName()); name { |
622 switch name := protoreflect.Name(tok.IdentName()); name { |
623 case genid.Any_TypeUrl_field_name: |
623 case genid.Any_TypeUrl_field_name: |
624 if seenTypeUrl { |
624 if seenTypeUrl { |
625 return d.newError(tok.Pos(), "duplicate %v field", genid.Any_TypeUrl_field_fullname) |
625 return d.newError(tok.Pos(), "duplicate %v field", genid.Any_TypeUrl_field_fullname) |
626 } |
626 } |
627 if isExpanded { |
627 if isExpanded { |
684 } |
684 } |
685 } |
685 } |
686 |
686 |
687 fds := m.Descriptor().Fields() |
687 fds := m.Descriptor().Fields() |
688 if len(typeURL) > 0 { |
688 if len(typeURL) > 0 { |
689 m.Set(fds.ByNumber(genid.Any_TypeUrl_field_number), pref.ValueOfString(typeURL)) |
689 m.Set(fds.ByNumber(genid.Any_TypeUrl_field_number), protoreflect.ValueOfString(typeURL)) |
690 } |
690 } |
691 if len(bValue) > 0 { |
691 if len(bValue) > 0 { |
692 m.Set(fds.ByNumber(genid.Any_Value_field_number), pref.ValueOfBytes(bValue)) |
692 m.Set(fds.ByNumber(genid.Any_Value_field_number), protoreflect.ValueOfBytes(bValue)) |
693 } |
693 } |
694 return nil |
694 return nil |
695 } |
695 } |
696 |
696 |
697 func (d decoder) unmarshalExpandedAny(typeURL string, pos int) ([]byte, error) { |
697 func (d decoder) unmarshalExpandedAny(typeURL string, pos int) ([]byte, error) { |