codec

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: Apache-2.0, Apache-2.0 Imports: 13 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSONIndent

func MarshalJSONIndent(cdc *Codec, obj interface{}) ([]byte, error)

attempt to make some pretty json

func MarshalYAML

func MarshalYAML(jsonMarshaler JSONMarshaler, toPrint proto.Message) ([]byte, error)

MarshalYAML marshals toPrint using jsonMarshaler to leverage specialized MarshalJSON methods (usually related to serialize data with protobuf or amin depending on a configuration). This involves additional roundtrip through JSON.

func MustMarshalJSONIndent

func MustMarshalJSONIndent(cdc *Codec, obj interface{}) []byte

MustMarshalJSONIndent executes MarshalJSONIndent except it panics upon failure.

func ProtoMarshalJSON

func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, error)

ProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded bytes of a message.

func RegisterCrypto

func RegisterCrypto(cdc *Codec)

Register the go-crypto to the codec

func RegisterEvidences

func RegisterEvidences(cdc *Codec)

RegisterEvidences registers Tendermint evidence types with the provided codec.

Types

type AminoMarshaler

type AminoMarshaler interface {
	MarshalAmino() ([]byte, error)
	UnmarshalAmino([]byte) error
	MarshalAminoJSON() ([]byte, error)
	UnmarshalAminoJSON([]byte) error
}

AminoMarshaler defines an interface where Amino marshalling can be overridden by custom marshalling.

type BinaryCodec

type BinaryCodec interface {
	// Marshal returns binary encoding of v.
	Marshal(o ProtoMarshaler) ([]byte, error)
	// MustMarshal calls Marshal and panics if error is returned.
	MustMarshal(o ProtoMarshaler) []byte

	// MarshalLengthPrefixed returns binary encoding of v with bytes length prefix.
	MarshalLengthPrefixed(o ProtoMarshaler) ([]byte, error)
	// MustMarshalLengthPrefixed calls MarshalLengthPrefixed and panics if
	// error is returned.
	MustMarshalLengthPrefixed(o ProtoMarshaler) []byte

	// Unmarshal parses the data encoded with Marshal method and stores the result
	// in the value pointed to by v.
	Unmarshal(bz []byte, ptr ProtoMarshaler) error
	// MustUnmarshal calls Unmarshal and panics if error is returned.
	MustUnmarshal(bz []byte, ptr ProtoMarshaler)

	// Unmarshal parses the data encoded with UnmarshalLengthPrefixed method and stores
	// the result in the value pointed to by v.
	UnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
	// MustUnmarshalLengthPrefixed calls UnmarshalLengthPrefixed and panics if error
	// is returned.
	MustUnmarshalLengthPrefixed(bz []byte, ptr ProtoMarshaler)

	// MarshalInterface is a helper method which will wrap `i` into `Any` for correct
	// binary interface (de)serialization.
	MarshalInterface(i proto.Message) ([]byte, error)
	// UnmarshalInterface is a helper method which will parse binary enoded data
	// into `Any` and unpack any into the `ptr`. It fails if the target interface type
	// is not registered in codec, or is not compatible with the serialized data
	UnmarshalInterface(bz []byte, ptr interface{}) error

	types.AnyUnpacker
}

type BinaryMarshaler

type BinaryMarshaler interface {
	MarshalBinaryBare(o ProtoMarshaler) ([]byte, error)
	MustMarshalBinaryBare(o ProtoMarshaler) []byte

	MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error)
	MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte

	UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error
	MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler)

	UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
	MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler)

	MarshalInterface(i proto.Message) ([]byte, error)
	UnmarshalInterface(bz []byte, ptr interface{}) error

	types.AnyUnpacker
}

type CdcAbstraction

type CdcAbstraction interface {
	UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller(bz []byte, ptr interface{}) (interface{}, error)
	UnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) error
}

type Codec

type Codec = amino.Codec

amino codec to marshal/unmarshal

var Cdc *Codec

Cdc generic sealed codec to be used throughout sdk

func New

func New() *Codec

type CodecProxy

type CodecProxy struct {
	// contains filtered or unexported fields
}

func NewCodecProxy

func NewCodecProxy(protoCodec *ProtoCodec, cdc *Codec) *CodecProxy

func (*CodecProxy) GetCdc

func (mp *CodecProxy) GetCdc() *Codec

func (*CodecProxy) GetProtocMarshal

func (mp *CodecProxy) GetProtocMarshal() *ProtoCodec

func (*CodecProxy) UnmarshalBinaryLengthPrefixed

func (mp *CodecProxy) UnmarshalBinaryLengthPrefixed(bz []byte, ptr interface{}) error

func (*CodecProxy) UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller

func (mp *CodecProxy) UnmarshalBinaryLengthPrefixedWithRegisteredUbmarshaller(bz []byte, ptr interface{}) (interface{}, error)

type IbcCodec

type IbcCodec interface {
	BinaryCodec
	JSONCodec
}

type JSONCodec

type JSONCodec interface {
	// MarshalJSON returns JSON encoding of v.
	MarshalJSON(o proto.Message) ([]byte, error)
	// MustMarshalJSON calls MarshalJSON and panics if error is returned.
	MustMarshalJSON(o proto.Message) []byte
	// MarshalInterfaceJSON is a helper method which will wrap `i` into `Any` for correct
	// JSON interface (de)serialization.
	MarshalInterfaceJSON(i proto.Message) ([]byte, error)
	// UnmarshalInterfaceJSON is a helper method which will parse JSON enoded data
	// into `Any` and unpack any into the `ptr`. It fails if the target interface type
	// is not registered in codec, or is not compatible with the serialized data
	UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error

	// UnmarshalJSON parses the data encoded with MarshalJSON method and stores the result
	// in the value pointed to by v.
	UnmarshalJSON(bz []byte, ptr proto.Message) error
	// MustUnmarshalJSON calls Unmarshal and panics if error is returned.
	MustUnmarshalJSON(bz []byte, ptr proto.Message)
}

type JSONMarshaler

type JSONMarshaler interface {
	MarshalJSON(o proto.Message) ([]byte, error)
	MustMarshalJSON(o proto.Message) []byte
	MarshalInterfaceJSON(i proto.Message) ([]byte, error)
	UnmarshalInterfaceJSON(bz []byte, ptr interface{}) error

	UnmarshalJSON(bz []byte, ptr proto.Message) error
	MustUnmarshalJSON(bz []byte, ptr proto.Message)
}

type Marshaler

type Marshaler interface {
	BinaryMarshaler
	JSONMarshaler
}

Marshaler defines the interface module codecs must implement in order to support backwards compatibility with Amino while allowing custom Protobuf-based serialization. Note, Amino can still be used without any dependency on Protobuf. There are two typical implementations that fulfill this contract:

1. AminoCodec: Provides full Amino serialization compatibility. 2. ProtoCodec: Provides full Protobuf serialization compatibility.

type ProtoCodec

type ProtoCodec struct {
	// contains filtered or unexported fields
}

ProtoCodec defines a codec that utilizes Protobuf for both binary and JSON encoding.

func NewProtoCodec

func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec

NewProtoCodec returns a reference to a new ProtoCodec

func (*ProtoCodec) InterfaceRegistry

func (pc *ProtoCodec) InterfaceRegistry() types.InterfaceRegistry

func (*ProtoCodec) Marshal

func (pc *ProtoCodec) Marshal(o ProtoMarshaler) ([]byte, error)

Marshal implements BinaryMarshaler.Marshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.MarshalInterface

func (*ProtoCodec) MarshalBinaryBare

func (pc *ProtoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error)

MarshalBinaryBare implements BinaryMarshaler.MarshalBinaryBare method.

func (*ProtoCodec) MarshalBinaryLengthPrefixed

func (pc *ProtoCodec) MarshalBinaryLengthPrefixed(o ProtoMarshaler) ([]byte, error)

MarshalBinaryLengthPrefixed implements BinaryMarshaler.MarshalBinaryLengthPrefixed method.

func (*ProtoCodec) MarshalInterface

func (pc *ProtoCodec) MarshalInterface(i proto.Message) ([]byte, error)

MarshalInterface is a convenience function for proto marshalling interfaces. It packs the provided value, which must be an interface, in an Any and then marshals it to bytes. NOTE: to marshal a concrete type, you should use MarshalBinaryBare instead

func (*ProtoCodec) MarshalInterfaceJSON

func (pc *ProtoCodec) MarshalInterfaceJSON(x proto.Message) ([]byte, error)

MarshalInterfaceJSON is a convenience function for proto marshalling interfaces. It packs the provided value in an Any and then marshals it to bytes. NOTE: to marshal a concrete type, you should use MarshalJSON instead

func (*ProtoCodec) MarshalJSON

func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error)

MarshalJSON implements JSONMarshaler.MarshalJSON method, it marshals to JSON using proto codec.

func (*ProtoCodec) MustMarshal

func (pc *ProtoCodec) MustMarshal(o ProtoMarshaler) []byte

MustMarshal implements BinaryMarshaler.MustMarshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.MarshalInterface

func (*ProtoCodec) MustMarshalBinaryBare

func (pc *ProtoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte

MustMarshalBinaryBare implements BinaryMarshaler.MustMarshalBinaryBare method.

func (*ProtoCodec) MustMarshalBinaryLengthPrefixed

func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte

MustMarshalBinaryLengthPrefixed implements BinaryMarshaler.MustMarshalBinaryLengthPrefixed method.

func (*ProtoCodec) MustMarshalJSON

func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte

MustMarshalJSON implements JSONMarshaler.MustMarshalJSON method, it executes MarshalJSON except it panics upon failure.

func (*ProtoCodec) MustUnmarshal

func (pc *ProtoCodec) MustUnmarshal(bz []byte, ptr ProtoMarshaler)

MustUnmarshal implements BinaryMarshaler.MustUnmarshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.UnmarshalInterface

func (*ProtoCodec) MustUnmarshalBinaryBare

func (pc *ProtoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler)

MustUnmarshalBinaryBare implements BinaryMarshaler.MustUnmarshalBinaryBare method.

func (*ProtoCodec) MustUnmarshalBinaryLengthPrefixed

func (pc *ProtoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler)

MustUnmarshalBinaryLengthPrefixed implements BinaryMarshaler.MustUnmarshalBinaryLengthPrefixed method.

func (*ProtoCodec) MustUnmarshalJSON

func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message)

MustUnmarshalJSON implements JSONMarshaler.MustUnmarshalJSON method, it executes UnmarshalJSON except it panics upon failure.

func (*ProtoCodec) Unmarshal

func (pc *ProtoCodec) Unmarshal(bz []byte, ptr ProtoMarshaler) error

Unmarshal implements BinaryMarshaler.Unmarshal method. NOTE: this function must be used with a concrete type which implements proto.Message. For interface please use the codec.UnmarshalInterface

func (*ProtoCodec) UnmarshalBinaryBare

func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error

UnmarshalBinaryBare implements BinaryMarshaler.UnmarshalBinaryBare method.

func (*ProtoCodec) UnmarshalBinaryLengthPrefixed

func (pc *ProtoCodec) UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error

UnmarshalBinaryLengthPrefixed implements BinaryMarshaler.UnmarshalBinaryLengthPrefixed method.

func (*ProtoCodec) UnmarshalInterface

func (pc *ProtoCodec) UnmarshalInterface(bz []byte, ptr interface{}) error

UnmarshalInterface is a convenience function for proto unmarshaling interfaces. It unmarshals an Any from bz bytes and then unpacks it to the `ptr`, which must be a pointer to a non empty interface with registered implementations. NOTE: to unmarshal a concrete type, you should use UnmarshalBinaryBare instead

Example:

var x MyInterface
err := cdc.UnmarshalInterface(bz, &x)

func (*ProtoCodec) UnmarshalInterfaceJSON

func (pc *ProtoCodec) UnmarshalInterfaceJSON(bz []byte, iface interface{}) error

UnmarshalInterfaceJSON is a convenience function for proto unmarshaling interfaces. It unmarshals an Any from bz bytes and then unpacks it to the `iface`, which must be a pointer to a non empty interface, implementing proto.Message with registered implementations. NOTE: to unmarshal a concrete type, you should use UnmarshalJSON instead

Example:

var x MyInterface  // must implement proto.Message
err := cdc.UnmarshalInterfaceJSON(&x, bz)

func (*ProtoCodec) UnmarshalJSON

func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error

UnmarshalJSON implements JSONMarshaler.UnmarshalJSON method, it unmarshals from JSON using proto codec.

func (*ProtoCodec) UnpackAny

func (pc *ProtoCodec) UnpackAny(any *types.Any, iface interface{}) error

UnpackAny implements AnyUnpacker.UnpackAny method, it unpacks the value in any to the interface pointer passed in as iface.

type ProtoCodecMarshaler

type ProtoCodecMarshaler interface {
	Marshaler
	InterfaceRegistry() types.InterfaceRegistry
}

ProtoCodecMarshaler defines an interface for codecs that utilize Protobuf for both binary and JSON encoding.

type ProtoMarshaler

type ProtoMarshaler interface {
	proto.Message // for JSON serialization

	Marshal() ([]byte, error)
	MarshalTo(data []byte) (n int, err error)
	MarshalToSizedBuffer(dAtA []byte) (int, error)
	Size() int
	Unmarshal(data []byte) error
}

ProtoMarshaler defines an interface a type must implement as protocol buffer defined message.

Directories

Path Synopsis
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
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

Jump to

Keyboard shortcuts

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