Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / gogo / protobuf / types / any.go
1 // 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.
31
32 package types
33
34 // This file implements functions to marshal proto.Message to/from
35 // google.protobuf.Any message.
36
37 import (
38         "fmt"
39         "reflect"
40         "strings"
41
42         "github.com/gogo/protobuf/proto"
43 )
44
45 const googleApis = "type.googleapis.com/"
46
47 // AnyMessageName returns the name of the message contained in a google.protobuf.Any message.
48 //
49 // Note that regular type assertions should be done using the Is
50 // function. AnyMessageName is provided for less common use cases like filtering a
51 // sequence of Any messages based on a set of allowed message type names.
52 func AnyMessageName(any *Any) (string, error) {
53         slash := strings.LastIndex(any.TypeUrl, "/")
54         if slash < 0 {
55                 return "", fmt.Errorf("message type url %q is invalid", any.TypeUrl)
56         }
57         return any.TypeUrl[slash+1:], nil
58 }
59
60 // MarshalAny takes the protocol buffer and encodes it into google.protobuf.Any.
61 func MarshalAny(pb proto.Message) (*Any, error) {
62         value, err := proto.Marshal(pb)
63         if err != nil {
64                 return nil, err
65         }
66         return &Any{TypeUrl: googleApis + proto.MessageName(pb), Value: value}, nil
67 }
68
69 // DynamicAny is a value that can be passed to UnmarshalAny to automatically
70 // allocate a proto.Message for the type specified in a google.protobuf.Any
71 // message. The allocated message is stored in the embedded proto.Message.
72 //
73 // Example:
74 //
75 //   var x ptypes.DynamicAny
76 //   if err := ptypes.UnmarshalAny(a, &x); err != nil { ... }
77 //   fmt.Printf("unmarshaled message: %v", x.Message)
78 type DynamicAny struct {
79         proto.Message
80 }
81
82 // Empty returns a new proto.Message of the type specified in a
83 // google.protobuf.Any message. It returns an error if corresponding message
84 // type isn't linked in.
85 func EmptyAny(any *Any) (proto.Message, error) {
86         aname, err := AnyMessageName(any)
87         if err != nil {
88                 return nil, err
89         }
90
91         t := proto.MessageType(aname)
92         if t == nil {
93                 return nil, fmt.Errorf("any: message type %q isn't linked in", aname)
94         }
95         return reflect.New(t.Elem()).Interface().(proto.Message), nil
96 }
97
98 // UnmarshalAny parses the protocol buffer representation in a google.protobuf.Any
99 // message and places the decoded result in pb. It returns an error if type of
100 // contents of Any message does not match type of pb message.
101 //
102 // pb can be a proto.Message, or a *DynamicAny.
103 func UnmarshalAny(any *Any, pb proto.Message) error {
104         if d, ok := pb.(*DynamicAny); ok {
105                 if d.Message == nil {
106                         var err error
107                         d.Message, err = EmptyAny(any)
108                         if err != nil {
109                                 return err
110                         }
111                 }
112                 return UnmarshalAny(any, d.Message)
113         }
114
115         aname, err := AnyMessageName(any)
116         if err != nil {
117                 return err
118         }
119
120         mname := proto.MessageName(pb)
121         if aname != mname {
122                 return fmt.Errorf("mismatched message type: got %q want %q", aname, mname)
123         }
124         return proto.Unmarshal(any.Value, pb)
125 }
126
127 // Is returns true if any value contains a given message type.
128 func Is(any *Any, pb proto.Message) bool {
129         aname, err := AnyMessageName(any)
130         if err != nil {
131                 return false
132         }
133
134         return aname == proto.MessageName(pb)
135 }