MCPcopy
hub / github.com/protocolbuffers/protobuf-go / marshalAny

Method marshalAny

encoding/prototext/encode.go:346–380  ·  view source on GitHub ↗

marshalAny marshals the given google.protobuf.Any message in expanded form. It returns true if it was able to marshal, else false.

(any protoreflect.Message)

Source from the content-addressed store, hash-verified

344// marshalAny marshals the given google.protobuf.Any message in expanded form.
345// It returns true if it was able to marshal, else false.
346func (e encoder) marshalAny(any protoreflect.Message) bool {
347 // Construct the embedded message.
348 fds := any.Descriptor().Fields()
349 fdType := fds.ByNumber(genid.Any_TypeUrl_field_number)
350 typeURL := any.Get(fdType).String()
351 mt, err := e.opts.Resolver.FindMessageByURL(typeURL)
352 if err != nil {
353 return false
354 }
355 m := mt.New().Interface()
356
357 // Unmarshal bytes into embedded message.
358 fdValue := fds.ByNumber(genid.Any_Value_field_number)
359 value := any.Get(fdValue)
360 err = proto.UnmarshalOptions{
361 AllowPartial: true,
362 Resolver: e.opts.Resolver,
363 }.Unmarshal(value.Bytes(), m)
364 if err != nil {
365 return false
366 }
367
368 // Get current encoder position. If marshaling fails, reset encoder output
369 // back to this position.
370 pos := e.Snapshot()
371
372 // Field name is the proto field name enclosed in [].
373 e.WriteName("[" + typeURL + "]")
374 err = e.marshalMessage(m.ProtoReflect(), true)
375 if err != nil {
376 e.Reset(pos)
377 return false
378 }
379 return true
380}

Callers 1

marshalMessageMethod · 0.95

Calls 15

marshalMessageMethod · 0.95
SnapshotMethod · 0.80
FieldsMethod · 0.65
DescriptorMethod · 0.65
ByNumberMethod · 0.65
StringMethod · 0.65
GetMethod · 0.65
FindMessageByURLMethod · 0.65
InterfaceMethod · 0.65
NewMethod · 0.65
UnmarshalMethod · 0.65
ProtoReflectMethod · 0.65

Tested by

no test coverage detected