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

Method unmarshalMessage

jsonpb/decode.go:135–387  ·  jsonpb/decode.go::Unmarshaler.unmarshalMessage
(m protoreflect.Message, in []byte)

Source from the content-addressed store, hash-verified

133}
134
135func (u *Unmarshaler) unmarshalMessage(m protoreflect.Message, in []byte) error {
136 md := m.Descriptor()
137 fds := md.Fields()
138
139 if jsu, ok := proto.MessageV1(m.Interface()).(JSONPBUnmarshaler); ok {
140 return jsu.UnmarshalJSONPB(u, in)
141 }
142
143 if string(in) == "null" && md.FullName() != "google.protobuf.Value" {
144 return nil
145 }
146
147 switch wellKnownType(md.FullName()) {
148 case "Any":
149 var jsonObject map[string]json.RawMessage
150 if err := json.Unmarshal(in, &jsonObject); err != nil {
151 return err
152 }
153
154 rawTypeURL, ok := jsonObject["@type"]
155 if !ok {
156 return errors.New("Any JSON doesn't have '@type'")
157 }
158 typeURL, err := unquoteString(string(rawTypeURL))
159 if err != nil {
160 return fmt.Errorf("can't unmarshal Any's '@type': %q", rawTypeURL)
161 }
162 m.Set(fds.ByNumber(1), protoreflect.ValueOfString(typeURL))
163
164 var m2 protoreflect.Message
165 if u.AnyResolver != nil {
166 mi, err := u.AnyResolver.Resolve(typeURL)
167 if err != nil {
168 return err
169 }
170 m2 = proto.MessageReflect(mi)
171 } else {
172 mt, err := protoregistry.GlobalTypes.FindMessageByURL(typeURL)
173 if err != nil {
174 if err == protoregistry.NotFound {
175 return fmt.Errorf("could not resolve Any message type: %v", typeURL)
176 }
177 return err
178 }
179 m2 = mt.New()
180 }
181
182 if wellKnownType(m2.Descriptor().FullName()) != "" {
183 rawValue, ok := jsonObject["value"]
184 if !ok {
185 return errors.New("Any JSON doesn't have 'value'")
186 }
187 if err := u.unmarshalMessage(m2, rawValue); err != nil {
188 return fmt.Errorf("can't unmarshal Any nested proto %v: %v", typeURL, err)
189 }
190 } else {
191 delete(jsonObject, "@type")
192 rawJSON, err := json.Marshal(jsonObject)

Callers 2

UnmarshalNextMethod · 0.95

Implementers 3

Bufferproto/buffer.go
InternalMessageInfoproto/deprecated.go
Unmarshalerjsonpb/decode.go

Calls 15

DescriptorMethod · 0.95
unmarshalValueMethod · 0.95
wellKnownTypeFunction · 0.85
unquoteStringFunction · 0.85
hasPrefixAndSuffixFunction · 0.85
isSingularWellKnownValueFunction · 0.85
InterfaceMethod · 0.80
FindMessageByURLMethod · 0.80
ParseMethod · 0.80
isMessageSetFunction · 0.70
UnmarshalJSONPBMethod · 0.65

Tested by

no test coverage detected