blob: 70276e8f5c946ef81aac9a969221b22695bc0b80 [file] [log] [blame]
Girish Gowdrad27a1902021-02-23 16:19:08 -08001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2016 The Go Authors. All rights reserved.
4// https://github.com/golang/protobuf
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
khenaidooac637102019-01-14 15:44:34 -050031
32package ptypes
33
Girish Gowdrad27a1902021-02-23 16:19:08 -080034// This file implements functions to marshal proto.Message to/from
35// google.protobuf.Any message.
36
khenaidooac637102019-01-14 15:44:34 -050037import (
38 "fmt"
Girish Gowdrad27a1902021-02-23 16:19:08 -080039 "reflect"
khenaidooac637102019-01-14 15:44:34 -050040 "strings"
41
42 "github.com/golang/protobuf/proto"
Girish Gowdrad27a1902021-02-23 16:19:08 -080043 "github.com/golang/protobuf/ptypes/any"
khenaidooac637102019-01-14 15:44:34 -050044)
45
Girish Gowdrad27a1902021-02-23 16:19:08 -080046const googleApis = "type.googleapis.com/"
khenaidooac637102019-01-14 15:44:34 -050047
Girish Gowdrad27a1902021-02-23 16:19:08 -080048// AnyMessageName returns the name of the message contained in a google.protobuf.Any message.
49//
50// Note that regular type assertions should be done using the Is
51// function. AnyMessageName is provided for less common use cases like filtering a
52// sequence of Any messages based on a set of allowed message type names.
53func AnyMessageName(any *any.Any) (string, error) {
khenaidooac637102019-01-14 15:44:34 -050054 if any == nil {
55 return "", fmt.Errorf("message is nil")
56 }
Girish Gowdrad27a1902021-02-23 16:19:08 -080057 slash := strings.LastIndex(any.TypeUrl, "/")
58 if slash < 0 {
khenaidooac637102019-01-14 15:44:34 -050059 return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl)
60 }
Girish Gowdrad27a1902021-02-23 16:19:08 -080061 return any.TypeUrl[slash+1:], nil
khenaidooac637102019-01-14 15:44:34 -050062}
63
Girish Gowdrad27a1902021-02-23 16:19:08 -080064// MarshalAny takes the protocol buffer and encodes it into google.protobuf.Any.
65func MarshalAny(pb proto.Message) (*any.Any, error) {
66 value, err := proto.Marshal(pb)
khenaidooac637102019-01-14 15:44:34 -050067 if err != nil {
68 return nil, err
69 }
Girish Gowdrad27a1902021-02-23 16:19:08 -080070 return &any.Any{TypeUrl: googleApis + proto.MessageName(pb), Value: value}, nil
khenaidooac637102019-01-14 15:44:34 -050071}
72
Girish Gowdrad27a1902021-02-23 16:19:08 -080073// DynamicAny is a value that can be passed to UnmarshalAny to automatically
74// allocate a proto.Message for the type specified in a google.protobuf.Any
75// message. The allocated message is stored in the embedded proto.Message.
khenaidooac637102019-01-14 15:44:34 -050076//
Girish Gowdrad27a1902021-02-23 16:19:08 -080077// Example:
78//
79// var x ptypes.DynamicAny
80// if err := ptypes.UnmarshalAny(a, &x); err != nil { ... }
81// fmt.Printf("unmarshaled message: %v", x.Message)
82type DynamicAny struct {
83 proto.Message
84}
85
86// Empty returns a new proto.Message of the type specified in a
87// google.protobuf.Any message. It returns an error if corresponding message
88// type isn't linked in.
89func Empty(any *any.Any) (proto.Message, error) {
90 aname, err := AnyMessageName(any)
91 if err != nil {
92 return nil, err
93 }
94
95 t := proto.MessageType(aname)
96 if t == nil {
97 return nil, fmt.Errorf("any: message type %q isn't linked in", aname)
98 }
99 return reflect.New(t.Elem()).Interface().(proto.Message), nil
100}
101
102// UnmarshalAny parses the protocol buffer representation in a google.protobuf.Any
103// message and places the decoded result in pb. It returns an error if type of
104// contents of Any message does not match type of pb message.
105//
106// pb can be a proto.Message, or a *DynamicAny.
107func UnmarshalAny(any *any.Any, pb proto.Message) error {
108 if d, ok := pb.(*DynamicAny); ok {
109 if d.Message == nil {
khenaidooac637102019-01-14 15:44:34 -0500110 var err error
Girish Gowdrad27a1902021-02-23 16:19:08 -0800111 d.Message, err = Empty(any)
khenaidooac637102019-01-14 15:44:34 -0500112 if err != nil {
113 return err
114 }
115 }
Girish Gowdrad27a1902021-02-23 16:19:08 -0800116 return UnmarshalAny(any, d.Message)
khenaidooac637102019-01-14 15:44:34 -0500117 }
118
Girish Gowdrad27a1902021-02-23 16:19:08 -0800119 aname, err := AnyMessageName(any)
khenaidooac637102019-01-14 15:44:34 -0500120 if err != nil {
121 return err
122 }
Girish Gowdrad27a1902021-02-23 16:19:08 -0800123
124 mname := proto.MessageName(pb)
125 if aname != mname {
126 return fmt.Errorf("mismatched message type: got %q want %q", aname, mname)
khenaidooac637102019-01-14 15:44:34 -0500127 }
Girish Gowdrad27a1902021-02-23 16:19:08 -0800128 return proto.Unmarshal(any.Value, pb)
khenaidooac637102019-01-14 15:44:34 -0500129}
130
Girish Gowdrad27a1902021-02-23 16:19:08 -0800131// Is returns true if any value contains a given message type.
132func Is(any *any.Any, pb proto.Message) bool {
133 // The following is equivalent to AnyMessageName(any) == proto.MessageName(pb),
134 // but it avoids scanning TypeUrl for the slash.
135 if any == nil {
khenaidooac637102019-01-14 15:44:34 -0500136 return false
137 }
Girish Gowdrad27a1902021-02-23 16:19:08 -0800138 name := proto.MessageName(pb)
139 prefix := len(any.TypeUrl) - len(name)
140 return prefix >= 1 && any.TypeUrl[prefix-1] == '/' && any.TypeUrl[prefix:] == name
khenaidooac637102019-01-14 15:44:34 -0500141}