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