Documentation ¶
Overview ¶
TODO AMINO DOCS, for now see the main README file, or the code.
Example ¶
package main import ( "fmt" "reflect" amino "github.com/tendermint/go-amino-x" ) func main() { type Message interface{} type bcMessage struct { Message string Height int } type bcResponse struct { Status int Message string } type bcStatus struct { Peers int } // amino.RegisterPackage registers globally. amino.RegisterPackage( amino.NewPackage( reflect.TypeOf(bcMessage{}).PkgPath(), "amino_test", amino.GetCallersDirname(), ). WithTypes(&bcMessage{}, &bcResponse{}, &bcStatus{}), ) var bm = &bcMessage{Message: "ABC", Height: 100} var msg = bm var bz []byte // the marshalled bytes. var err error bz, err = amino.MarshalAnyLengthPrefixed(msg) fmt.Printf("Encoded: %X (err: %v)\n", bz, err) var msg2 Message err = amino.UnmarshalLengthPrefixed(bz, &msg2) fmt.Printf("Decoded: %v (err: %v)\n", msg2, err) var bm2 = msg2.(*bcMessage) fmt.Printf("Decoded successfully: %v\n", *bm == *bm2) }
Output: Encoded: 210A152F616D696E6F5F746573742E62634D65737361676512080A0341424310C801 (err: <nil>) Decoded: &{ABC 100} (err: <nil>) Decoded successfully: true
Index ¶
- Constants
- Variables
- func ByteSliceSize(bz []byte) int
- func DecodeBool(bz []byte) (b bool, n int, err error)
- func DecodeByte(bz []byte) (b byte, n int, err error)
- func DecodeByteSlice(bz []byte) (bz2 []byte, n int, err error)
- func DecodeDuration(bz []byte) (d time.Duration, n int, err error)
- func DecodeDurationValue(bz []byte) (s int64, ns int32, n int, err error)
- func DecodeFloat32(bz []byte) (f float32, n int, err error)
- func DecodeFloat64(bz []byte) (f float64, n int, err error)
- func DecodeInt32(bz []byte) (i int32, n int, err error)
- func DecodeInt64(bz []byte) (i int64, n int, err error)
- func DecodeJSONDuration(bz []byte, fopts FieldOptions) (d time.Duration, err error)
- func DecodeJSONPBDuration(bz []byte, fopts FieldOptions) (d durationpb.Duration, err error)
- func DecodeJSONPBTimestamp(bz []byte, fopts FieldOptions) (t timestamppb.Timestamp, err error)
- func DecodeJSONTime(bz []byte, fopts FieldOptions) (t time.Time, err error)
- func DecodeString(bz []byte) (s string, n int, err error)
- func DecodeTime(bz []byte) (t time.Time, n int, err error)
- func DecodeTimeValue(bz []byte) (s int64, ns int32, n int, err error)
- func DecodeUint32(bz []byte) (u uint32, n int, err error)
- func DecodeUint64(bz []byte) (u uint64, n int, err error)
- func DecodeUvarint(bz []byte) (u uint64, n int, err error)
- func DecodeUvarint16(bz []byte) (u uint16, n int, err error)
- func DecodeUvarint8(bz []byte) (u uint8, n int, err error)
- func DecodeVarint(bz []byte) (i int64, n int, err error)
- func DecodeVarint16(bz []byte) (i int16, n int, err error)
- func DecodeVarint8(bz []byte) (i int8, n int, err error)
- func DeepCopy(o interface{}) (r interface{})
- func DeepEqual(a, b interface{}) bool
- func EncodeBool(w io.Writer, b bool) (err error)
- func EncodeByte(w io.Writer, b byte) (err error)
- func EncodeByteSlice(w io.Writer, bz []byte) (err error)
- func EncodeDuration(w io.Writer, d time.Duration) (err error)
- func EncodeDurationValue(w io.Writer, s int64, ns int32) (err error)
- func EncodeFloat32(w io.Writer, f float32) (err error)
- func EncodeFloat64(w io.Writer, f float64) (err error)
- func EncodeInt32(w io.Writer, i int32) (err error)
- func EncodeInt64(w io.Writer, i int64) (err error)
- func EncodeJSONDuration(w io.Writer, d time.Duration) (err error)
- func EncodeJSONDurationValue(w io.Writer, s int64, ns int32) (err error)
- func EncodeJSONPBDuration(w io.Writer, d durationpb.Duration) (err error)
- func EncodeJSONPBTimestamp(w io.Writer, t timestamppb.Timestamp) (err error)
- func EncodeJSONTime(w io.Writer, t time.Time) (err error)
- func EncodeJSONTimeValue(w io.Writer, s int64, ns int32) (err error)
- func EncodeString(w io.Writer, s string) (err error)
- func EncodeTime(w io.Writer, t time.Time) (err error)
- func EncodeTimeValue(w io.Writer, s int64, ns int32) (err error)
- func EncodeUint32(w io.Writer, u uint32) (err error)
- func EncodeUint64(w io.Writer, u uint64) (err error)
- func EncodeUvarint(w io.Writer, u uint64) (err error)
- func EncodeVarint(w io.Writer, i int64) (err error)
- func GetCallersDirname() string
- func GetTypeURL(o interface{}) string
- func IsASCIIText(s string) bool
- func IsEmptyTime(t time.Time) bool
- func Marshal(o interface{}) ([]byte, error)
- func MarshalAny(o interface{}) ([]byte, error)
- func MarshalAnyLengthPrefixed(o interface{}) ([]byte, error)
- func MarshalJSON(o interface{}) ([]byte, error)
- func MarshalJSONAny(o interface{}) ([]byte, error)
- func MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error)
- func MarshalLengthPrefixed(o interface{}) ([]byte, error)
- func MarshalLengthPrefixedWriter(w io.Writer, o interface{}) (n int64, err error)
- func MustMarshal(o interface{}) []byte
- func MustMarshalAny(o interface{}) []byte
- func MustMarshalAnyLengthPrefixed(o interface{}) []byte
- func MustMarshalLengthPrefixed(o interface{}) []byte
- func MustUnmarshal(bz []byte, ptr interface{})
- func MustUnmarshalAny(typeURL string, value []byte, ptr interface{})
- func MustUnmarshalLengthPrefixed(bz []byte, ptr interface{})
- func Unmarshal(bz []byte, ptr interface{}) error
- func UnmarshalAny(typeURL string, value []byte, ptr interface{}) error
- func UnmarshalJSON(bz []byte, ptr interface{}) error
- func UnmarshalLengthPrefixed(bz []byte, ptr interface{}) error
- func UnmarshalLengthPrefixedReader(r io.Reader, ptr interface{}, maxSize int64) (n int64, err error)
- func UvarintSize(u uint64) int
- func VarintSize(i int64) int
- type Codec
- func (cdc *Codec) Autoseal() *Codec
- func (cdc *Codec) GetPackages() pkg.PackageSet
- func (cdc *Codec) GetTypeInfo(rt reflect.Type) (info *TypeInfo, err error)
- func (cdc *Codec) GetTypeURL(o interface{}) string
- func (cdc *Codec) Marshal(o interface{}) ([]byte, error)
- func (cdc *Codec) MarshalAny(o interface{}) ([]byte, error)
- func (cdc *Codec) MarshalAnyLengthPrefixed(o interface{}) ([]byte, error)
- func (cdc *Codec) MarshalJSON(o interface{}) ([]byte, error)
- func (cdc *Codec) MarshalJSONAny(o interface{}) ([]byte, error)
- func (cdc *Codec) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error)
- func (cdc *Codec) MarshalLengthPrefixed(o interface{}) ([]byte, error)
- func (cdc *Codec) MarshalLengthPrefixedWriter(w io.Writer, o interface{}) (n int64, err error)
- func (cdc *Codec) MarshalPBBindings(pbm PBMessager) ([]byte, error)
- func (cdc *Codec) MarshalReflect(o interface{}) ([]byte, error)
- func (cdc *Codec) MustMarshal(o interface{}) []byte
- func (cdc *Codec) MustMarshalAny(o interface{}) []byte
- func (cdc *Codec) MustMarshalAnyLengthPrefixed(o interface{}) []byte
- func (cdc *Codec) MustMarshalJSON(o interface{}) []byte
- func (cdc *Codec) MustMarshalLengthPrefixed(o interface{}) []byte
- func (cdc *Codec) MustUnmarshal(bz []byte, ptr interface{})
- func (cdc *Codec) MustUnmarshalAny(typeURL string, value []byte, ptr interface{})
- func (cdc *Codec) MustUnmarshalJSON(bz []byte, ptr interface{})
- func (cdc *Codec) MustUnmarshalLengthPrefixed(bz []byte, ptr interface{})
- func (cdc *Codec) PrintTypes(out io.Writer) error
- func (cdc *Codec) RegisterPackage(pkg *Package)
- func (cdc *Codec) RegisterTypeFrom(rt reflect.Type, pkg *Package)
- func (cdc *Codec) Seal() *Codec
- func (cdc *Codec) Unmarshal(bz []byte, ptr interface{}) error
- func (cdc *Codec) UnmarshalAny(typeURL string, value []byte, ptr interface{}) (err error)
- func (cdc *Codec) UnmarshalJSON(bz []byte, ptr interface{}) error
- func (cdc *Codec) UnmarshalLengthPrefixed(bz []byte, ptr interface{}) error
- func (cdc *Codec) UnmarshalLengthPrefixedReader(r io.Reader, ptr interface{}, maxSize int64) (n int64, err error)
- func (cdc *Codec) WithPBBindings() *Codec
- type ConcreteInfo
- type FieldInfo
- type FieldOptions
- type InterfaceInfo
- type InvalidDurationErr
- type InvalidTimeErr
- type Object
- type PBMessager
- type Package
- type StructInfo
- type Typ3
- type Type
- type TypeInfo
Examples ¶
Constants ¶
const ( // Typ3 types Typ3Varint = Typ3(0) Typ38Byte = Typ3(1) Typ3ByteLength = Typ3(2) //Typ3_Struct = Typ3(3) //Typ3_StructTerm = Typ3(4) Typ34Byte = Typ3(5) )
const Version = "1.0.0-rc.6"
Version
Variables ¶
var ( // ErrNoPointer is thrown when you call a method that expects a pointer, e.g. Unmarshal ErrNoPointer = errors.New("expected a pointer") )
var (
ErrOverflowInt = errors.New("encoded integer value overflows int(32)")
)
Functions ¶
func ByteSliceSize ¶
func DecodeDurationValue ¶
func DecodeJSONDuration ¶
func DecodeJSONDuration(bz []byte, fopts FieldOptions) (d time.Duration, err error)
func DecodeJSONPBDuration ¶
func DecodeJSONPBDuration(bz []byte, fopts FieldOptions) (d durationpb.Duration, err error)
func DecodeJSONPBTimestamp ¶
func DecodeJSONPBTimestamp(bz []byte, fopts FieldOptions) (t timestamppb.Timestamp, err error)
func DecodeJSONTime ¶
func DecodeJSONTime(bz []byte, fopts FieldOptions) (t time.Time, err error)
func DecodeTimeValue ¶
DecodeTimeValue decodes seconds (int64) and nanoseconds (int32) since January 1, 1970 UTC, and returns the corresponding time. If nanoseconds is not in the range [0, 999999999], or if seconds is too large, an error is returned.
func DeepCopy ¶
func DeepCopy(o interface{}) (r interface{})
Deeply copies an object. If anything implements `.DeepCopy() <any>` along the way, the result of that function will be used. Otherwise, if it implements `.MarshalAmino() (<any>, error)` and `.UnmarshalAmino(<any>) error`, the pair will be used to copy. If .MarshalAmino() or .UnmarshalAmino() returns an error, this function will panic.
func DeepEqual ¶
func DeepEqual(a, b interface{}) bool
DeepEqual returns true if the types are the same and the binary amino encoding would be the same. TODO: optimize, and support genproto.
func EncodeByte ¶
Unlike EncodeUint8, writes a single byte.
func EncodeDurationValue ¶
The binary encoding of Duration is the same as Timestamp, but the validation checks are different.
See https://godoc.org/google.golang.org/protobuf/types/known/durationpb#Duration
type Duration struct {
// Signed seconds of the span of time. Must be from -315,576,000,000 // to +315,576,000,000 inclusive. Note: these bounds are computed from: // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"` // Signed fractions of a second at nanosecond resolution of the span // of time. Durations less than one second are represented with a 0 // `seconds` field and a positive or negative `nanos` field. For durations // of one second or more, a non-zero value for the `nanos` field must be // of the same sign as the `seconds` field. Must be from -999,999,999 // to +999,999,999 inclusive. Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"` // contains filtered or unexported fields }
func EncodeJSONDurationValue ¶
func EncodeJSONPBDuration ¶
func EncodeJSONPBDuration(w io.Writer, d durationpb.Duration) (err error)
func EncodeJSONPBTimestamp ¶
func EncodeJSONPBTimestamp(w io.Writer, t timestamppb.Timestamp) (err error)
func EncodeTimeValue ¶
EncodeTimeValue writes the number of seconds (int64) and nanoseconds (int32), with millisecond resolution since January 1, 1970 UTC to the Writer as an UInt64. Milliseconds are used to ease compatibility with Javascript, which does not support finer resolution.
See https://godoc.org/google.golang.org/protobuf/types/known/timestamppb#Timestamp
type Timestamp struct {
// Represents seconds of UTC time since Unix epoch // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to // 9999-12-31T23:59:59Z inclusive. Seconds int64 `protobuf:"varint,1,opt,name=seconds,proto3" json:"seconds,omitempty"` // Non-negative fractions of a second at nanosecond resolution. Negative // second values with fractions must still have non-negative nanos values // that count forward in time. Must be from 0 to 999,999,999 // inclusive. Nanos int32 `protobuf:"varint,2,opt,name=nanos,proto3" json:"nanos,omitempty"` // contains filtered or unexported fields }
func IsASCIIText ¶
Returns true if s is a non-empty printable non-tab ascii character.
func IsEmptyTime ¶
func MarshalAny ¶
func MarshalJSON ¶
func MarshalJSONAny ¶
func MarshalJSONIndent ¶
func MarshalLengthPrefixed ¶
func MustMarshal ¶
func MustMarshal(o interface{}) []byte
func MustMarshalAny ¶
func MustMarshalAny(o interface{}) []byte
func MustMarshalAnyLengthPrefixed ¶
func MustMarshalAnyLengthPrefixed(o interface{}) []byte
func MustMarshalLengthPrefixed ¶
func MustMarshalLengthPrefixed(o interface{}) []byte
func MustUnmarshal ¶
func MustUnmarshal(bz []byte, ptr interface{})
func MustUnmarshalAny ¶
func MustUnmarshalLengthPrefixed ¶
func MustUnmarshalLengthPrefixed(bz []byte, ptr interface{})
func UnmarshalAny ¶
func UnmarshalJSON ¶
func UnmarshalLengthPrefixed ¶
func UvarintSize ¶
func VarintSize ¶
Types ¶
type Codec ¶
type Codec struct {
// contains filtered or unexported fields
}
func (*Codec) GetPackages ¶
func (cdc *Codec) GetPackages() pkg.PackageSet
XXX TODO: make this safe so modifications don't affect runtime codec, and ensure that it stays safe. NOTE: do not modify the returned Packages.
func (*Codec) GetTypeInfo ¶
This is used primarily for gengo. XXX TODO: make this safe so modifications don't affect runtime codec, and ensure that it stays safe. NOTE: do not modify the returned TypeInfo.
func (*Codec) GetTypeURL ¶
TODO: this does need the cdc receiver, as it should also work for non-pbbindings-optimized types. Returns the default type url for the given concrete type. NOTE: It must be fast, as it is used in pbbindings. XXX Unstable API.
func (*Codec) Marshal ¶
Marshal encodes the object o according to the Amino spec. Marshal doesn't prefix the byte-length of the encoding, so the caller must handle framing. Type information as in google.protobuf.Any isn't included, so manually wrap before calling if you need to decode into an interface. NOTE: nil-struct-pointers have no encoding. In the context of a struct, the absence of a field does denote a nil-struct-pointer, but in general this is not the case, so unlike MarshalJSON.
func (*Codec) MarshalAny ¶
MarshalAny encodes the registered object wrapped with google.protobuf.Any.
func (*Codec) MarshalAnyLengthPrefixed ¶
func (*Codec) MarshalJSON ¶
func (*Codec) MarshalJSONAny ¶
func (*Codec) MarshalJSONIndent ¶
MarshalJSONIndent calls json.Indent on the output of cdc.MarshalJSON using the given prefix and indent string.
func (*Codec) MarshalLengthPrefixed ¶
MarshalLengthPrefixed encodes the object o according to the Amino spec, but prefixed by a uvarint encoding of the object to encode. Use Marshal if you don't want byte-length prefixing.
For consistency, MarshalLengthPrefixed will first dereference pointers before encoding. MarshalLengthPrefixed will panic if o is a nil-pointer, or if o is invalid.
func (*Codec) MarshalLengthPrefixedWriter ¶
MarshalLengthPrefixedWriter writes the bytes as would be returned from MarshalLengthPrefixed to the writer w.
func (*Codec) MarshalPBBindings ¶
func (cdc *Codec) MarshalPBBindings(pbm PBMessager) ([]byte, error)
Use pbbindings.
func (*Codec) MarshalReflect ¶
Use reflection.
func (*Codec) MustMarshalAny ¶
Panics if error.
func (*Codec) MustMarshalAnyLengthPrefixed ¶
func (*Codec) MustMarshalJSON ¶
MustMarshalJSON panics if an error occurs. Besides tha behaves exactly like MarshalJSON.
func (*Codec) MustMarshalLengthPrefixed ¶
Panics if error.
func (*Codec) MustUnmarshal ¶
Panics if error.
func (*Codec) MustUnmarshalAny ¶
func (*Codec) MustUnmarshalJSON ¶
MustUnmarshalJSON panics if an error occurs. Besides tha behaves exactly like UnmarshalJSON.
func (*Codec) MustUnmarshalLengthPrefixed ¶
Panics if error.
func (*Codec) PrintTypes ¶
PrintTypes writes all registered types in a markdown-style table. The table's header is:
| Type | TypeURL | Notes |
Where Type is the golang type name and TypeURL is the type_url the type was registered with.
func (*Codec) RegisterPackage ¶
The package isn't (yet) necessary besides to get the full name of concrete types. Registers all dependencies of pkg recursively. This operation is idempotent -- pkgs already registered may be registered again.
func (*Codec) RegisterTypeFrom ¶
This function should be used to register concrete types that will appear in interface fields/elements to be encoded/decoded by go-amino. You may want to use RegisterPackage() instead which registers everything in a package. Usage: `amino.RegisterTypeFrom(MyStruct1{}, "/tm.cryp.MyStruct1")`
func (*Codec) UnmarshalAny ¶
UnmarshalAny decodes the registered object from the Any fields.
func (*Codec) UnmarshalJSON ¶
func (*Codec) UnmarshalLengthPrefixed ¶
Like Unmarshal, but will first decode the byte-length prefix. UnmarshalLengthPrefixed will panic if ptr is a nil-pointer. Returns an error if not all of bz is consumed.
func (*Codec) UnmarshalLengthPrefixedReader ¶
func (cdc *Codec) UnmarshalLengthPrefixedReader(r io.Reader, ptr interface{}, maxSize int64) (n int64, err error)
Like Unmarshal, but will first read the byte-length prefix. UnmarshalLengthPrefixedReader will panic if ptr is a nil-pointer. If maxSize is 0, there is no limit (not recommended).
func (*Codec) WithPBBindings ¶
Returns a new codec that is optimized w/ pbbindings. The returned codec is sealed, but may be affected by modifications to the underlying codec.
type ConcreteInfo ¶
type ConcreteInfo struct { Registered bool // Registered with Register*(). Name string // Registered name which may override default reflection name. PointerPreferred bool // Deserialize to pointer type if possible. TypeURL string // <domain and path>/<p3 package no slashes>.<Name> IsAminoMarshaler bool // Implements MarshalAmino() (<ReprObject>, error) and UnmarshalAmino(<ReprObject>) (error). ReprType *TypeInfo // <ReprType> if IsAminoMarshaler, that, or by default the identity Type. IsJSONValueType bool // If true, the Any representation uses the "value" field (instead of embedding @type). IsBinaryWellKnownType bool // If true, use built-in functions to encode/decode. IsJSONWellKnownType bool // If true, use built-in functions to encode/decode. IsJSONAnyValueType bool // If true, the interface/Any representation uses the "value" field. Elem *TypeInfo // Set if Type.Kind() is Slice or Array. ElemIsPtr bool // Set true iff Type.Elem().Kind() is Pointer. }
type FieldInfo ¶
type FieldInfo struct { Type reflect.Type // Struct field reflect.Type. TypeInfo *TypeInfo // Dereferenced struct field TypeInfo Name string // Struct field name Index int // Struct field index ZeroValue reflect.Value // Could be nil pointer unlike TypeInfo.ZeroValue. UnpackedList bool // True iff this field should be encoded as an unpacked list. FieldOptions // Encoding options }
func (*FieldInfo) ValidateBasic ¶
func (finfo *FieldInfo) ValidateBasic()
type FieldOptions ¶
type FieldOptions struct { JSONName string // (JSON) field name JSONOmitEmpty bool // (JSON) omitempty BinFixed64 bool // (Binary) Encode as fixed64 BinFixed32 bool // (Binary) Encode as fixed32 BinFieldNum uint32 // (Binary) max 1<<29-1 Unsafe bool // e.g. if this field is a float. WriteEmpty bool // write empty structs and lists (default false except for pointers) NilElements bool // Empty list elements are decoded as nil iff set, otherwise are never nil. UseGoogleTypes bool // If true, decodes Any timestamp and duration to google types. }
type InterfaceInfo ¶
type InterfaceInfo struct { }
type InvalidDurationErr ¶
type InvalidDurationErr string
func (InvalidDurationErr) Error ¶
func (e InvalidDurationErr) Error() string
type InvalidTimeErr ¶
type InvalidTimeErr string
func (InvalidTimeErr) Error ¶
func (e InvalidTimeErr) Error() string
type Object ¶
type Object interface {
GetTypeURL() string
}
All concrete types must implement the Object interface for genproto bindings. They are generated automatically by genproto/bindings.go
type PBMessager ¶
type PBMessager interface { ToPBMessage(*Codec) (proto.Message, error) EmptyPBMessage(*Codec) proto.Message FromPBMessage(*Codec, proto.Message) error }
Methods generated by genproto/bindings.go for faster encoding.
type Package ¶
Package "pkg" exists So dependencies can create Packages. We export it here so this amino package can use it natively.
func RegisterPackage ¶
NOTE: do not modify the result.
type StructInfo ¶
type StructInfo struct {
Fields []FieldInfo // If a struct.
}
type TypeInfo ¶
type TypeInfo struct { Type reflect.Type // never a pointer kind. Package *Package // package associated with Type. PtrToType reflect.Type ZeroValue reflect.Value InterfaceInfo ConcreteInfo StructInfo }
func (*TypeInfo) GetTyp3 ¶
func (info *TypeInfo) GetTyp3(fopts FieldOptions) Typ3
func (*TypeInfo) GetUltimateElem ¶
If this is a slice or array, get .Elem.ReprType until no longer slice or array.
func (*TypeInfo) IsStructOrUnpacked ¶
func (info *TypeInfo) IsStructOrUnpacked(fopt FieldOptions) bool
Used to determine whether to create an implicit struct or not. Notice that the binary encoding of a list to be unpacked is indistinguishable from a struct that contains that list. NOTE: we expect info.Elem to be prepopulated, constructed within the scope of a Codec.