Documentation ¶
Overview ¶
Example (Ledger) ¶
package main import ( "fmt" "github.com/aviate-labs/agent-go/candid/idl" ) func main() { fmt.Println(idl.NewInterface(func(typ idl.IDL) *idl.Service { accountIdentitier := typ.Vec(typ.Nat8) accountBalanceArgs := typ.Record(map[string]idl.Type{ "account": accountIdentitier, }) tokens := idl.NewRecordType(map[string]idl.Type{ "e8s": idl.Nat64Type(), }) return typ.Service(map[string]*idl.FunctionType{ "account_balance": typ.Func([]idl.FunctionParameter{{Type: accountBalanceArgs}}, []idl.FunctionParameter{{Type: tokens}}, []string{"query"}), // etc. }) })) }
Output: service {account_balance:(record {account:vec nat8}) -> (record {e8s:nat64}) query}
Example (OptionalNat) ¶
package main import ( "fmt" "github.com/aviate-labs/agent-go/candid/idl" ) func main() { fmt.Println(idl.NewInterface(func(typ idl.IDL) *idl.Service { time := idl.NewOptionalType(new(idl.NatType)) return typ.Service(map[string]*idl.FunctionType{ "now": typ.Func([]idl.FunctionParameter{}, []idl.FunctionParameter{{Type: time}}, []string{"query"}), // etc. }) })) }
Output: service {now:() -> (opt nat) query}
Example (Tokens) ¶
package main import ( "fmt" "github.com/aviate-labs/agent-go/candid/idl" ) func main() { fmt.Println(idl.NewRecordType(map[string]idl.Type{ "e8s": idl.Nat64Type(), })) }
Output: record {e8s:nat64}
Index ¶
- func EmptyOf(t Type) (any, error)
- func Encode(argumentTypes []Type, arguments []any) ([]byte, error)
- func Hash(s string) *big.Int
- func HashString(s string) string
- func Marshal(args []any) ([]byte, error)
- func Ptr[a any](v a) *a
- func StructToMap(value any) (map[string]any, error)
- func Unmarshal(data []byte, values []any) error
- type BoolType
- func (BoolType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (b BoolType) Decode(r *bytes.Reader) (any, error)
- func (BoolType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (BoolType) EncodeValue(v any) ([]byte, error)
- func (BoolType) String() string
- func (BoolType) UnmarshalGo(raw any, _v any) error
- type DecodeError
- type Empty
- type EmptyType
- func (EmptyType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (EmptyType) Decode(*bytes.Reader) (any, error)
- func (EmptyType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (EmptyType) EncodeValue(_ any) ([]byte, error)
- func (EmptyType) String() string
- func (EmptyType) UnmarshalGo(raw any, _v any) error
- type EncodeValueError
- type FieldType
- type FloatType
- func (FloatType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (f FloatType) Base() uint
- func (f FloatType) Decode(r *bytes.Reader) (any, error)
- func (f FloatType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (f FloatType) EncodeValue(v any) ([]byte, error)
- func (f FloatType) String() string
- func (f FloatType) UnmarshalGo(raw any, _v any) error
- type FormatError
- type FunctionParameter
- type FunctionType
- func (f FunctionType) AddTypeDefinition(tdt *TypeDefinitionTable) error
- func (f FunctionType) Decode(r *bytes.Reader) (any, error)
- func (f FunctionType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
- func (f FunctionType) EncodeValue(v any) ([]byte, error)
- func (f FunctionType) String() string
- func (FunctionType) UnmarshalGo(raw any, _v any) error
- type IDL
- type IDLFactory
- type Int
- type IntType
- func (IntType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (n IntType) Base() uint
- func (n IntType) Decode(r *bytes.Reader) (any, error)
- func (n IntType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (n IntType) EncodeValue(v any) ([]byte, error)
- func (n IntType) String() string
- func (n IntType) UnmarshalGo(raw any, _v any) error
- type Integer
- type Method
- type Nat
- type NatType
- func (NatType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (n NatType) Base() uint
- func (n NatType) Decode(r *bytes.Reader) (any, error)
- func (n NatType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (n NatType) EncodeValue(v any) ([]byte, error)
- func (n NatType) String() string
- func (n NatType) UnmarshalGo(raw any, _v any) error
- type Natural
- type Null
- type NullType
- func (NullType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (NullType) Decode(_ *bytes.Reader) (any, error)
- func (NullType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (NullType) EncodeValue(v any) ([]byte, error)
- func (NullType) String() string
- func (NullType) UnmarshalGo(raw any, _v any) error
- type OptionalType
- func (o OptionalType) AddTypeDefinition(tdt *TypeDefinitionTable) error
- func (o OptionalType) Decode(r *bytes.Reader) (any, error)
- func (o OptionalType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
- func (o OptionalType) EncodeValue(v any) ([]byte, error)
- func (o OptionalType) String() string
- func (o OptionalType) UnmarshalGo(raw any, _v any) error
- type PrimType
- type PrincipalMethod
- type PrincipalType
- func (PrincipalType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (PrincipalType) Decode(r *bytes.Reader) (any, error)
- func (PrincipalType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (PrincipalType) EncodeValue(v any) ([]byte, error)
- func (PrincipalType) String() string
- func (PrincipalType) UnmarshalGo(raw any, _v any) error
- type RecordType
- func (record RecordType) AddTypeDefinition(tdt *TypeDefinitionTable) error
- func (record RecordType) Decode(r_ *bytes.Reader) (any, error)
- func (record RecordType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
- func (record RecordType) EncodeValue(v any) ([]byte, error)
- func (record RecordType) String() string
- func (record RecordType) UnmarshalGo(raw any, _v any) error
- type Reserved
- type ReservedType
- func (ReservedType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (ReservedType) Decode(*bytes.Reader) (any, error)
- func (ReservedType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (ReservedType) EncodeValue(_ any) ([]byte, error)
- func (ReservedType) String() string
- func (ReservedType) UnmarshalGo(raw any, _v any) error
- type Service
- func (s Service) AddTypeDefinition(tdt *TypeDefinitionTable) error
- func (s Service) Decode(r *bytes.Reader) (any, error)
- func (s Service) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
- func (s Service) EncodeValue(v any) ([]byte, error)
- func (s Service) String() string
- func (Service) UnmarshalGo(raw any, _v any) error
- type Tag
- type TextType
- func (TextType) AddTypeDefinition(_ *TypeDefinitionTable) error
- func (TextType) Decode(r *bytes.Reader) (any, error)
- func (TextType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
- func (TextType) EncodeValue(v any) ([]byte, error)
- func (TextType) String() string
- func (t TextType) UnmarshalGo(raw any, _v any) error
- type TupleType
- type Type
- type TypeDefinitionTable
- type UnknownTypeError
- type UnknownValueTypeError
- type UnmarshalGoError
- type Variant
- type VariantType
- func (variant VariantType) AddTypeDefinition(tdt *TypeDefinitionTable) error
- func (variant VariantType) Decode(r *bytes.Reader) (any, error)
- func (variant VariantType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
- func (variant VariantType) EncodeValue(value any) ([]byte, error)
- func (variant VariantType) String() string
- func (variant VariantType) UnmarshalGo(raw any, _v any) error
- type VectorType
- func (vec VectorType) AddTypeDefinition(tdt *TypeDefinitionTable) error
- func (vec VectorType) Decode(r *bytes.Reader) (any, error)
- func (vec VectorType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
- func (vec VectorType) EncodeValue(v any) ([]byte, error)
- func (vec VectorType) String() string
- func (vec VectorType) UnmarshalGo(raw any, _v any) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Hash ¶
Hash hashes a string to a number. ( Sum_(i=0..k) utf8(id)[i] * 223^(k-i) ) mod 2^32 where k = |utf8(id)|-1
func HashString ¶
Types ¶
type BoolType ¶
type BoolType struct {
// contains filtered or unexported fields
}
BoolType is a type of bool.
func (BoolType) AddTypeDefinition ¶
func (BoolType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (BoolType) EncodeType ¶
func (BoolType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
EncodeType returns the leb128 encoding of the bool type.
func (BoolType) EncodeValue ¶
EncodeValue encodes a bool value. Accepted types are: `bool`.
type DecodeError ¶
func (DecodeError) Error ¶
func (e DecodeError) Error() string
type EmptyType ¶
type EmptyType struct {
// contains filtered or unexported fields
}
EmptyType represents the empty type.
func (EmptyType) AddTypeDefinition ¶
func (EmptyType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (EmptyType) EncodeType ¶
func (EmptyType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
EncodeType returns the leb128 encoding of the empty type.
func (EmptyType) EncodeValue ¶
EncodeValue returns an empty byte slice.
type EncodeValueError ¶
Example ¶
fmt.Println(EncodeValueError{ Expected: boolType, Value: 0, }.Error())
Output: invalid type 0 (int), expected type bool
func NewEncodeValueError ¶
func NewEncodeValueError(v any, e int64) *EncodeValueError
func (EncodeValueError) Error ¶
func (e EncodeValueError) Error() string
type FieldType ¶
type FieldType struct { // Name is the name of the field. Name string // Type is the type of the field. Type Type // contains filtered or unexported fields }
FieldType represents a field of a struct or variant.
type FloatType ¶
type FloatType struct {
// contains filtered or unexported fields
}
FloatType is either a type of float32 or float64. Should only be initialized through `Float32Type` and `Float64Type`.
func Float32Type ¶
func Float32Type() *FloatType
Float32Type returns a type of float32.
Example ¶
test([]idl.Type{idl.Float32Type()}, []any{float32(-0.5)}) test([]idl.Type{idl.Float32Type()}, []any{float32(0)}) test([]idl.Type{idl.Float32Type()}, []any{float32(0.5)}) test([]idl.Type{idl.Float32Type()}, []any{float32(3)})
Output: 4449444c000173000000bf 4449444c00017300000000 4449444c0001730000003f 4449444c00017300004040
func Float64Type ¶
func Float64Type() *FloatType
Float64Type returns a type of float64.
Example ¶
test([]idl.Type{idl.Float64Type()}, []any{-0.5}) test([]idl.Type{idl.Float64Type()}, []any{float32(0)}) test([]idl.Type{idl.Float64Type()}, []any{0.5}) test([]idl.Type{idl.Float64Type()}, []any{float64(3)})
Output: 4449444c000172000000000000e0bf 4449444c0001720000000000000000 4449444c000172000000000000e03f 4449444c0001720000000000000840
func (FloatType) AddTypeDefinition ¶
func (FloatType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (FloatType) EncodeType ¶
func (f FloatType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
EncodeType returns the leb128 encoding of the FloatType.
func (FloatType) EncodeValue ¶
EncodeValue encodes a float value. Accepted types are: `float32` and `float64`.
type FormatError ¶
type FormatError struct {
Description string
}
func (FormatError) Error ¶
func (e FormatError) Error() string
type FunctionParameter ¶ added in v0.3.1
type FunctionParameter struct { Type Type // contains filtered or unexported fields }
type FunctionType ¶
type FunctionType struct { ArgumentParameters []FunctionParameter ReturnParameters []FunctionParameter Annotations []string }
Example ¶
p, _ := principal.Decode("w7x7r-cok77-xa") test_( []idl.Type{ idl.NewFunctionType( []idl.FunctionParameter{{Type: new(idl.TextType)}}, []idl.FunctionParameter{{Type: new(idl.NatType)}}, nil, ), }, []any{ idl.PrincipalMethod{ Principal: p, Method: "foo", }, }, )
Output: 4449444c016a0171017d000100010103caffee03666f6f
func NewFunctionType ¶
func NewFunctionType(argumentTypes []FunctionParameter, returnTypes []FunctionParameter, annotations []string) *FunctionType
func (FunctionType) AddTypeDefinition ¶
func (f FunctionType) AddTypeDefinition(tdt *TypeDefinitionTable) error
func (FunctionType) EncodeType ¶
func (f FunctionType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
func (FunctionType) EncodeValue ¶
func (f FunctionType) EncodeValue(v any) ([]byte, error)
func (FunctionType) String ¶
func (f FunctionType) String() string
func (FunctionType) UnmarshalGo ¶ added in v0.3.0
func (FunctionType) UnmarshalGo(raw any, _v any) error
type IDL ¶
type IDL struct { Null *NullType Bool *BoolType Nat *NatType Int *IntType Nat8 *NatType Nat16 *NatType Nat32 *NatType Nat64 *NatType Int8 *IntType Int16 *IntType Int32 *IntType Int64 *IntType Float32 *FloatType Float64 *FloatType Text *TextType Reserved *ReservedType Empty *EmptyType Opt func(typ Type) *OptionalType Tuple func(ts ...Type) *TupleType Vec func(t Type) *VectorType Record func(fields map[string]Type) *RecordType Variant func(fields map[string]Type) *VariantType Func func(args, ret []FunctionParameter, annotations []string) *FunctionType Service func(functions map[string]*FunctionType) *Service Principal *PrincipalType }
type IDLFactory ¶
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
Int represents an unbounded integer.
Example ¶
test([]idl.Type{new(idl.IntType)}, []any{idl.NewInt(0)}) test([]idl.Type{new(idl.IntType)}, []any{idl.NewInt(42)}) test([]idl.Type{new(idl.IntType)}, []any{idl.NewInt(1234567890)}) test([]idl.Type{new(idl.IntType)}, []any{idl.NewInt(-1234567890)}) test([]idl.Type{new(idl.IntType)}, []any{func() idl.Int { bi, _ := new(big.Int).SetString("60000000000000000", 10) return idl.NewBigInt(bi) }()})
Output: 4449444c00017c00 4449444c00017c2a 4449444c00017cd285d8cc04 4449444c00017caefaa7b37b 4449444c00017c808098f4e9b5caea00
func NewIntFromString ¶
NewIntFromString creates a new Int from a string.
type IntType ¶
type IntType struct {
// contains filtered or unexported fields
}
IntType is either a type of int8, int16, int32, int64, or int.
func Int32Type ¶
func Int32Type() *IntType
Int32Type returns a type of int32.
Example ¶
test([]idl.Type{idl.Int32Type()}, []any{int32(-1234567890)}) test([]idl.Type{idl.Int32Type()}, []any{int32(-42)}) test([]idl.Type{idl.Int32Type()}, []any{int32(42)}) test([]idl.Type{idl.Int32Type()}, []any{int32(1234567890)})
Output: 4449444c0001752efd69b6 4449444c000175d6ffffff 4449444c0001752a000000 4449444c000175d2029649
func Int8Type ¶
func Int8Type() *IntType
Int8Type returns a type of int8.
Example ¶
test([]idl.Type{idl.Int8Type()}, []any{int16(-129)}) test([]idl.Type{idl.Int8Type()}, []any{int8(-128)}) test([]idl.Type{idl.Int8Type()}, []any{int8(-42)}) test([]idl.Type{idl.Int8Type()}, []any{int8(-1)}) test([]idl.Type{idl.Int8Type()}, []any{int8(0)}) test([]idl.Type{idl.Int8Type()}, []any{int8(1)}) test([]idl.Type{idl.Int8Type()}, []any{int8(42)}) test([]idl.Type{idl.Int8Type()}, []any{int8(127)}) test([]idl.Type{idl.Int8Type()}, []any{int16(128)})
Output: enc: invalid value: -129 4449444c00017780 4449444c000177d6 4449444c000177ff 4449444c00017700 4449444c00017701 4449444c0001772a 4449444c0001777f enc: invalid value: 128
func (IntType) AddTypeDefinition ¶
func (IntType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (IntType) EncodeType ¶
func (n IntType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
EncodeType returns the leb128 encoding of the IntType.
func (IntType) EncodeValue ¶
EncodeValue encodes an int value. Accepted types are: `int`, `int8`, `int16`, `int32`, `int64`, `Int`.
type Method ¶
type Method struct { Name string Func *FunctionType }
type Nat ¶
type Nat struct {
// contains filtered or unexported fields
}
Nat represents an unbounded natural number.
func NewNatFromString ¶
NewNatFromString creates a new Nat from a string.
type NatType ¶
type NatType struct {
// contains filtered or unexported fields
}
NatType is either a type of nat8, nat16, nat32, nat64, or nat.
Example ¶
test([]idl.Type{new(idl.NatType)}, []any{idl.NewNat(uint(0))}) test([]idl.Type{new(idl.NatType)}, []any{idl.NewNat(uint(42))}) test([]idl.Type{new(idl.NatType)}, []any{idl.NewNat(uint(1234567890))}) test([]idl.Type{new(idl.NatType)}, []any{func() idl.Nat { bi, _ := new(big.Int).SetString("60000000000000000", 10) return idl.NewBigNat(bi) }()})
Output: 4449444c00017d00 4449444c00017d2a 4449444c00017dd285d8cc04 4449444c00017d808098f4e9b5ca6a
func Nat16Type ¶
func Nat16Type() *NatType
Nat16Type returns a type of nat16.
Example ¶
test([]idl.Type{idl.Nat16Type()}, []any{uint16(0)}) test([]idl.Type{idl.Nat16Type()}, []any{uint16(42)}) test([]idl.Type{idl.Nat16Type()}, []any{uint16(65535)}) test([]idl.Type{idl.Nat16Type()}, []any{uint32(65536)})
Output: 4449444c00017a0000 4449444c00017a2a00 4449444c00017affff enc: invalid value: 65536
func Nat32Type ¶
func Nat32Type() *NatType
Nat32Type returns a type of nat32.
Example ¶
test([]idl.Type{idl.Nat32Type()}, []any{uint32(0)}) test([]idl.Type{idl.Nat32Type()}, []any{uint32(42)}) test([]idl.Type{idl.Nat32Type()}, []any{uint32(4294967295)}) test([]idl.Type{idl.Nat32Type()}, []any{uint64(4294967296)})
Output: 4449444c00017900000000 4449444c0001792a000000 4449444c000179ffffffff enc: invalid value: 4294967296
func Nat64Type ¶
func Nat64Type() *NatType
Nat64Type returns a type of nat64.
Example ¶
test([]idl.Type{idl.Nat64Type()}, []any{uint64(0)}) test([]idl.Type{idl.Nat64Type()}, []any{uint64(42)}) test([]idl.Type{idl.Nat64Type()}, []any{uint64(1234567890)})
Output: 4449444c0001780000000000000000 4449444c0001782a00000000000000 4449444c000178d202964900000000
func Nat8Type ¶
func Nat8Type() *NatType
Nat8Type returns a type of nat8.
Example ¶
test([]idl.Type{idl.Nat8Type()}, []any{uint8(0)}) test([]idl.Type{idl.Nat8Type()}, []any{uint8(42)}) test([]idl.Type{idl.Nat8Type()}, []any{uint8(255)}) test([]idl.Type{idl.Nat8Type()}, []any{uint16(256)})
Output: 4449444c00017b00 4449444c00017b2a 4449444c00017bff enc: invalid value: 256
func (NatType) AddTypeDefinition ¶
func (NatType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (NatType) EncodeType ¶
func (n NatType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
EncodeType returns the leb128 encoding of the NatType.
func (NatType) EncodeValue ¶
EncodeValue encodes an nat value. Accepts: `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `Nat`.
type Null ¶
type Null struct{}
Example ¶
test([]idl.Type{new(idl.NullType)}, []any{nil})
Output: 4449444c00017f
type NullType ¶
type NullType struct {
// contains filtered or unexported fields
}
func (NullType) AddTypeDefinition ¶
func (NullType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (NullType) EncodeType ¶
func (NullType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
type OptionalType ¶
type OptionalType struct {
Type Type
}
OptionalType is the type of an optional value.
func NewOptionalType ¶
func NewOptionalType(t Type) *OptionalType
NewOptionalType creates a new optional type.
func (OptionalType) AddTypeDefinition ¶
func (o OptionalType) AddTypeDefinition(tdt *TypeDefinitionTable) error
AddTypeDefinition adds the type definition to the table.
func (OptionalType) Decode ¶
func (o OptionalType) Decode(r *bytes.Reader) (any, error)
Decode decodes the value from the given reader into either `nil` or a value (of the subtype of the optional type).
func (OptionalType) EncodeType ¶
func (o OptionalType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
EncodeType encodes the type into a byte array.
func (OptionalType) EncodeValue ¶
func (o OptionalType) EncodeValue(v any) ([]byte, error)
EncodeValue encodes the value into a byte array. Accepts `nil` or a value (of the subtype of the optional type).
func (OptionalType) String ¶
func (o OptionalType) String() string
String returns the string representation of the type.
func (OptionalType) UnmarshalGo ¶ added in v0.3.0
func (o OptionalType) UnmarshalGo(raw any, _v any) error
type PrincipalMethod ¶
type PrincipalType ¶
type PrincipalType struct {
// contains filtered or unexported fields
}
func (PrincipalType) AddTypeDefinition ¶
func (PrincipalType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (PrincipalType) EncodeType ¶
func (PrincipalType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
func (PrincipalType) EncodeValue ¶
func (PrincipalType) EncodeValue(v any) ([]byte, error)
func (PrincipalType) String ¶
func (PrincipalType) String() string
func (PrincipalType) UnmarshalGo ¶ added in v0.3.0
func (PrincipalType) UnmarshalGo(raw any, _v any) error
type RecordType ¶
type RecordType struct {
Fields []FieldType
}
Example ¶
test([]idl.Type{idl.NewRecordType(nil)}, []any{nil}) test_([]idl.Type{idl.NewRecordType(map[string]idl.Type{ "foo": new(idl.TextType), "bar": new(idl.IntType), })}, []any{ map[string]any{ "foo": "💩", "bar": idl.NewInt(42), "baz": idl.NewInt(0), }, })
Output: 4449444c016c000100 4449444c016c02d3e3aa027c868eb7027101002a04f09f92a9
Example (Nested) ¶
recordType := idl.NewRecordType(map[string]idl.Type{ "foo": idl.Int32Type(), "bar": new(idl.BoolType), }) recordValue := map[string]any{ "foo": int32(42), "bar": true, } test_([]idl.Type{idl.NewRecordType(map[string]idl.Type{ "foo": idl.Int32Type(), "bar": recordType, "baz": recordType, "bib": recordType, })}, []any{ map[string]any{ "foo": int32(42), "bar": recordValue, "baz": recordValue, "bib": recordValue, }, })
Output: 4449444c026c02d3e3aa027e868eb702756c04d3e3aa0200dbe3aa0200bbf1aa0200868eb702750101012a000000012a000000012a0000002a000000
func NewRecordType ¶
func NewRecordType(fields map[string]Type) *RecordType
func (RecordType) AddTypeDefinition ¶
func (record RecordType) AddTypeDefinition(tdt *TypeDefinitionTable) error
func (RecordType) EncodeType ¶
func (record RecordType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
func (RecordType) EncodeValue ¶
func (record RecordType) EncodeValue(v any) ([]byte, error)
func (RecordType) String ¶
func (record RecordType) String() string
func (RecordType) UnmarshalGo ¶ added in v0.3.0
func (record RecordType) UnmarshalGo(raw any, _v any) error
type ReservedType ¶
type ReservedType struct {
// contains filtered or unexported fields
}
func (ReservedType) AddTypeDefinition ¶
func (ReservedType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (ReservedType) EncodeType ¶
func (ReservedType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
func (ReservedType) EncodeValue ¶
func (ReservedType) EncodeValue(_ any) ([]byte, error)
func (ReservedType) String ¶
func (ReservedType) String() string
func (ReservedType) UnmarshalGo ¶ added in v0.3.0
func (ReservedType) UnmarshalGo(raw any, _v any) error
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Example ¶
p, _ := principal.Decode("w7x7r-cok77-xa") test( []idl.Type{idl.NewServiceType( map[string]*idl.FunctionType{ "foo": idl.NewFunctionType( []idl.FunctionParameter{{Type: new(idl.TextType)}}, []idl.FunctionParameter{{Type: new(idl.NatType)}}, nil, ), }, )}, []any{ p, }, )
Output: 4449444c026a0171017d00690103666f6f0001010103caffee
func NewInterface ¶
func NewInterface(factory IDLFactory) *Service
func NewServiceType ¶
func NewServiceType(methods map[string]*FunctionType) *Service
func (Service) AddTypeDefinition ¶
func (s Service) AddTypeDefinition(tdt *TypeDefinitionTable) error
func (Service) EncodeType ¶
func (s Service) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
type Tag ¶ added in v0.3.0
func ParseTags ¶ added in v0.3.0
func ParseTags(field reflect.StructField) Tag
type TextType ¶
type TextType struct {
// contains filtered or unexported fields
}
TextType is the type of a text value.
func (TextType) AddTypeDefinition ¶
func (TextType) AddTypeDefinition(_ *TypeDefinitionTable) error
func (TextType) EncodeType ¶
func (TextType) EncodeType(_ *TypeDefinitionTable) ([]byte, error)
EncodeType encodes the type into a byte slice.
func (TextType) EncodeValue ¶
EncodeValue encodes the value into a byte slice.
type Type ¶
type Type interface { // AddTypeDefinition adds itself to the definition table if it is not a primitive type. AddTypeDefinition(*TypeDefinitionTable) error // Decode decodes the value from the reader. Decode(*bytes.Reader) (any, error) // EncodeType encodes the type. EncodeType(*TypeDefinitionTable) ([]byte, error) // EncodeValue encodes the value. EncodeValue(v any) ([]byte, error) // UnmarshalGo unmarshals the value from the go value. UnmarshalGo(raw any, v any) error fmt.Stringer }
func TypeOf ¶
Example ¶
package main import ( "fmt" "github.com/aviate-labs/agent-go/candid/idl" "github.com/aviate-labs/agent-go/principal" ) func main() { i := 0 fmt.Println(idl.TypeOf(i)) fmt.Println(idl.TypeOf(&i)) fmt.Println(idl.TypeOf([]any{0})) fmt.Println(idl.TypeOf(map[string]any{ "foo": 0, })) fmt.Println(idl.TypeOf(idl.Variant{ Name: "foo", Value: 0, Type: idl.NewVariantType(map[string]idl.Type{ "foo": new(idl.NatType), }), })) fmt.Println(idl.TypeOf(principal.Principal{})) }
Output: int64 <nil> opt int64 <nil> vec int64 <nil> record {foo:int64} <nil> variant {foo:int64} <nil> principal <nil>
type TypeDefinitionTable ¶
func (*TypeDefinitionTable) Add ¶
func (tdt *TypeDefinitionTable) Add(t Type, bs []byte)
type UnknownTypeError ¶
type UnknownTypeError struct {
Type Type
}
func (UnknownTypeError) Error ¶
func (e UnknownTypeError) Error() string
type UnknownValueTypeError ¶
type UnknownValueTypeError struct {
Value any
}
func (UnknownValueTypeError) Error ¶
func (e UnknownValueTypeError) Error() string
type UnmarshalGoError ¶ added in v0.3.0
func NewUnmarshalGoError ¶ added in v0.3.0
func NewUnmarshalGoError(raw any, v any) *UnmarshalGoError
func (UnmarshalGoError) Error ¶ added in v0.3.0
func (e UnmarshalGoError) Error() string
type VariantType ¶
type VariantType struct {
Fields []FieldType
}
Example ¶
result := map[string]idl.Type{ "ok": new(idl.TextType), "err": new(idl.TextType), } typ := idl.NewVariantType(result) test_([]idl.Type{typ}, []any{idl.Variant{ Name: "ok", Value: "good", Type: typ, }}) test_([]idl.Type{idl.NewVariantType(result)}, []any{idl.Variant{ Name: "err", Value: "uhoh", Type: typ, }})
Output: 4449444c016b029cc20171e58eb4027101000004676f6f64 4449444c016b029cc20171e58eb402710100010475686f68
func NewVariantType ¶
func NewVariantType(fields map[string]Type) *VariantType
func (VariantType) AddTypeDefinition ¶
func (variant VariantType) AddTypeDefinition(tdt *TypeDefinitionTable) error
func (VariantType) EncodeType ¶
func (variant VariantType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
func (VariantType) EncodeValue ¶
func (variant VariantType) EncodeValue(value any) ([]byte, error)
func (VariantType) String ¶
func (variant VariantType) String() string
func (VariantType) UnmarshalGo ¶ added in v0.3.0
func (variant VariantType) UnmarshalGo(raw any, _v any) error
type VectorType ¶
type VectorType struct {
Type Type
}
Example ¶
test([]idl.Type{idl.NewVectorType(new(idl.IntType))}, []any{ []any{idl.NewInt(0), idl.NewInt(1), idl.NewInt(2), idl.NewInt(3)}, })
Output: 4449444c016d7c01000400010203
func NewVectorType ¶
func NewVectorType(t Type) *VectorType
func (VectorType) AddTypeDefinition ¶
func (vec VectorType) AddTypeDefinition(tdt *TypeDefinitionTable) error
func (VectorType) EncodeType ¶
func (vec VectorType) EncodeType(tdt *TypeDefinitionTable) ([]byte, error)
func (VectorType) EncodeValue ¶
func (vec VectorType) EncodeValue(v any) ([]byte, error)
func (VectorType) String ¶
func (vec VectorType) String() string
func (VectorType) UnmarshalGo ¶ added in v0.3.0
func (vec VectorType) UnmarshalGo(raw any, _v any) error