types

package
v0.41.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package types defines a custom wrapper for google.protobuf.Any which supports cached values as well as InterfaceRegistry which keeps track of types which can be used with Any for both security and introspection

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthAny        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowAny          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupAny = fmt.Errorf("proto: unexpected end of group")
)
View Source
var Debug = true

Functions

func UnpackInterfaces

func UnpackInterfaces(x interface{}, unpacker AnyUnpacker) error

UnpackInterfaces is a convenience function that calls UnpackInterfaces on x if x implements UnpackInterfacesMessage

Types

type AminoJSONPacker

type AminoJSONPacker struct {
	Cdc *amino.Codec
}

AminoUnpacker is an AnyUnpacker provided for backwards compatibility with amino for the JSON un-marshaling phase

func (AminoJSONPacker) UnpackAny

func (a AminoJSONPacker) UnpackAny(any *Any, _ interface{}) error

type AminoJSONUnpacker

type AminoJSONUnpacker struct {
	Cdc *amino.Codec
}

AminoUnpacker is an AnyUnpacker provided for backwards compatibility with amino for the JSON marshaling phase

func (AminoJSONUnpacker) UnpackAny

func (a AminoJSONUnpacker) UnpackAny(any *Any, iface interface{}) error

type AminoPacker

type AminoPacker struct {
	Cdc *amino.Codec
}

AminoUnpacker is an AnyUnpacker provided for backwards compatibility with amino for the binary marshaling phase

func (AminoPacker) UnpackAny

func (a AminoPacker) UnpackAny(any *Any, _ interface{}) error

type AminoUnpacker

type AminoUnpacker struct {
	Cdc *amino.Codec
}

AminoUnpacker is an AnyUnpacker provided for backwards compatibility with amino for the binary un-marshaling phase

func (AminoUnpacker) UnpackAny

func (a AminoUnpacker) UnpackAny(any *Any, iface interface{}) error

type Any

type Any struct {

	// nolint
	TypeUrl string `protobuf:"bytes,1,opt,name=type_url,json=typeUrl,proto3" json:"type_url,omitempty"`
	// Must be a valid serialized protocol buffer of the above specified type.
	Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`

	// nolint
	XXX_NoUnkeyedLiteral struct{} `json:"-"`

	// nolint
	XXX_unrecognized []byte `json:"-"`

	// nolint
	XXX_sizecache int32 `json:"-"`
	// contains filtered or unexported fields
}

func NewAnyWithCustomTypeURL

func NewAnyWithCustomTypeURL(v proto.Message, typeURL string) (*Any, error)

NewAnyWithCustomTypeURL same as NewAnyWithValue, but sets a custom type url, instead using the one from proto.Message. NOTE: This functions should be only used for types with additional logic bundled into the protobuf Any serialization. For simple marshaling you should use NewAnyWithValue.

func NewAnyWithValue

func NewAnyWithValue(v proto.Message) (*Any, error)

NewAnyWithValue constructs a new Any packed with the value provided or returns an error if that value couldn't be packed. This also caches the packed value so that it can be retrieved from GetCachedValue without unmarshaling

func NewPopulatedAny

func NewPopulatedAny(r randyAny, easy bool) *Any

func UnsafePackAny

func UnsafePackAny(x interface{}) *Any

UnsafePackAny packs the value x in the Any and instead of returning the error in the case of a packing failure, keeps the cached value. This should only be used in situations where compatibility is needed with amino. Amino-only values can safely be packed using this method when they will only be marshaled with amino and not protobuf.

func (*Any) ClearCachedValue

func (any *Any) ClearCachedValue()

ClearCachedValue clears the cached value from the Any

func (*Any) Compare

func (this *Any) Compare(that interface{}) int

func (*Any) Descriptor

func (*Any) Descriptor() ([]byte, []int)

func (*Any) Equal

func (this *Any) Equal(that interface{}) bool

func (*Any) GetCachedValue

func (any *Any) GetCachedValue() interface{}

GetCachedValue returns the cached value from the Any if present

func (*Any) GetTypeUrl

func (m *Any) GetTypeUrl() string

func (*Any) GetValue

func (m *Any) GetValue() []byte

func (*Any) GoString

func (this *Any) GoString() string

func (*Any) Marshal

func (m *Any) Marshal() (dAtA []byte, err error)

func (Any) MarshalAmino

func (any Any) MarshalAmino() ([]byte, error)

func (*Any) MarshalJSON

func (any *Any) MarshalJSON() ([]byte, error)

func (*Any) MarshalTo

func (m *Any) MarshalTo(dAtA []byte) (int, error)

func (*Any) MarshalToSizedBuffer

func (m *Any) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*Any) ProtoMessage

func (*Any) ProtoMessage()

func (*Any) Reset

func (m *Any) Reset()

func (*Any) Size

func (m *Any) Size() (n int)

func (*Any) String

func (this *Any) String() string

func (*Any) Unmarshal

func (m *Any) Unmarshal(dAtA []byte) error

func (*Any) UnmarshalAmino

func (any *Any) UnmarshalAmino(bz []byte) error

func (*Any) UnmarshalJSON

func (any *Any) UnmarshalJSON(bz []byte) error

func (*Any) XXX_DiscardUnknown

func (m *Any) XXX_DiscardUnknown()

func (*Any) XXX_Marshal

func (m *Any) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Any) XXX_Merge

func (m *Any) XXX_Merge(src proto.Message)

func (*Any) XXX_MessageName

func (*Any) XXX_MessageName() string

func (*Any) XXX_Size

func (m *Any) XXX_Size() int

func (*Any) XXX_Unmarshal

func (m *Any) XXX_Unmarshal(b []byte) error

func (*Any) XXX_WellKnownType

func (*Any) XXX_WellKnownType() string

type AnyUnpacker

type AnyUnpacker interface {
	// UnpackAny unpacks the value in any to the interface pointer passed in as
	// iface. Note that the type in any must have been registered in the
	// underlying whitelist registry as a concrete type for that interface
	// Ex:
	//    var msg sdk.Msg
	//    err := cdc.UnpackAny(any, &msg)
	//    ...
	UnpackAny(any *Any, iface interface{}) error
}

AnyUnpacker is an interface which allows safely unpacking types packed in Any's against a whitelist of registered types

type InterfaceRegistry

type InterfaceRegistry interface {
	AnyUnpacker
	jsonpb.AnyResolver

	// RegisterInterface associates protoName as the public name for the
	// interface passed in as iface. This is to be used primarily to create
	// a public facing registry of interface implementations for clients.
	// protoName should be a well-chosen public facing name that remains stable.
	// RegisterInterface takes an optional list of impls to be registered
	// as implementations of iface.
	//
	// Ex:
	//   registry.RegisterInterface("cosmos.base.v1beta1.Msg", (*sdk.Msg)(nil))
	RegisterInterface(protoName string, iface interface{}, impls ...proto.Message)

	// RegisterImplementations registers impls as concrete implementations of
	// the interface iface.
	//
	// Ex:
	//  registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSend{}, &MsgMultiSend{})
	RegisterImplementations(iface interface{}, impls ...proto.Message)

	// RegisterCustomTypeURL allows a protobuf message to be registered as a
	// google.protobuf.Any with a custom typeURL (besides its own canonical
	// typeURL). iface should be an interface as type, as in RegisterInterface
	// and RegisterImplementations.
	//
	// Ex:
	// This will allow us to pack service methods in Any's using the full method name
	// as the type URL and the request body as the value, and allow us to unpack
	// such packed methods using the normal UnpackAny method for the interface iface.
	RegisterCustomTypeURL(iface interface{}, typeURL string, impl proto.Message)

	// ListAllInterfaces list the type URLs of all registered interfaces.
	ListAllInterfaces() []string

	// ListImplementations lists the valid type URLs for the given interface name that can be used
	// for the provided interface type URL.
	ListImplementations(ifaceTypeURL string) []string
}

InterfaceRegistry provides a mechanism for registering interfaces and implementations that can be safely unpacked from Any

func NewInterfaceRegistry

func NewInterfaceRegistry() InterfaceRegistry

NewInterfaceRegistry returns a new InterfaceRegistry

type ProtoJSONPacker

type ProtoJSONPacker struct {
	JSONPBMarshaler *jsonpb.Marshaler
}

ProtoJSONPacker is an AnyUnpacker provided for compatibility with jsonpb

func (ProtoJSONPacker) UnpackAny

func (a ProtoJSONPacker) UnpackAny(any *Any, _ interface{}) error

type UnpackInterfacesMessage

type UnpackInterfacesMessage interface {
	// UnpackInterfaces is implemented in order to unpack values packed within
	// Any's using the AnyUnpacker. It should generally be implemented as
	// follows:
	//   func (s *MyStruct) UnpackInterfaces(unpacker AnyUnpacker) error {
	//		var x AnyInterface
	//		// where X is an Any field on MyStruct
	//		err := unpacker.UnpackAny(s.X, &x)
	//		if err != nil {
	//			return nil
	//		}
	//		// where Y is a field on MyStruct that implements UnpackInterfacesMessage itself
	//		err = s.Y.UnpackInterfaces(unpacker)
	//		if err != nil {
	//			return nil
	//		}
	//		return nil
	//	 }
	UnpackInterfaces(unpacker AnyUnpacker) error
}

UnpackInterfacesMessage is meant to extend protobuf types (which implement proto.Message) to support a post-deserialization phase which unpacks types packed within Any's using the whitelist provided by AnyUnpacker

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL