idl

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: Apache-2.0 Imports: 11 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(argumentTypes []Type, arguments []interface{}) ([]byte, error)

func Hash

func Hash(s string) *big.Int

Hashes a string to a number. ( Sum_(i=0..k) utf8(id)[i] * 223^(k-i) ) mod 2^32 where k = |utf8(id)|-1

Types

type Bool

type Bool struct {
	// contains filtered or unexported fields
}
Example
test([]idl.Type{new(idl.Bool)}, []interface{}{true})
test([]idl.Type{new(idl.Bool)}, []interface{}{false})
test([]idl.Type{new(idl.Bool)}, []interface{}{0})
test([]idl.Type{new(idl.Bool)}, []interface{}{"false"})
Output:
4449444c00017e01
4449444c00017e00
enc: invalid argument: 0
enc: invalid argument: false

func (Bool) AddTypeDefinition

func (Bool) AddTypeDefinition(_ *TypeDefinitionTable) error

func (Bool) Decode

func (b Bool) Decode(r *bytes.Reader) (interface{}, error)

func (Bool) EncodeType

func (Bool) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Bool) EncodeValue

func (b Bool) EncodeValue(v interface{}) ([]byte, error)

func (Bool) String

func (Bool) String() string

type DecodeError

type DecodeError struct {
	Types       Tuple
	Description string
}

func (DecodeError) Error

func (e DecodeError) Error() string

type Empty

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

func (Empty) AddTypeDefinition

func (Empty) AddTypeDefinition(_ *TypeDefinitionTable) error

func (Empty) Decode

func (Empty) Decode(*bytes.Reader) (interface{}, error)

func (Empty) EncodeType

func (Empty) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Empty) EncodeValue

func (Empty) EncodeValue(_ interface{}) ([]byte, error)

func (Empty) String

func (Empty) String() string

type Field

type Field struct {
	Name string
	Type Type
}

type FieldValue

type FieldValue struct {
	Name  string
	Value interface{}
}

type Float

type Float struct {
	Base uint8
	// contains filtered or unexported fields
}

func Float32

func Float32() *Float
Example
test([]idl.Type{idl.Float32()}, []interface{}{big.NewFloat(-0.5)})
test([]idl.Type{idl.Float32()}, []interface{}{big.NewFloat(0)})
test([]idl.Type{idl.Float32()}, []interface{}{big.NewFloat(0.5)})
test([]idl.Type{idl.Float32()}, []interface{}{big.NewFloat(3)})
Output:
4449444c000173000000bf
4449444c00017300000000
4449444c0001730000003f
4449444c00017300004040

func Float64

func Float64() *Float
Example
test([]idl.Type{idl.Float64()}, []interface{}{big.NewFloat(-0.5)})
test([]idl.Type{idl.Float64()}, []interface{}{big.NewFloat(0)})
test([]idl.Type{idl.Float64()}, []interface{}{big.NewFloat(0.5)})
test([]idl.Type{idl.Float64()}, []interface{}{big.NewFloat(3)})
Output:
4449444c000172000000000000e0bf
4449444c0001720000000000000000
4449444c000172000000000000e03f
4449444c0001720000000000000840

func (Float) AddTypeDefinition

func (Float) AddTypeDefinition(_ *TypeDefinitionTable) error

func (*Float) Decode

func (f *Float) Decode(r *bytes.Reader) (interface{}, error)

func (Float) EncodeType

func (f Float) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Float) EncodeValue

func (f Float) EncodeValue(v interface{}) ([]byte, error)

func (Float) String

func (f Float) String() string

type FormatError

type FormatError struct {
	Description string
}

func (FormatError) Error

func (e FormatError) Error() string

type Func

type Func struct {
	ArgTypes    []Type
	RetTypes    []Type
	Annotations []string
}
Example
p, _ := principal.Decode("w7x7r-cok77-xa")
test_(
	[]idl.Type{
		idl.NewFunc(
			[]idl.Type{new(idl.Text)},
			[]idl.Type{new(idl.Nat)},
			nil,
		),
	},
	[]interface{}{
		idl.PrincipalMethod{
			Principal: p,
			Method:    "foo",
		},
	},
)
Output:
4449444c016a0171017d000100010103caffee03666f6f

func NewFunc

func NewFunc(argumentTypes []Type, returnTypes []Type, annotations []string) *Func

func (Func) AddTypeDefinition

func (f Func) AddTypeDefinition(tdt *TypeDefinitionTable) error

func (Func) Decode

func (f Func) Decode(r *bytes.Reader) (interface{}, error)

func (Func) EncodeType

func (f Func) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)

func (Func) EncodeValue

func (f Func) EncodeValue(v interface{}) ([]byte, error)

func (Func) String

func (f Func) String() string

type Int

type Int struct {
	Base uint8
	// contains filtered or unexported fields
}
Example
test([]idl.Type{new(idl.Int)}, []interface{}{big.NewInt(0)})
test([]idl.Type{new(idl.Int)}, []interface{}{big.NewInt(42)})
test([]idl.Type{new(idl.Int)}, []interface{}{big.NewInt(1234567890)})
test([]idl.Type{new(idl.Int)}, []interface{}{big.NewInt(-1234567890)})
test([]idl.Type{new(idl.Int)}, []interface{}{func() *big.Int {
	bi, _ := new(big.Int).SetString("60000000000000000", 10)
	return bi
}()})
Output:
4449444c00017c00
4449444c00017c2a
4449444c00017cd285d8cc04
4449444c00017caefaa7b37b
4449444c00017c808098f4e9b5caea00

func Int8

func Int8() *Int
Example
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(-129)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(-128)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(-42)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(-1)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(0)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(1)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(42)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(127)})
test([]idl.Type{idl.Int8()}, []interface{}{big.NewInt(128)})
Output:
enc: invalid value: -129
4449444c00017780
4449444c000177d6
4449444c000177ff
4449444c00017700
4449444c00017701
4449444c0001772a
4449444c0001777f
enc: invalid value: 128

func Int16

func Int16() *Int

func Int32

func Int32() *Int
Example
test([]idl.Type{idl.Int32()}, []interface{}{big.NewInt(-1234567890)})
test([]idl.Type{idl.Int32()}, []interface{}{big.NewInt(-42)})
test([]idl.Type{idl.Int32()}, []interface{}{big.NewInt(42)})
test([]idl.Type{idl.Int32()}, []interface{}{big.NewInt(1234567890)})
Output:
4449444c0001752efd69b6
4449444c000175d6ffffff
4449444c0001752a000000
4449444c000175d2029649

func Int64

func Int64() *Int

func (Int) AddTypeDefinition

func (Int) AddTypeDefinition(_ *TypeDefinitionTable) error

func (*Int) Decode

func (n *Int) Decode(r *bytes.Reader) (interface{}, error)

func (Int) EncodeType

func (n Int) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Int) EncodeValue

func (n Int) EncodeValue(v interface{}) ([]byte, error)

func (Int) String

func (n Int) String() string

type Method

type Method struct {
	Name string
	Func *Func
}

type Nat

type Nat struct {
	Base uint8
	// contains filtered or unexported fields
}
Example
test([]idl.Type{new(idl.Nat)}, []interface{}{big.NewInt(-1)})
test([]idl.Type{new(idl.Nat)}, []interface{}{big.NewInt(0)})
test([]idl.Type{new(idl.Nat)}, []interface{}{big.NewInt(42)})
test([]idl.Type{new(idl.Nat)}, []interface{}{big.NewInt(1234567890)})
test([]idl.Type{new(idl.Nat)}, []interface{}{func() *big.Int {
	bi, _ := new(big.Int).SetString("60000000000000000", 10)
	return bi
}()})
Output:
enc: can not leb128 encode negative values
4449444c00017d00
4449444c00017d2a
4449444c00017dd285d8cc04
4449444c00017d808098f4e9b5ca6a

func Nat8

func Nat8() *Nat
Example
test([]idl.Type{idl.Nat8()}, []interface{}{big.NewInt(0)})
test([]idl.Type{idl.Nat8()}, []interface{}{big.NewInt(42)})
test([]idl.Type{idl.Nat8()}, []interface{}{big.NewInt(255)})
test([]idl.Type{idl.Nat8()}, []interface{}{big.NewInt(256)})
Output:
4449444c00017b00
4449444c00017b2a
4449444c00017bff
enc: invalid value: 256

func Nat16

func Nat16() *Nat
Example
test([]idl.Type{idl.Nat16()}, []interface{}{big.NewInt(0)})
test([]idl.Type{idl.Nat16()}, []interface{}{big.NewInt(42)})
test([]idl.Type{idl.Nat16()}, []interface{}{big.NewInt(65535)})
test([]idl.Type{idl.Nat16()}, []interface{}{big.NewInt(65536)})
Output:
4449444c00017a0000
4449444c00017a2a00
4449444c00017affff
enc: invalid value: 65536

func Nat32

func Nat32() *Nat
Example
test([]idl.Type{idl.Nat32()}, []interface{}{big.NewInt(0)})
test([]idl.Type{idl.Nat32()}, []interface{}{big.NewInt(42)})
test([]idl.Type{idl.Nat32()}, []interface{}{big.NewInt(4294967295)})
test([]idl.Type{idl.Nat32()}, []interface{}{big.NewInt(4294967296)})
Output:
4449444c00017900000000
4449444c0001792a000000
4449444c000179ffffffff
enc: invalid value: 4294967296

func Nat64

func Nat64() *Nat
Example
test([]idl.Type{idl.Nat64()}, []interface{}{big.NewInt(0)})
test([]idl.Type{idl.Nat64()}, []interface{}{big.NewInt(42)})
test([]idl.Type{idl.Nat64()}, []interface{}{big.NewInt(1234567890)})
Output:
4449444c0001780000000000000000
4449444c0001782a00000000000000
4449444c000178d202964900000000

func (Nat) AddTypeDefinition

func (Nat) AddTypeDefinition(_ *TypeDefinitionTable) error

func (*Nat) Decode

func (n *Nat) Decode(r *bytes.Reader) (interface{}, error)

func (Nat) EncodeType

func (n Nat) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Nat) EncodeValue

func (n Nat) EncodeValue(v interface{}) ([]byte, error)

func (Nat) String

func (n Nat) String() string

type Null

type Null struct {
	// contains filtered or unexported fields
}
Example
test([]idl.Type{new(idl.Null)}, []interface{}{nil})
Output:
4449444c00017f

func (Null) AddTypeDefinition

func (Null) AddTypeDefinition(_ *TypeDefinitionTable) error

func (*Null) Decode

func (n *Null) Decode(_ *bytes.Reader) (interface{}, error)

func (Null) EncodeType

func (Null) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Null) EncodeValue

func (Null) EncodeValue(v interface{}) ([]byte, error)

func (Null) String

func (n Null) String() string

type Opt

type Opt struct {
	Type Type
}
Example
test([]idl.Type{idl.NewOpt(new(idl.Nat))}, []interface{}{nil})
test([]idl.Type{idl.NewOpt(new(idl.Nat))}, []interface{}{big.NewInt(1)})
Output:
4449444c016e7d010000
4449444c016e7d01000101

func NewOpt

func NewOpt(t Type) *Opt

func (Opt) AddTypeDefinition

func (o Opt) AddTypeDefinition(tdt *TypeDefinitionTable) error

func (Opt) Decode

func (o Opt) Decode(r *bytes.Reader) (interface{}, error)

func (Opt) EncodeType

func (o Opt) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)

func (Opt) EncodeValue

func (o Opt) EncodeValue(v interface{}) ([]byte, error)

func (Opt) String

func (o Opt) String() string

type PrimType

type PrimType interface {
	// contains filtered or unexported methods
}

type Principal

type Principal struct {
	// contains filtered or unexported fields
}
Example
p, _ := principal.Decode("gvbup-jyaaa-aaaah-qcdwa-cai")
test([]idl.Type{new(idl.Principal)}, []interface{}{p})
Output:
4449444c000168010a0000000000f010ec0101

func (Principal) AddTypeDefinition

func (Principal) AddTypeDefinition(_ *TypeDefinitionTable) error

func (*Principal) Decode

func (t *Principal) Decode(r *bytes.Reader) (interface{}, error)

func (Principal) EncodeType

func (t Principal) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Principal) EncodeValue

func (t Principal) EncodeValue(v interface{}) ([]byte, error)

func (Principal) String

func (t Principal) String() string

type PrincipalMethod

type PrincipalMethod struct {
	Principal principal.Principal
	Method    string
}

type Rec

type Rec struct {
	Fields []Field
}
Example
test([]idl.Type{idl.NewRec(nil)}, []interface{}{nil})
test_([]idl.Type{idl.NewRec(map[string]idl.Type{
	"foo": new(idl.Text),
	"bar": new(idl.Int),
})}, []interface{}{
	map[string]interface{}{
		"foo": "💩",
		"bar": big.NewInt(42),
		"baz": big.NewInt(0),
	},
})
Output:
4449444c016c000100
4449444c016c02d3e3aa027c868eb7027101002a04f09f92a9

func NewRec

func NewRec(fields map[string]Type) *Rec

func (Rec) AddTypeDefinition

func (r Rec) AddTypeDefinition(tdt *TypeDefinitionTable) error

func (Rec) Decode

func (r Rec) Decode(r_ *bytes.Reader) (interface{}, error)

func (Rec) EncodeType

func (r Rec) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)

func (Rec) EncodeValue

func (r Rec) EncodeValue(v interface{}) ([]byte, error)

func (Rec) String

func (r Rec) String() string

type Reserved

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

func (Reserved) AddTypeDefinition

func (Reserved) AddTypeDefinition(_ *TypeDefinitionTable) error

func (Reserved) Decode

func (Reserved) Decode(*bytes.Reader) (interface{}, error)

func (Reserved) EncodeType

func (Reserved) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Reserved) EncodeValue

func (Reserved) EncodeValue(_ interface{}) ([]byte, error)

func (Reserved) String

func (Reserved) String() string

type Service

type Service struct {
	// contains filtered or unexported fields
}
Example
p, _ := principal.Decode("w7x7r-cok77-xa")
test(
	[]idl.Type{idl.NewService(
		map[string]*idl.Func{
			"foo": idl.NewFunc(
				[]idl.Type{new(idl.Text)},
				[]idl.Type{new(idl.Nat)},
				nil,
			),
		},
	)},
	[]interface{}{
		p,
	},
)
Output:
4449444c026a0171017d00690103666f6f0001010103caffee

func NewService

func NewService(methods map[string]*Func) *Service

func (Service) AddTypeDefinition

func (s Service) AddTypeDefinition(tdt *TypeDefinitionTable) error

func (Service) Decode

func (s Service) Decode(r *bytes.Reader) (interface{}, error)

func (Service) EncodeType

func (s Service) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)

func (Service) EncodeValue

func (s Service) EncodeValue(v interface{}) ([]byte, error)

func (Service) String

func (s Service) String() string

type Text

type Text struct {
	// contains filtered or unexported fields
}
Example
test([]idl.Type{new(idl.Text)}, []interface{}{""})
test([]idl.Type{new(idl.Text)}, []interface{}{"Motoko"})
test([]idl.Type{new(idl.Text)}, []interface{}{"Hi ☃\n"})
Output:
4449444c00017100
4449444c000171064d6f746f6b6f
4449444c00017107486920e298830a

func (Text) AddTypeDefinition

func (Text) AddTypeDefinition(_ *TypeDefinitionTable) error

func (*Text) Decode

func (t *Text) Decode(r *bytes.Reader) (interface{}, error)

func (Text) EncodeType

func (t Text) EncodeType(_ *TypeDefinitionTable) ([]byte, error)

func (Text) EncodeValue

func (t Text) EncodeValue(v interface{}) ([]byte, error)

func (Text) String

func (t Text) String() string

type Tuple

type Tuple []Type

func (Tuple) String

func (ts Tuple) String() string

type Type

type Type interface {
	// AddTypeDefinition adds itself to the definition table if it is not a primitive type.
	AddTypeDefinition(*TypeDefinitionTable) error

	// Decodes the value from the reader.
	Decode(*bytes.Reader) (interface{}, error)

	// Encodes the type.
	EncodeType(*TypeDefinitionTable) ([]byte, error)

	// Encodes the value.
	EncodeValue(v interface{}) ([]byte, error)

	fmt.Stringer
}

func Decode

func Decode(bs []byte) ([]Type, []interface{}, error)

type TypeDefinitionTable

type TypeDefinitionTable struct {
	Types   [][]byte
	Indexes map[string]int
}

func (*TypeDefinitionTable) Add

func (tdt *TypeDefinitionTable) Add(t Type, bs []byte)

type Variant

type Variant struct {
	Fields []Field
}
Example
result := map[string]idl.Type{
	"ok":  new(idl.Text),
	"err": new(idl.Text),
}
test_([]idl.Type{idl.NewVariant(result)}, []interface{}{idl.FieldValue{
	Name:  "ok",
	Value: "good",
}})
test_([]idl.Type{idl.NewVariant(result)}, []interface{}{idl.FieldValue{
	Name:  "err",
	Value: "uhoh",
}})
Output:
4449444c016b029cc20171e58eb4027101000004676f6f64
4449444c016b029cc20171e58eb402710100010475686f68

func NewVariant

func NewVariant(fields map[string]Type) *Variant

func (Variant) AddTypeDefinition

func (v Variant) AddTypeDefinition(tdt *TypeDefinitionTable) error

func (Variant) Decode

func (v Variant) Decode(r *bytes.Reader) (interface{}, error)

func (Variant) EncodeType

func (v Variant) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)

func (Variant) EncodeValue

func (v Variant) EncodeValue(value interface{}) ([]byte, error)

func (Variant) String

func (v Variant) String() string

type Vec

type Vec struct {
	Type Type
}
Example
test([]idl.Type{idl.NewVec(new(idl.Int))}, []interface{}{
	[]interface{}{big.NewInt(0), big.NewInt(1), big.NewInt(2), big.NewInt(3)},
})
Output:
4449444c016d7c01000400010203

func NewVec

func NewVec(t Type) *Vec

func (Vec) AddTypeDefinition

func (v Vec) AddTypeDefinition(tdt *TypeDefinitionTable) error

func (Vec) Decode

func (v Vec) Decode(r *bytes.Reader) (interface{}, error)

func (Vec) EncodeType

func (v Vec) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)

func (Vec) EncodeValue

func (v Vec) EncodeValue(value interface{}) ([]byte, error)

func (Vec) String

func (v Vec) String() string

Jump to

Keyboard shortcuts

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