blob: e729dcff13c2097db26253cf117bd737e458ec93 [file] [log] [blame]
amit.ghosh258d14c2020-10-02 15:13:38 +02001// 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 Scandoloa6a3aee2019-11-26 13:30:14 -07004
5package ptypes
6
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07007import (
8 "fmt"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07009 "strings"
10
11 "github.com/golang/protobuf/proto"
amit.ghosh258d14c2020-10-02 15:13:38 +020012 "google.golang.org/protobuf/reflect/protoreflect"
13 "google.golang.org/protobuf/reflect/protoregistry"
14
15 anypb "github.com/golang/protobuf/ptypes/any"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070016)
17
amit.ghosh258d14c2020-10-02 15:13:38 +020018const urlPrefix = "type.googleapis.com/"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070019
amit.ghosh258d14c2020-10-02 15:13:38 +020020// AnyMessageName returns the message name contained in an anypb.Any message.
21// Most type assertions should use the Is function instead.
22func AnyMessageName(any *anypb.Any) (string, error) {
23 name, err := anyMessageName(any)
24 return string(name), err
25}
26func anyMessageName(any *anypb.Any) (protoreflect.FullName, error) {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070027 if any == nil {
28 return "", fmt.Errorf("message is nil")
29 }
amit.ghosh258d14c2020-10-02 15:13:38 +020030 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 Scandoloa6a3aee2019-11-26 13:30:14 -070035 return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl)
36 }
amit.ghosh258d14c2020-10-02 15:13:38 +020037 return name, nil
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070038}
39
amit.ghosh258d14c2020-10-02 15:13:38 +020040// MarshalAny marshals the given message m into an anypb.Any message.
41func 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 Scandoloa6a3aee2019-11-26 13:30:14 -070052 if err != nil {
53 return nil, err
54 }
amit.ghosh258d14c2020-10-02 15:13:38 +020055 return &anypb.Any{TypeUrl: urlPrefix + proto.MessageName(m), Value: b}, nil
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070056}
57
amit.ghosh258d14c2020-10-02 15:13:38 +020058// 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.
61func Empty(any *anypb.Any) (proto.Message, error) {
62 name, err := anyMessageName(any)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070063 if err != nil {
64 return nil, err
65 }
amit.ghosh258d14c2020-10-02 15:13:38 +020066 mt, err := protoregistry.GlobalTypes.FindMessageByName(name)
67 if err != nil {
68 return nil, err
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070069 }
amit.ghosh258d14c2020-10-02 15:13:38 +020070 return proto.MessageV1(mt.New().Interface()), nil
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070071}
72
amit.ghosh258d14c2020-10-02 15:13:38 +020073// 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 Scandoloa6a3aee2019-11-26 13:30:14 -070076//
amit.ghosh258d14c2020-10-02 15:13:38 +020077// The target message m may be a *DynamicAny message. If the underlying message
78// type could not be resolved, then this returns protoregistry.NotFound.
79func UnmarshalAny(any *anypb.Any, m proto.Message) error {
80 if dm, ok := m.(*DynamicAny); ok {
81 if dm.Message == nil {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070082 var err error
amit.ghosh258d14c2020-10-02 15:13:38 +020083 dm.Message, err = Empty(any)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070084 if err != nil {
85 return err
86 }
87 }
amit.ghosh258d14c2020-10-02 15:13:38 +020088 m = dm.Message
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070089 }
90
amit.ghosh258d14c2020-10-02 15:13:38 +020091 anyName, err := AnyMessageName(any)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070092 if err != nil {
93 return err
94 }
amit.ghosh258d14c2020-10-02 15:13:38 +020095 msgName := proto.MessageName(m)
96 if anyName != msgName {
97 return fmt.Errorf("mismatched message type: got %q want %q", anyName, msgName)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070098 }
amit.ghosh258d14c2020-10-02 15:13:38 +020099 return proto.Unmarshal(any.Value, m)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700100}
101
amit.ghosh258d14c2020-10-02 15:13:38 +0200102// Is reports whether the Any message contains a message of the specified type.
103func Is(any *anypb.Any, m proto.Message) bool {
104 if any == nil || m == nil {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700105 return false
106 }
amit.ghosh258d14c2020-10-02 15:13:38 +0200107 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)
122type DynamicAny struct{ proto.Message }
123
124func (m DynamicAny) String() string {
125 if m.Message == nil {
126 return "<nil>"
127 }
128 return m.Message.String()
129}
130func (m DynamicAny) Reset() {
131 if m.Message == nil {
132 return
133 }
134 m.Message.Reset()
135}
136func (m DynamicAny) ProtoMessage() {
137 return
138}
139func (m DynamicAny) ProtoReflect() protoreflect.Message {
140 if m.Message == nil {
141 return nil
142 }
143 return dynamicAny{proto.MessageReflect(m.Message)}
144}
145
146type dynamicAny struct{ protoreflect.Message }
147
148func (m dynamicAny) Type() protoreflect.MessageType {
149 return dynamicAnyType{m.Message.Type()}
150}
151func (m dynamicAny) New() protoreflect.Message {
152 return dynamicAnyType{m.Message.Type()}.New()
153}
154func (m dynamicAny) Interface() protoreflect.ProtoMessage {
155 return DynamicAny{proto.MessageV1(m.Message.Interface())}
156}
157
158type dynamicAnyType struct{ protoreflect.MessageType }
159
160func (t dynamicAnyType) New() protoreflect.Message {
161 return dynamicAny{t.MessageType.New()}
162}
163func (t dynamicAnyType) Zero() protoreflect.Message {
164 return dynamicAny{t.MessageType.Zero()}
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700165}