MCPcopy
hub / github.com/golang/protobuf / unmarshalMessage

Method unmarshalMessage

proto/text_decode.go:91–169  ·  view source on GitHub ↗
(m protoreflect.Message, terminator string)

Source from the content-addressed store, hash-verified

89}
90
91func (p *textParser) unmarshalMessage(m protoreflect.Message, terminator string) (err error) {
92 md := m.Descriptor()
93 fds := md.Fields()
94
95 // A struct is a sequence of "name: value", terminated by one of
96 // '>' or '}', or the end of the input. A name may also be
97 // "[extension]" or "[type/url]".
98 //
99 // The whole struct can also be an expanded Any message, like:
100 // [type/url] < ... struct contents ... >
101 seen := make(map[protoreflect.FieldNumber]bool)
102 for {
103 tok := p.next()
104 if tok.err != nil {
105 return tok.err
106 }
107 if tok.value == terminator {
108 break
109 }
110 if tok.value == "[" {
111 if err := p.unmarshalExtensionOrAny(m, seen); err != nil {
112 return err
113 }
114 continue
115 }
116
117 // This is a normal, non-extension field.
118 name := protoreflect.Name(tok.value)
119 fd := fds.ByName(name)
120 switch {
121 case fd == nil:
122 gd := fds.ByName(protoreflect.Name(strings.ToLower(string(name))))
123 if gd != nil && gd.Kind() == protoreflect.GroupKind && gd.Message().Name() == name {
124 fd = gd
125 }
126 case fd.Kind() == protoreflect.GroupKind && fd.Message().Name() != name:
127 fd = nil
128 case fd.IsWeak() && fd.Message().IsPlaceholder():
129 fd = nil
130 }
131 if fd == nil {
132 typeName := string(md.FullName())
133 if m, ok := m.Interface().(Message); ok {
134 t := reflect.TypeOf(m)
135 if t.Kind() == reflect.Ptr {
136 typeName = t.Elem().String()
137 }
138 }
139 return p.errorf("unknown field name %q in %v", name, typeName)
140 }
141 if od := fd.ContainingOneof(); od != nil && m.WhichOneof(od) != nil {
142 return p.errorf("field '%s' would overwrite already parsed oneof '%s'", name, od.Name())
143 }
144 if fd.Cardinality() != protoreflect.Repeated && seen[fd.Number()] {
145 return p.errorf("non-repeated field %q was repeated", fd.Name())
146 }
147 seen[fd.Number()] = true
148

Callers 3

UnmarshalTextFunction · 0.45

Calls 11

nextMethod · 0.95
errorfMethod · 0.95
checkForColonMethod · 0.95
unmarshalValueMethod · 0.95
InterfaceMethod · 0.80
HasMethod · 0.80
DescriptorMethod · 0.65
NameMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected