blob: 85f9f57365fd4e32c3c26b9bf80a063e7662f91b [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// 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.
khenaidooac637102019-01-14 15:44:34 -05004
5package ptypes
6
khenaidooac637102019-01-14 15:44:34 -05007import (
8 "fmt"
khenaidooac637102019-01-14 15:44:34 -05009 "strings"
10
11 "github.com/golang/protobuf/proto"
khenaidood948f772021-08-11 17:49:24 -040012 "google.golang.org/protobuf/reflect/protoreflect"
13 "google.golang.org/protobuf/reflect/protoregistry"
14
15 anypb "github.com/golang/protobuf/ptypes/any"
khenaidooac637102019-01-14 15:44:34 -050016)
17
khenaidood948f772021-08-11 17:49:24 -040018const urlPrefix = "type.googleapis.com/"
khenaidooac637102019-01-14 15:44:34 -050019
khenaidood948f772021-08-11 17:49:24 -040020// AnyMessageName returns the message name contained in an anypb.Any message.
21// Most type assertions should use the Is function instead.
Girish Gowdrad27a1902021-02-23 16:19:08 -080022//
khenaidood948f772021-08-11 17:49:24 -040023// Deprecated: Call the any.MessageName method instead.
24func AnyMessageName(any *anypb.Any) (string, error) {
25 name, err := anyMessageName(any)
26 return string(name), err
27}
28func anyMessageName(any *anypb.Any) (protoreflect.FullName, error) {
khenaidooac637102019-01-14 15:44:34 -050029 if any == nil {
30 return "", fmt.Errorf("message is nil")
31 }
khenaidood948f772021-08-11 17:49:24 -040032 name := protoreflect.FullName(any.TypeUrl)
33 if i := strings.LastIndex(any.TypeUrl, "/"); i >= 0 {
34 name = name[i+len("/"):]
35 }
36 if !name.IsValid() {
khenaidooac637102019-01-14 15:44:34 -050037 return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl)
38 }
khenaidood948f772021-08-11 17:49:24 -040039 return name, nil
khenaidooac637102019-01-14 15:44:34 -050040}
41
khenaidood948f772021-08-11 17:49:24 -040042// MarshalAny marshals the given message m into an anypb.Any message.
43//
44// Deprecated: Call the anypb.New function instead.
45func 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)
khenaidooac637102019-01-14 15:44:34 -050056 if err != nil {
57 return nil, err
58 }
khenaidood948f772021-08-11 17:49:24 -040059 return &anypb.Any{TypeUrl: urlPrefix + proto.MessageName(m), Value: b}, nil
khenaidooac637102019-01-14 15:44:34 -050060}
61
khenaidood948f772021-08-11 17:49:24 -040062// 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.
khenaidooac637102019-01-14 15:44:34 -050065//
khenaidood948f772021-08-11 17:49:24 -040066// Deprecated: Use protoregistry.GlobalTypes.FindMessageByName instead
67// to resolve the message name and create a new instance of it.
68func Empty(any *anypb.Any) (proto.Message, error) {
69 name, err := anyMessageName(any)
Girish Gowdrad27a1902021-02-23 16:19:08 -080070 if err != nil {
71 return nil, err
72 }
khenaidood948f772021-08-11 17:49:24 -040073 mt, err := protoregistry.GlobalTypes.FindMessageByName(name)
74 if err != nil {
75 return nil, err
Girish Gowdrad27a1902021-02-23 16:19:08 -080076 }
khenaidood948f772021-08-11 17:49:24 -040077 return proto.MessageV1(mt.New().Interface()), nil
Girish Gowdrad27a1902021-02-23 16:19:08 -080078}
79
khenaidood948f772021-08-11 17:49:24 -040080// 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.
Girish Gowdrad27a1902021-02-23 16:19:08 -080083//
khenaidood948f772021-08-11 17:49:24 -040084// 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.
88func UnmarshalAny(any *anypb.Any, m proto.Message) error {
89 if dm, ok := m.(*DynamicAny); ok {
90 if dm.Message == nil {
khenaidooac637102019-01-14 15:44:34 -050091 var err error
khenaidood948f772021-08-11 17:49:24 -040092 dm.Message, err = Empty(any)
khenaidooac637102019-01-14 15:44:34 -050093 if err != nil {
94 return err
95 }
96 }
khenaidood948f772021-08-11 17:49:24 -040097 m = dm.Message
khenaidooac637102019-01-14 15:44:34 -050098 }
99
khenaidood948f772021-08-11 17:49:24 -0400100 anyName, err := AnyMessageName(any)
khenaidooac637102019-01-14 15:44:34 -0500101 if err != nil {
102 return err
103 }
khenaidood948f772021-08-11 17:49:24 -0400104 msgName := proto.MessageName(m)
105 if anyName != msgName {
106 return fmt.Errorf("mismatched message type: got %q want %q", anyName, msgName)
khenaidooac637102019-01-14 15:44:34 -0500107 }
khenaidood948f772021-08-11 17:49:24 -0400108 return proto.Unmarshal(any.Value, m)
khenaidooac637102019-01-14 15:44:34 -0500109}
110
khenaidood948f772021-08-11 17:49:24 -0400111// Is reports whether the Any message contains a message of the specified type.
112//
113// Deprecated: Call the any.MessageIs method instead.
114func Is(any *anypb.Any, m proto.Message) bool {
115 if any == nil || m == nil {
khenaidooac637102019-01-14 15:44:34 -0500116 return false
117 }
khenaidood948f772021-08-11 17:49:24 -0400118 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.
136type DynamicAny struct{ proto.Message }
137
138func (m DynamicAny) String() string {
139 if m.Message == nil {
140 return "<nil>"
141 }
142 return m.Message.String()
143}
144func (m DynamicAny) Reset() {
145 if m.Message == nil {
146 return
147 }
148 m.Message.Reset()
149}
150func (m DynamicAny) ProtoMessage() {
151 return
152}
153func (m DynamicAny) ProtoReflect() protoreflect.Message {
154 if m.Message == nil {
155 return nil
156 }
157 return dynamicAny{proto.MessageReflect(m.Message)}
158}
159
160type dynamicAny struct{ protoreflect.Message }
161
162func (m dynamicAny) Type() protoreflect.MessageType {
163 return dynamicAnyType{m.Message.Type()}
164}
165func (m dynamicAny) New() protoreflect.Message {
166 return dynamicAnyType{m.Message.Type()}.New()
167}
168func (m dynamicAny) Interface() protoreflect.ProtoMessage {
169 return DynamicAny{proto.MessageV1(m.Message.Interface())}
170}
171
172type dynamicAnyType struct{ protoreflect.MessageType }
173
174func (t dynamicAnyType) New() protoreflect.Message {
175 return dynamicAny{t.MessageType.New()}
176}
177func (t dynamicAnyType) Zero() protoreflect.Message {
178 return dynamicAny{t.MessageType.Zero()}
khenaidooac637102019-01-14 15:44:34 -0500179}