10 "sync" |
10 "sync" |
11 |
11 |
12 "google.golang.org/protobuf/encoding/protowire" |
12 "google.golang.org/protobuf/encoding/protowire" |
13 "google.golang.org/protobuf/internal/errors" |
13 "google.golang.org/protobuf/internal/errors" |
14 "google.golang.org/protobuf/proto" |
14 "google.golang.org/protobuf/proto" |
15 pref "google.golang.org/protobuf/reflect/protoreflect" |
15 "google.golang.org/protobuf/reflect/protoreflect" |
16 preg "google.golang.org/protobuf/reflect/protoregistry" |
16 "google.golang.org/protobuf/reflect/protoregistry" |
17 piface "google.golang.org/protobuf/runtime/protoiface" |
17 "google.golang.org/protobuf/runtime/protoiface" |
18 ) |
18 ) |
19 |
19 |
20 type errInvalidUTF8 struct{} |
20 type errInvalidUTF8 struct{} |
21 |
21 |
22 func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" } |
22 func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" } |
28 // For size, marshal, and isInit operations, functions are set only on the first field |
28 // For size, marshal, and isInit operations, functions are set only on the first field |
29 // in the oneof. The functions are called when the oneof is non-nil, and will dispatch |
29 // in the oneof. The functions are called when the oneof is non-nil, and will dispatch |
30 // to the appropriate field-specific function as necessary. |
30 // to the appropriate field-specific function as necessary. |
31 // |
31 // |
32 // The unmarshal function is set on each field individually as usual. |
32 // The unmarshal function is set on each field individually as usual. |
33 func (mi *MessageInfo) initOneofFieldCoders(od pref.OneofDescriptor, si structInfo) { |
33 func (mi *MessageInfo) initOneofFieldCoders(od protoreflect.OneofDescriptor, si structInfo) { |
34 fs := si.oneofsByName[od.Name()] |
34 fs := si.oneofsByName[od.Name()] |
35 ft := fs.Type |
35 ft := fs.Type |
36 oneofFields := make(map[reflect.Type]*coderFieldInfo) |
36 oneofFields := make(map[reflect.Type]*coderFieldInfo) |
37 needIsInit := false |
37 needIsInit := false |
38 fields := od.Fields() |
38 fields := od.Fields() |
116 return info.funcs.isInit(p, info) |
116 return info.funcs.isInit(p, info) |
117 } |
117 } |
118 } |
118 } |
119 } |
119 } |
120 |
120 |
121 func makeWeakMessageFieldCoder(fd pref.FieldDescriptor) pointerCoderFuncs { |
121 func makeWeakMessageFieldCoder(fd protoreflect.FieldDescriptor) pointerCoderFuncs { |
122 var once sync.Once |
122 var once sync.Once |
123 var messageType pref.MessageType |
123 var messageType protoreflect.MessageType |
124 lazyInit := func() { |
124 lazyInit := func() { |
125 once.Do(func() { |
125 once.Do(func() { |
126 messageName := fd.Message().FullName() |
126 messageName := fd.Message().FullName() |
127 messageType, _ = preg.GlobalTypes.FindMessageByName(messageName) |
127 messageType, _ = protoregistry.GlobalTypes.FindMessageByName(messageName) |
128 }) |
128 }) |
129 } |
129 } |
130 |
130 |
131 return pointerCoderFuncs{ |
131 return pointerCoderFuncs{ |
132 size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int { |
132 size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int { |
188 opts.Merge(dm, sm) |
188 opts.Merge(dm, sm) |
189 }, |
189 }, |
190 } |
190 } |
191 } |
191 } |
192 |
192 |
193 func makeMessageFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
193 func makeMessageFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
194 if mi := getMessageInfo(ft); mi != nil { |
194 if mi := getMessageInfo(ft); mi != nil { |
195 funcs := pointerCoderFuncs{ |
195 funcs := pointerCoderFuncs{ |
196 size: sizeMessageInfo, |
196 size: sizeMessageInfo, |
197 marshal: appendMessageInfo, |
197 marshal: appendMessageInfo, |
198 unmarshal: consumeMessageInfo, |
198 unmarshal: consumeMessageInfo, |
278 } |
278 } |
279 v, n := protowire.ConsumeBytes(b) |
279 v, n := protowire.ConsumeBytes(b) |
280 if n < 0 { |
280 if n < 0 { |
281 return out, errDecode |
281 return out, errDecode |
282 } |
282 } |
283 o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ |
283 o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{ |
284 Buf: v, |
284 Buf: v, |
285 Message: m.ProtoReflect(), |
285 Message: m.ProtoReflect(), |
286 }) |
286 }) |
287 if err != nil { |
287 if err != nil { |
288 return out, err |
288 return out, err |
289 } |
289 } |
290 out.n = n |
290 out.n = n |
291 out.initialized = o.Flags&piface.UnmarshalInitialized != 0 |
291 out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0 |
292 return out, nil |
292 return out, nil |
293 } |
293 } |
294 |
294 |
295 func sizeMessageValue(v pref.Value, tagsize int, opts marshalOptions) int { |
295 func sizeMessageValue(v protoreflect.Value, tagsize int, opts marshalOptions) int { |
296 m := v.Message().Interface() |
296 m := v.Message().Interface() |
297 return sizeMessage(m, tagsize, opts) |
297 return sizeMessage(m, tagsize, opts) |
298 } |
298 } |
299 |
299 |
300 func appendMessageValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
300 func appendMessageValue(b []byte, v protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
301 m := v.Message().Interface() |
301 m := v.Message().Interface() |
302 return appendMessage(b, m, wiretag, opts) |
302 return appendMessage(b, m, wiretag, opts) |
303 } |
303 } |
304 |
304 |
305 func consumeMessageValue(b []byte, v pref.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (pref.Value, unmarshalOutput, error) { |
305 func consumeMessageValue(b []byte, v protoreflect.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (protoreflect.Value, unmarshalOutput, error) { |
306 m := v.Message().Interface() |
306 m := v.Message().Interface() |
307 out, err := consumeMessage(b, m, wtyp, opts) |
307 out, err := consumeMessage(b, m, wtyp, opts) |
308 return v, out, err |
308 return v, out, err |
309 } |
309 } |
310 |
310 |
311 func isInitMessageValue(v pref.Value) error { |
311 func isInitMessageValue(v protoreflect.Value) error { |
312 m := v.Message().Interface() |
312 m := v.Message().Interface() |
313 return proto.CheckInitialized(m) |
313 return proto.CheckInitialized(m) |
314 } |
314 } |
315 |
315 |
316 var coderMessageValue = valueCoderFuncs{ |
316 var coderMessageValue = valueCoderFuncs{ |
319 unmarshal: consumeMessageValue, |
319 unmarshal: consumeMessageValue, |
320 isInit: isInitMessageValue, |
320 isInit: isInitMessageValue, |
321 merge: mergeMessageValue, |
321 merge: mergeMessageValue, |
322 } |
322 } |
323 |
323 |
324 func sizeGroupValue(v pref.Value, tagsize int, opts marshalOptions) int { |
324 func sizeGroupValue(v protoreflect.Value, tagsize int, opts marshalOptions) int { |
325 m := v.Message().Interface() |
325 m := v.Message().Interface() |
326 return sizeGroup(m, tagsize, opts) |
326 return sizeGroup(m, tagsize, opts) |
327 } |
327 } |
328 |
328 |
329 func appendGroupValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
329 func appendGroupValue(b []byte, v protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
330 m := v.Message().Interface() |
330 m := v.Message().Interface() |
331 return appendGroup(b, m, wiretag, opts) |
331 return appendGroup(b, m, wiretag, opts) |
332 } |
332 } |
333 |
333 |
334 func consumeGroupValue(b []byte, v pref.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (pref.Value, unmarshalOutput, error) { |
334 func consumeGroupValue(b []byte, v protoreflect.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (protoreflect.Value, unmarshalOutput, error) { |
335 m := v.Message().Interface() |
335 m := v.Message().Interface() |
336 out, err := consumeGroup(b, m, num, wtyp, opts) |
336 out, err := consumeGroup(b, m, num, wtyp, opts) |
337 return v, out, err |
337 return v, out, err |
338 } |
338 } |
339 |
339 |
343 unmarshal: consumeGroupValue, |
343 unmarshal: consumeGroupValue, |
344 isInit: isInitMessageValue, |
344 isInit: isInitMessageValue, |
345 merge: mergeMessageValue, |
345 merge: mergeMessageValue, |
346 } |
346 } |
347 |
347 |
348 func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
348 func makeGroupFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
349 num := fd.Number() |
349 num := fd.Number() |
350 if mi := getMessageInfo(ft); mi != nil { |
350 if mi := getMessageInfo(ft); mi != nil { |
351 funcs := pointerCoderFuncs{ |
351 funcs := pointerCoderFuncs{ |
352 size: sizeGroupType, |
352 size: sizeGroupType, |
353 marshal: appendGroupType, |
353 marshal: appendGroupType, |
422 } |
422 } |
423 b, n := protowire.ConsumeGroup(num, b) |
423 b, n := protowire.ConsumeGroup(num, b) |
424 if n < 0 { |
424 if n < 0 { |
425 return out, errDecode |
425 return out, errDecode |
426 } |
426 } |
427 o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ |
427 o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{ |
428 Buf: b, |
428 Buf: b, |
429 Message: m.ProtoReflect(), |
429 Message: m.ProtoReflect(), |
430 }) |
430 }) |
431 if err != nil { |
431 if err != nil { |
432 return out, err |
432 return out, err |
433 } |
433 } |
434 out.n = n |
434 out.n = n |
435 out.initialized = o.Flags&piface.UnmarshalInitialized != 0 |
435 out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0 |
436 return out, nil |
436 return out, nil |
437 } |
437 } |
438 |
438 |
439 func makeMessageSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
439 func makeMessageSliceFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
440 if mi := getMessageInfo(ft); mi != nil { |
440 if mi := getMessageInfo(ft); mi != nil { |
441 funcs := pointerCoderFuncs{ |
441 funcs := pointerCoderFuncs{ |
442 size: sizeMessageSliceInfo, |
442 size: sizeMessageSliceInfo, |
443 marshal: appendMessageSliceInfo, |
443 marshal: appendMessageSliceInfo, |
444 unmarshal: consumeMessageSliceInfo, |
444 unmarshal: consumeMessageSliceInfo, |
553 v, n := protowire.ConsumeBytes(b) |
553 v, n := protowire.ConsumeBytes(b) |
554 if n < 0 { |
554 if n < 0 { |
555 return out, errDecode |
555 return out, errDecode |
556 } |
556 } |
557 mp := reflect.New(goType.Elem()) |
557 mp := reflect.New(goType.Elem()) |
558 o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ |
558 o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{ |
559 Buf: v, |
559 Buf: v, |
560 Message: asMessage(mp).ProtoReflect(), |
560 Message: asMessage(mp).ProtoReflect(), |
561 }) |
561 }) |
562 if err != nil { |
562 if err != nil { |
563 return out, err |
563 return out, err |
564 } |
564 } |
565 p.AppendPointerSlice(pointerOfValue(mp)) |
565 p.AppendPointerSlice(pointerOfValue(mp)) |
566 out.n = n |
566 out.n = n |
567 out.initialized = o.Flags&piface.UnmarshalInitialized != 0 |
567 out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0 |
568 return out, nil |
568 return out, nil |
569 } |
569 } |
570 |
570 |
571 func isInitMessageSlice(p pointer, goType reflect.Type) error { |
571 func isInitMessageSlice(p pointer, goType reflect.Type) error { |
572 s := p.PointerSlice() |
572 s := p.PointerSlice() |
579 return nil |
579 return nil |
580 } |
580 } |
581 |
581 |
582 // Slices of messages |
582 // Slices of messages |
583 |
583 |
584 func sizeMessageSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int { |
584 func sizeMessageSliceValue(listv protoreflect.Value, tagsize int, opts marshalOptions) int { |
585 list := listv.List() |
585 list := listv.List() |
586 n := 0 |
586 n := 0 |
587 for i, llen := 0, list.Len(); i < llen; i++ { |
587 for i, llen := 0, list.Len(); i < llen; i++ { |
588 m := list.Get(i).Message().Interface() |
588 m := list.Get(i).Message().Interface() |
589 n += protowire.SizeBytes(proto.Size(m)) + tagsize |
589 n += protowire.SizeBytes(proto.Size(m)) + tagsize |
590 } |
590 } |
591 return n |
591 return n |
592 } |
592 } |
593 |
593 |
594 func appendMessageSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
594 func appendMessageSliceValue(b []byte, listv protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
595 list := listv.List() |
595 list := listv.List() |
596 mopts := opts.Options() |
596 mopts := opts.Options() |
597 for i, llen := 0, list.Len(); i < llen; i++ { |
597 for i, llen := 0, list.Len(); i < llen; i++ { |
598 m := list.Get(i).Message().Interface() |
598 m := list.Get(i).Message().Interface() |
599 b = protowire.AppendVarint(b, wiretag) |
599 b = protowire.AppendVarint(b, wiretag) |
606 } |
606 } |
607 } |
607 } |
608 return b, nil |
608 return b, nil |
609 } |
609 } |
610 |
610 |
611 func consumeMessageSliceValue(b []byte, listv pref.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ pref.Value, out unmarshalOutput, err error) { |
611 func consumeMessageSliceValue(b []byte, listv protoreflect.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ protoreflect.Value, out unmarshalOutput, err error) { |
612 list := listv.List() |
612 list := listv.List() |
613 if wtyp != protowire.BytesType { |
613 if wtyp != protowire.BytesType { |
614 return pref.Value{}, out, errUnknown |
614 return protoreflect.Value{}, out, errUnknown |
615 } |
615 } |
616 v, n := protowire.ConsumeBytes(b) |
616 v, n := protowire.ConsumeBytes(b) |
617 if n < 0 { |
617 if n < 0 { |
618 return pref.Value{}, out, errDecode |
618 return protoreflect.Value{}, out, errDecode |
619 } |
619 } |
620 m := list.NewElement() |
620 m := list.NewElement() |
621 o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ |
621 o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{ |
622 Buf: v, |
622 Buf: v, |
623 Message: m.Message(), |
623 Message: m.Message(), |
624 }) |
624 }) |
625 if err != nil { |
625 if err != nil { |
626 return pref.Value{}, out, err |
626 return protoreflect.Value{}, out, err |
627 } |
627 } |
628 list.Append(m) |
628 list.Append(m) |
629 out.n = n |
629 out.n = n |
630 out.initialized = o.Flags&piface.UnmarshalInitialized != 0 |
630 out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0 |
631 return listv, out, nil |
631 return listv, out, nil |
632 } |
632 } |
633 |
633 |
634 func isInitMessageSliceValue(listv pref.Value) error { |
634 func isInitMessageSliceValue(listv protoreflect.Value) error { |
635 list := listv.List() |
635 list := listv.List() |
636 for i, llen := 0, list.Len(); i < llen; i++ { |
636 for i, llen := 0, list.Len(); i < llen; i++ { |
637 m := list.Get(i).Message().Interface() |
637 m := list.Get(i).Message().Interface() |
638 if err := proto.CheckInitialized(m); err != nil { |
638 if err := proto.CheckInitialized(m); err != nil { |
639 return err |
639 return err |
648 unmarshal: consumeMessageSliceValue, |
648 unmarshal: consumeMessageSliceValue, |
649 isInit: isInitMessageSliceValue, |
649 isInit: isInitMessageSliceValue, |
650 merge: mergeMessageListValue, |
650 merge: mergeMessageListValue, |
651 } |
651 } |
652 |
652 |
653 func sizeGroupSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int { |
653 func sizeGroupSliceValue(listv protoreflect.Value, tagsize int, opts marshalOptions) int { |
654 list := listv.List() |
654 list := listv.List() |
655 n := 0 |
655 n := 0 |
656 for i, llen := 0, list.Len(); i < llen; i++ { |
656 for i, llen := 0, list.Len(); i < llen; i++ { |
657 m := list.Get(i).Message().Interface() |
657 m := list.Get(i).Message().Interface() |
658 n += 2*tagsize + proto.Size(m) |
658 n += 2*tagsize + proto.Size(m) |
659 } |
659 } |
660 return n |
660 return n |
661 } |
661 } |
662 |
662 |
663 func appendGroupSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
663 func appendGroupSliceValue(b []byte, listv protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
664 list := listv.List() |
664 list := listv.List() |
665 mopts := opts.Options() |
665 mopts := opts.Options() |
666 for i, llen := 0, list.Len(); i < llen; i++ { |
666 for i, llen := 0, list.Len(); i < llen; i++ { |
667 m := list.Get(i).Message().Interface() |
667 m := list.Get(i).Message().Interface() |
668 b = protowire.AppendVarint(b, wiretag) // start group |
668 b = protowire.AppendVarint(b, wiretag) // start group |
674 b = protowire.AppendVarint(b, wiretag+1) // end group |
674 b = protowire.AppendVarint(b, wiretag+1) // end group |
675 } |
675 } |
676 return b, nil |
676 return b, nil |
677 } |
677 } |
678 |
678 |
679 func consumeGroupSliceValue(b []byte, listv pref.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ pref.Value, out unmarshalOutput, err error) { |
679 func consumeGroupSliceValue(b []byte, listv protoreflect.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ protoreflect.Value, out unmarshalOutput, err error) { |
680 list := listv.List() |
680 list := listv.List() |
681 if wtyp != protowire.StartGroupType { |
681 if wtyp != protowire.StartGroupType { |
682 return pref.Value{}, out, errUnknown |
682 return protoreflect.Value{}, out, errUnknown |
683 } |
683 } |
684 b, n := protowire.ConsumeGroup(num, b) |
684 b, n := protowire.ConsumeGroup(num, b) |
685 if n < 0 { |
685 if n < 0 { |
686 return pref.Value{}, out, errDecode |
686 return protoreflect.Value{}, out, errDecode |
687 } |
687 } |
688 m := list.NewElement() |
688 m := list.NewElement() |
689 o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ |
689 o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{ |
690 Buf: b, |
690 Buf: b, |
691 Message: m.Message(), |
691 Message: m.Message(), |
692 }) |
692 }) |
693 if err != nil { |
693 if err != nil { |
694 return pref.Value{}, out, err |
694 return protoreflect.Value{}, out, err |
695 } |
695 } |
696 list.Append(m) |
696 list.Append(m) |
697 out.n = n |
697 out.n = n |
698 out.initialized = o.Flags&piface.UnmarshalInitialized != 0 |
698 out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0 |
699 return listv, out, nil |
699 return listv, out, nil |
700 } |
700 } |
701 |
701 |
702 var coderGroupSliceValue = valueCoderFuncs{ |
702 var coderGroupSliceValue = valueCoderFuncs{ |
703 size: sizeGroupSliceValue, |
703 size: sizeGroupSliceValue, |
705 unmarshal: consumeGroupSliceValue, |
705 unmarshal: consumeGroupSliceValue, |
706 isInit: isInitMessageSliceValue, |
706 isInit: isInitMessageSliceValue, |
707 merge: mergeMessageListValue, |
707 merge: mergeMessageListValue, |
708 } |
708 } |
709 |
709 |
710 func makeGroupSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
710 func makeGroupSliceFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
711 num := fd.Number() |
711 num := fd.Number() |
712 if mi := getMessageInfo(ft); mi != nil { |
712 if mi := getMessageInfo(ft); mi != nil { |
713 funcs := pointerCoderFuncs{ |
713 funcs := pointerCoderFuncs{ |
714 size: sizeGroupSliceInfo, |
714 size: sizeGroupSliceInfo, |
715 marshal: appendGroupSliceInfo, |
715 marshal: appendGroupSliceInfo, |
770 b, n := protowire.ConsumeGroup(num, b) |
770 b, n := protowire.ConsumeGroup(num, b) |
771 if n < 0 { |
771 if n < 0 { |
772 return out, errDecode |
772 return out, errDecode |
773 } |
773 } |
774 mp := reflect.New(goType.Elem()) |
774 mp := reflect.New(goType.Elem()) |
775 o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{ |
775 o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{ |
776 Buf: b, |
776 Buf: b, |
777 Message: asMessage(mp).ProtoReflect(), |
777 Message: asMessage(mp).ProtoReflect(), |
778 }) |
778 }) |
779 if err != nil { |
779 if err != nil { |
780 return out, err |
780 return out, err |
781 } |
781 } |
782 p.AppendPointerSlice(pointerOfValue(mp)) |
782 p.AppendPointerSlice(pointerOfValue(mp)) |
783 out.n = n |
783 out.n = n |
784 out.initialized = o.Flags&piface.UnmarshalInitialized != 0 |
784 out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0 |
785 return out, nil |
785 return out, nil |
786 } |
786 } |
787 |
787 |
788 func sizeGroupSliceInfo(p pointer, f *coderFieldInfo, opts marshalOptions) int { |
788 func sizeGroupSliceInfo(p pointer, f *coderFieldInfo, opts marshalOptions) int { |
789 s := p.PointerSlice() |
789 s := p.PointerSlice() |