...

Source file src/google.golang.org/protobuf/types/known/anypb/any.pb.go

Documentation: google.golang.org/protobuf/types/known/anypb

     1  // Protocol Buffers - Google's data interchange format
     2  // Copyright 2008 Google Inc.  All rights reserved.
     3  // https://developers.google.com/protocol-buffers/
     4  //
     5  // Redistribution and use in source and binary forms, with or without
     6  // modification, are permitted provided that the following conditions are
     7  // met:
     8  //
     9  //     * Redistributions of source code must retain the above copyright
    10  // notice, this list of conditions and the following disclaimer.
    11  //     * Redistributions in binary form must reproduce the above
    12  // copyright notice, this list of conditions and the following disclaimer
    13  // in the documentation and/or other materials provided with the
    14  // distribution.
    15  //     * Neither the name of Google Inc. nor the names of its
    16  // contributors may be used to endorse or promote products derived from
    17  // this software without specific prior written permission.
    18  //
    19  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    20  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    21  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    22  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    23  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    24  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    25  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    26  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    27  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    28  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    29  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30  
    31  // Code generated by protoc-gen-go. DO NOT EDIT.
    32  // source: google/protobuf/any.proto
    33  
    34  // Package anypb contains generated types for google/protobuf/any.proto.
    35  //
    36  // The Any message is a dynamic representation of any other message value.
    37  // It is functionally a tuple of the full name of the remote message type and
    38  // the serialized bytes of the remote message value.
    39  //
    40  // # Constructing an Any
    41  //
    42  // An Any message containing another message value is constructed using New:
    43  //
    44  //	any, err := anypb.New(m)
    45  //	if err != nil {
    46  //		... // handle error
    47  //	}
    48  //	... // make use of any
    49  //
    50  // # Unmarshaling an Any
    51  //
    52  // With a populated Any message, the underlying message can be serialized into
    53  // a remote concrete message value in a few ways.
    54  //
    55  // If the exact concrete type is known, then a new (or pre-existing) instance
    56  // of that message can be passed to the UnmarshalTo method:
    57  //
    58  //	m := new(foopb.MyMessage)
    59  //	if err := any.UnmarshalTo(m); err != nil {
    60  //		... // handle error
    61  //	}
    62  //	... // make use of m
    63  //
    64  // If the exact concrete type is not known, then the UnmarshalNew method can be
    65  // used to unmarshal the contents into a new instance of the remote message type:
    66  //
    67  //	m, err := any.UnmarshalNew()
    68  //	if err != nil {
    69  //		... // handle error
    70  //	}
    71  //	... // make use of m
    72  //
    73  // UnmarshalNew uses the global type registry to resolve the message type and
    74  // construct a new instance of that message to unmarshal into. In order for a
    75  // message type to appear in the global registry, the Go type representing that
    76  // protobuf message type must be linked into the Go binary. For messages
    77  // generated by protoc-gen-go, this is achieved through an import of the
    78  // generated Go package representing a .proto file.
    79  //
    80  // A common pattern with UnmarshalNew is to use a type switch with the resulting
    81  // proto.Message value:
    82  //
    83  //	switch m := m.(type) {
    84  //	case *foopb.MyMessage:
    85  //		... // make use of m as a *foopb.MyMessage
    86  //	case *barpb.OtherMessage:
    87  //		... // make use of m as a *barpb.OtherMessage
    88  //	case *bazpb.SomeMessage:
    89  //		... // make use of m as a *bazpb.SomeMessage
    90  //	}
    91  //
    92  // This pattern ensures that the generated packages containing the message types
    93  // listed in the case clauses are linked into the Go binary and therefore also
    94  // registered in the global registry.
    95  //
    96  // # Type checking an Any
    97  //
    98  // In order to type check whether an Any message represents some other message,
    99  // then use the MessageIs method:
   100  //
   101  //	if any.MessageIs((*foopb.MyMessage)(nil)) {
   102  //		... // make use of any, knowing that it contains a foopb.MyMessage
   103  //	}
   104  //
   105  // The MessageIs method can also be used with an allocated instance of the target
   106  // message type if the intention is to unmarshal into it if the type matches:
   107  //
   108  //	m := new(foopb.MyMessage)
   109  //	if any.MessageIs(m) {
   110  //		if err := any.UnmarshalTo(m); err != nil {
   111  //			... // handle error
   112  //		}
   113  //		... // make use of m
   114  //	}
   115  package anypb
   116  
   117  import (
   118  	proto "google.golang.org/protobuf/proto"
   119  	protoreflect "google.golang.org/protobuf/reflect/protoreflect"
   120  	protoregistry "google.golang.org/protobuf/reflect/protoregistry"
   121  	protoimpl "google.golang.org/protobuf/runtime/protoimpl"
   122  	reflect "reflect"
   123  	strings "strings"
   124  	sync "sync"
   125  )
   126  
   127  // `Any` contains an arbitrary serialized protocol buffer message along with a
   128  // URL that describes the type of the serialized message.
   129  //
   130  // Protobuf library provides support to pack/unpack Any values in the form
   131  // of utility functions or additional generated methods of the Any type.
   132  //
   133  // Example 1: Pack and unpack a message in C++.
   134  //
   135  //	Foo foo = ...;
   136  //	Any any;
   137  //	any.PackFrom(foo);
   138  //	...
   139  //	if (any.UnpackTo(&foo)) {
   140  //	  ...
   141  //	}
   142  //
   143  // Example 2: Pack and unpack a message in Java.
   144  //
   145  //	   Foo foo = ...;
   146  //	   Any any = Any.pack(foo);
   147  //	   ...
   148  //	   if (any.is(Foo.class)) {
   149  //	     foo = any.unpack(Foo.class);
   150  //	   }
   151  //	   // or ...
   152  //	   if (any.isSameTypeAs(Foo.getDefaultInstance())) {
   153  //	     foo = any.unpack(Foo.getDefaultInstance());
   154  //	   }
   155  //
   156  //	Example 3: Pack and unpack a message in Python.
   157  //
   158  //	   foo = Foo(...)
   159  //	   any = Any()
   160  //	   any.Pack(foo)
   161  //	   ...
   162  //	   if any.Is(Foo.DESCRIPTOR):
   163  //	     any.Unpack(foo)
   164  //	     ...
   165  //
   166  //	Example 4: Pack and unpack a message in Go
   167  //
   168  //	    foo := &pb.Foo{...}
   169  //	    any, err := anypb.New(foo)
   170  //	    if err != nil {
   171  //	      ...
   172  //	    }
   173  //	    ...
   174  //	    foo := &pb.Foo{}
   175  //	    if err := any.UnmarshalTo(foo); err != nil {
   176  //	      ...
   177  //	    }
   178  //
   179  // The pack methods provided by protobuf library will by default use
   180  // 'type.googleapis.com/full.type.name' as the type URL and the unpack
   181  // methods only use the fully qualified type name after the last '/'
   182  // in the type URL, for example "foo.bar.com/x/y.z" will yield type
   183  // name "y.z".
   184  //
   185  // JSON
   186  // ====
   187  // The JSON representation of an `Any` value uses the regular
   188  // representation of the deserialized, embedded message, with an
   189  // additional field `@type` which contains the type URL. Example:
   190  //
   191  //	package google.profile;
   192  //	message Person {
   193  //	  string first_name = 1;
   194  //	  string last_name = 2;
   195  //	}
   196  //
   197  //	{
   198  //	  "@type": "type.googleapis.com/google.profile.Person",
   199  //	  "firstName": <string>,
   200  //	  "lastName": <string>
   201  //	}
   202  //
   203  // If the embedded message type is well-known and has a custom JSON
   204  // representation, that representation will be embedded adding a field
   205  // `value` which holds the custom JSON in addition to the `@type`
   206  // field. Example (for message [google.protobuf.Duration][]):
   207  //
   208  //	{
   209  //	  "@type": "type.googleapis.com/google.protobuf.Duration",
   210  //	  "value": "1.212s"
   211  //	}
   212  type Any struct {
   213  	state         protoimpl.MessageState
   214  	sizeCache     protoimpl.SizeCache
   215  	unknownFields protoimpl.UnknownFields
   216  
   217  	// A URL/resource name that uniquely identifies the type of the serialized
   218  	// protocol buffer message. This string must contain at least
   219  	// one "/" character. The last segment of the URL's path must represent
   220  	// the fully qualified name of the type (as in
   221  	// `path/google.protobuf.Duration`). The name should be in a canonical form
   222  	// (e.g., leading "." is not accepted).
   223  	//
   224  	// In practice, teams usually precompile into the binary all types that they
   225  	// expect it to use in the context of Any. However, for URLs which use the
   226  	// scheme `http`, `https`, or no scheme, one can optionally set up a type
   227  	// server that maps type URLs to message definitions as follows:
   228  	//
   229  	//   - If no scheme is provided, `https` is assumed.
   230  	//   - An HTTP GET on the URL must yield a [google.protobuf.Type][]
   231  	//     value in binary format, or produce an error.
   232  	//   - Applications are allowed to cache lookup results based on the
   233  	//     URL, or have them precompiled into a binary to avoid any
   234  	//     lookup. Therefore, binary compatibility needs to be preserved
   235  	//     on changes to types. (Use versioned type names to manage
   236  	//     breaking changes.)
   237  	//
   238  	// Note: this functionality is not currently available in the official
   239  	// protobuf release, and it is not used for type URLs beginning with
   240  	// type.googleapis.com. As of May 2023, there are no widely used type server
   241  	// implementations and no plans to implement one.
   242  	//
   243  	// Schemes other than `http`, `https` (or the empty scheme) might be
   244  	// used with implementation specific semantics.
   245  	TypeUrl string `protobuf:"bytes,1,opt,name=type_url,json=typeUrl,proto3" json:"type_url,omitempty"`
   246  	// Must be a valid serialized protocol buffer of the above specified type.
   247  	Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
   248  }
   249  
   250  // New marshals src into a new Any instance.
   251  func New(src proto.Message) (*Any, error) {
   252  	dst := new(Any)
   253  	if err := dst.MarshalFrom(src); err != nil {
   254  		return nil, err
   255  	}
   256  	return dst, nil
   257  }
   258  
   259  // MarshalFrom marshals src into dst as the underlying message
   260  // using the provided marshal options.
   261  //
   262  // If no options are specified, call dst.MarshalFrom instead.
   263  func MarshalFrom(dst *Any, src proto.Message, opts proto.MarshalOptions) error {
   264  	const urlPrefix = "type.googleapis.com/"
   265  	if src == nil {
   266  		return protoimpl.X.NewError("invalid nil source message")
   267  	}
   268  	b, err := opts.Marshal(src)
   269  	if err != nil {
   270  		return err
   271  	}
   272  	dst.TypeUrl = urlPrefix + string(src.ProtoReflect().Descriptor().FullName())
   273  	dst.Value = b
   274  	return nil
   275  }
   276  
   277  // UnmarshalTo unmarshals the underlying message from src into dst
   278  // using the provided unmarshal options.
   279  // It reports an error if dst is not of the right message type.
   280  //
   281  // If no options are specified, call src.UnmarshalTo instead.
   282  func UnmarshalTo(src *Any, dst proto.Message, opts proto.UnmarshalOptions) error {
   283  	if src == nil {
   284  		return protoimpl.X.NewError("invalid nil source message")
   285  	}
   286  	if !src.MessageIs(dst) {
   287  		got := dst.ProtoReflect().Descriptor().FullName()
   288  		want := src.MessageName()
   289  		return protoimpl.X.NewError("mismatched message type: got %q, want %q", got, want)
   290  	}
   291  	return opts.Unmarshal(src.GetValue(), dst)
   292  }
   293  
   294  // UnmarshalNew unmarshals the underlying message from src into dst,
   295  // which is newly created message using a type resolved from the type URL.
   296  // The message type is resolved according to opt.Resolver,
   297  // which should implement protoregistry.MessageTypeResolver.
   298  // It reports an error if the underlying message type could not be resolved.
   299  //
   300  // If no options are specified, call src.UnmarshalNew instead.
   301  func UnmarshalNew(src *Any, opts proto.UnmarshalOptions) (dst proto.Message, err error) {
   302  	if src.GetTypeUrl() == "" {
   303  		return nil, protoimpl.X.NewError("invalid empty type URL")
   304  	}
   305  	if opts.Resolver == nil {
   306  		opts.Resolver = protoregistry.GlobalTypes
   307  	}
   308  	r, ok := opts.Resolver.(protoregistry.MessageTypeResolver)
   309  	if !ok {
   310  		return nil, protoregistry.NotFound
   311  	}
   312  	mt, err := r.FindMessageByURL(src.GetTypeUrl())
   313  	if err != nil {
   314  		if err == protoregistry.NotFound {
   315  			return nil, err
   316  		}
   317  		return nil, protoimpl.X.NewError("could not resolve %q: %v", src.GetTypeUrl(), err)
   318  	}
   319  	dst = mt.New().Interface()
   320  	return dst, opts.Unmarshal(src.GetValue(), dst)
   321  }
   322  
   323  // MessageIs reports whether the underlying message is of the same type as m.
   324  func (x *Any) MessageIs(m proto.Message) bool {
   325  	if m == nil {
   326  		return false
   327  	}
   328  	url := x.GetTypeUrl()
   329  	name := string(m.ProtoReflect().Descriptor().FullName())
   330  	if !strings.HasSuffix(url, name) {
   331  		return false
   332  	}
   333  	return len(url) == len(name) || url[len(url)-len(name)-1] == '/'
   334  }
   335  
   336  // MessageName reports the full name of the underlying message,
   337  // returning an empty string if invalid.
   338  func (x *Any) MessageName() protoreflect.FullName {
   339  	url := x.GetTypeUrl()
   340  	name := protoreflect.FullName(url)
   341  	if i := strings.LastIndexByte(url, '/'); i >= 0 {
   342  		name = name[i+len("/"):]
   343  	}
   344  	if !name.IsValid() {
   345  		return ""
   346  	}
   347  	return name
   348  }
   349  
   350  // MarshalFrom marshals m into x as the underlying message.
   351  func (x *Any) MarshalFrom(m proto.Message) error {
   352  	return MarshalFrom(x, m, proto.MarshalOptions{})
   353  }
   354  
   355  // UnmarshalTo unmarshals the contents of the underlying message of x into m.
   356  // It resets m before performing the unmarshal operation.
   357  // It reports an error if m is not of the right message type.
   358  func (x *Any) UnmarshalTo(m proto.Message) error {
   359  	return UnmarshalTo(x, m, proto.UnmarshalOptions{})
   360  }
   361  
   362  // UnmarshalNew unmarshals the contents of the underlying message of x into
   363  // a newly allocated message of the specified type.
   364  // It reports an error if the underlying message type could not be resolved.
   365  func (x *Any) UnmarshalNew() (proto.Message, error) {
   366  	return UnmarshalNew(x, proto.UnmarshalOptions{})
   367  }
   368  
   369  func (x *Any) Reset() {
   370  	*x = Any{}
   371  	if protoimpl.UnsafeEnabled {
   372  		mi := &file_google_protobuf_any_proto_msgTypes[0]
   373  		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
   374  		ms.StoreMessageInfo(mi)
   375  	}
   376  }
   377  
   378  func (x *Any) String() string {
   379  	return protoimpl.X.MessageStringOf(x)
   380  }
   381  
   382  func (*Any) ProtoMessage() {}
   383  
   384  func (x *Any) ProtoReflect() protoreflect.Message {
   385  	mi := &file_google_protobuf_any_proto_msgTypes[0]
   386  	if protoimpl.UnsafeEnabled && x != nil {
   387  		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
   388  		if ms.LoadMessageInfo() == nil {
   389  			ms.StoreMessageInfo(mi)
   390  		}
   391  		return ms
   392  	}
   393  	return mi.MessageOf(x)
   394  }
   395  
   396  // Deprecated: Use Any.ProtoReflect.Descriptor instead.
   397  func (*Any) Descriptor() ([]byte, []int) {
   398  	return file_google_protobuf_any_proto_rawDescGZIP(), []int{0}
   399  }
   400  
   401  func (x *Any) GetTypeUrl() string {
   402  	if x != nil {
   403  		return x.TypeUrl
   404  	}
   405  	return ""
   406  }
   407  
   408  func (x *Any) GetValue() []byte {
   409  	if x != nil {
   410  		return x.Value
   411  	}
   412  	return nil
   413  }
   414  
   415  var File_google_protobuf_any_proto protoreflect.FileDescriptor
   416  
   417  var file_google_protobuf_any_proto_rawDesc = []byte{
   418  	0x0a, 0x19, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75,
   419  	0x66, 0x2f, 0x61, 0x6e, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0f, 0x67, 0x6f, 0x6f,
   420  	0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x22, 0x36, 0x0a, 0x03,
   421  	0x41, 0x6e, 0x79, 0x12, 0x19, 0x0a, 0x08, 0x74, 0x79, 0x70, 0x65, 0x5f, 0x75, 0x72, 0x6c, 0x18,
   422  	0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x74, 0x79, 0x70, 0x65, 0x55, 0x72, 0x6c, 0x12, 0x14,
   423  	0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76,
   424  	0x61, 0x6c, 0x75, 0x65, 0x42, 0x76, 0x0a, 0x13, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x6f, 0x6f, 0x67,
   425  	0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x42, 0x08, 0x41, 0x6e, 0x79,
   426  	0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
   427  	0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
   428  	0x62, 0x75, 0x66, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x2f,
   429  	0x61, 0x6e, 0x79, 0x70, 0x62, 0xa2, 0x02, 0x03, 0x47, 0x50, 0x42, 0xaa, 0x02, 0x1e, 0x47, 0x6f,
   430  	0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x57, 0x65,
   431  	0x6c, 0x6c, 0x4b, 0x6e, 0x6f, 0x77, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72,
   432  	0x6f, 0x74, 0x6f, 0x33,
   433  }
   434  
   435  var (
   436  	file_google_protobuf_any_proto_rawDescOnce sync.Once
   437  	file_google_protobuf_any_proto_rawDescData = file_google_protobuf_any_proto_rawDesc
   438  )
   439  
   440  func file_google_protobuf_any_proto_rawDescGZIP() []byte {
   441  	file_google_protobuf_any_proto_rawDescOnce.Do(func() {
   442  		file_google_protobuf_any_proto_rawDescData = protoimpl.X.CompressGZIP(file_google_protobuf_any_proto_rawDescData)
   443  	})
   444  	return file_google_protobuf_any_proto_rawDescData
   445  }
   446  
   447  var file_google_protobuf_any_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
   448  var file_google_protobuf_any_proto_goTypes = []interface{}{
   449  	(*Any)(nil), // 0: google.protobuf.Any
   450  }
   451  var file_google_protobuf_any_proto_depIdxs = []int32{
   452  	0, // [0:0] is the sub-list for method output_type
   453  	0, // [0:0] is the sub-list for method input_type
   454  	0, // [0:0] is the sub-list for extension type_name
   455  	0, // [0:0] is the sub-list for extension extendee
   456  	0, // [0:0] is the sub-list for field type_name
   457  }
   458  
   459  func init() { file_google_protobuf_any_proto_init() }
   460  func file_google_protobuf_any_proto_init() {
   461  	if File_google_protobuf_any_proto != nil {
   462  		return
   463  	}
   464  	if !protoimpl.UnsafeEnabled {
   465  		file_google_protobuf_any_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
   466  			switch v := v.(*Any); i {
   467  			case 0:
   468  				return &v.state
   469  			case 1:
   470  				return &v.sizeCache
   471  			case 2:
   472  				return &v.unknownFields
   473  			default:
   474  				return nil
   475  			}
   476  		}
   477  	}
   478  	type x struct{}
   479  	out := protoimpl.TypeBuilder{
   480  		File: protoimpl.DescBuilder{
   481  			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
   482  			RawDescriptor: file_google_protobuf_any_proto_rawDesc,
   483  			NumEnums:      0,
   484  			NumMessages:   1,
   485  			NumExtensions: 0,
   486  			NumServices:   0,
   487  		},
   488  		GoTypes:           file_google_protobuf_any_proto_goTypes,
   489  		DependencyIndexes: file_google_protobuf_any_proto_depIdxs,
   490  		MessageInfos:      file_google_protobuf_any_proto_msgTypes,
   491  	}.Build()
   492  	File_google_protobuf_any_proto = out.File
   493  	file_google_protobuf_any_proto_rawDesc = nil
   494  	file_google_protobuf_any_proto_goTypes = nil
   495  	file_google_protobuf_any_proto_depIdxs = nil
   496  }
   497  

View as plain text