khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | // Copyright 2016 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 4 | |
| 5 | package ptypes |
| 6 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 7 | import ( |
| 8 | "fmt" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 9 | "strings" |
| 10 | |
| 11 | "github.com/golang/protobuf/proto" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 12 | "google.golang.org/protobuf/reflect/protoreflect" |
| 13 | "google.golang.org/protobuf/reflect/protoregistry" |
| 14 | |
| 15 | anypb "github.com/golang/protobuf/ptypes/any" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 18 | const urlPrefix = "type.googleapis.com/" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 19 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 20 | // AnyMessageName returns the message name contained in an anypb.Any message. |
| 21 | // Most type assertions should use the Is function instead. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 22 | // |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 23 | // Deprecated: Call the any.MessageName method instead. |
| 24 | func AnyMessageName(any *anypb.Any) (string, error) { |
| 25 | name, err := anyMessageName(any) |
| 26 | return string(name), err |
| 27 | } |
| 28 | func anyMessageName(any *anypb.Any) (protoreflect.FullName, error) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 29 | if any == nil { |
| 30 | return "", fmt.Errorf("message is nil") |
| 31 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 32 | name := protoreflect.FullName(any.TypeUrl) |
| 33 | if i := strings.LastIndex(any.TypeUrl, "/"); i >= 0 { |
| 34 | name = name[i+len("/"):] |
| 35 | } |
| 36 | if !name.IsValid() { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 37 | return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl) |
| 38 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 39 | return name, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 40 | } |
| 41 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 42 | // MarshalAny marshals the given message m into an anypb.Any message. |
| 43 | // |
| 44 | // Deprecated: Call the anypb.New function instead. |
| 45 | func MarshalAny(m proto.Message) (*anypb.Any, error) { |
| 46 | switch dm := m.(type) { |
| 47 | case DynamicAny: |
| 48 | m = dm.Message |
| 49 | case *DynamicAny: |
| 50 | if dm == nil { |
| 51 | return nil, proto.ErrNil |
| 52 | } |
| 53 | m = dm.Message |
| 54 | } |
| 55 | b, err := proto.Marshal(m) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 59 | return &anypb.Any{TypeUrl: urlPrefix + proto.MessageName(m), Value: b}, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 60 | } |
| 61 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 62 | // Empty returns a new message of the type specified in an anypb.Any message. |
| 63 | // It returns protoregistry.NotFound if the corresponding message type could not |
| 64 | // be resolved in the global registry. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 65 | // |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 66 | // Deprecated: Use protoregistry.GlobalTypes.FindMessageByName instead |
| 67 | // to resolve the message name and create a new instance of it. |
| 68 | func Empty(any *anypb.Any) (proto.Message, error) { |
| 69 | name, err := anyMessageName(any) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 73 | mt, err := protoregistry.GlobalTypes.FindMessageByName(name) |
| 74 | if err != nil { |
| 75 | return nil, err |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 76 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 77 | return proto.MessageV1(mt.New().Interface()), nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 78 | } |
| 79 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 80 | // UnmarshalAny unmarshals the encoded value contained in the anypb.Any message |
| 81 | // into the provided message m. It returns an error if the target message |
| 82 | // does not match the type in the Any message or if an unmarshal error occurs. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 83 | // |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 84 | // The target message m may be a *DynamicAny message. If the underlying message |
| 85 | // type could not be resolved, then this returns protoregistry.NotFound. |
| 86 | // |
| 87 | // Deprecated: Call the any.UnmarshalTo method instead. |
| 88 | func UnmarshalAny(any *anypb.Any, m proto.Message) error { |
| 89 | if dm, ok := m.(*DynamicAny); ok { |
| 90 | if dm.Message == nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 91 | var err error |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 92 | dm.Message, err = Empty(any) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 97 | m = dm.Message |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 100 | anyName, err := AnyMessageName(any) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 101 | if err != nil { |
| 102 | return err |
| 103 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 104 | msgName := proto.MessageName(m) |
| 105 | if anyName != msgName { |
| 106 | return fmt.Errorf("mismatched message type: got %q want %q", anyName, msgName) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 107 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 108 | return proto.Unmarshal(any.Value, m) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 109 | } |
| 110 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 111 | // Is reports whether the Any message contains a message of the specified type. |
| 112 | // |
| 113 | // Deprecated: Call the any.MessageIs method instead. |
| 114 | func Is(any *anypb.Any, m proto.Message) bool { |
| 115 | if any == nil || m == nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 116 | return false |
| 117 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 118 | name := proto.MessageName(m) |
| 119 | if !strings.HasSuffix(any.TypeUrl, name) { |
| 120 | return false |
| 121 | } |
| 122 | return len(any.TypeUrl) == len(name) || any.TypeUrl[len(any.TypeUrl)-len(name)-1] == '/' |
| 123 | } |
| 124 | |
| 125 | // DynamicAny is a value that can be passed to UnmarshalAny to automatically |
| 126 | // allocate a proto.Message for the type specified in an anypb.Any message. |
| 127 | // The allocated message is stored in the embedded proto.Message. |
| 128 | // |
| 129 | // Example: |
| 130 | // var x ptypes.DynamicAny |
| 131 | // if err := ptypes.UnmarshalAny(a, &x); err != nil { ... } |
| 132 | // fmt.Printf("unmarshaled message: %v", x.Message) |
| 133 | // |
| 134 | // Deprecated: Use the any.UnmarshalNew method instead to unmarshal |
| 135 | // the any message contents into a new instance of the underlying message. |
| 136 | type DynamicAny struct{ proto.Message } |
| 137 | |
| 138 | func (m DynamicAny) String() string { |
| 139 | if m.Message == nil { |
| 140 | return "<nil>" |
| 141 | } |
| 142 | return m.Message.String() |
| 143 | } |
| 144 | func (m DynamicAny) Reset() { |
| 145 | if m.Message == nil { |
| 146 | return |
| 147 | } |
| 148 | m.Message.Reset() |
| 149 | } |
| 150 | func (m DynamicAny) ProtoMessage() { |
| 151 | return |
| 152 | } |
| 153 | func (m DynamicAny) ProtoReflect() protoreflect.Message { |
| 154 | if m.Message == nil { |
| 155 | return nil |
| 156 | } |
| 157 | return dynamicAny{proto.MessageReflect(m.Message)} |
| 158 | } |
| 159 | |
| 160 | type dynamicAny struct{ protoreflect.Message } |
| 161 | |
| 162 | func (m dynamicAny) Type() protoreflect.MessageType { |
| 163 | return dynamicAnyType{m.Message.Type()} |
| 164 | } |
| 165 | func (m dynamicAny) New() protoreflect.Message { |
| 166 | return dynamicAnyType{m.Message.Type()}.New() |
| 167 | } |
| 168 | func (m dynamicAny) Interface() protoreflect.ProtoMessage { |
| 169 | return DynamicAny{proto.MessageV1(m.Message.Interface())} |
| 170 | } |
| 171 | |
| 172 | type dynamicAnyType struct{ protoreflect.MessageType } |
| 173 | |
| 174 | func (t dynamicAnyType) New() protoreflect.Message { |
| 175 | return dynamicAny{t.MessageType.New()} |
| 176 | } |
| 177 | func (t dynamicAnyType) Zero() protoreflect.Message { |
| 178 | return dynamicAny{t.MessageType.Zero()} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 179 | } |