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)
| 344 | // marshalAny marshals the given google.protobuf.Any message in expanded form. |
| 345 | // It returns true if it was able to marshal, else false. |
| 346 | func (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 | } |
no test coverage detected