idl

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: Apache-2.0 Imports: 13 Imported by: 7

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmptyOf

func EmptyOf(t Type) (any, error)

func Encode

func Encode(argumentTypes []Type, arguments []any) ([]byte, error)

func Hash

func Hash(s string) *big.Int

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

func HashString(s string) string

func Marshal added in v0.3.0

func Marshal(args []any) ([]byte, error)

func Ptr added in v0.3.2

func Ptr[a any](v a) *a

func StructToMap

func StructToMap(value any) (map[string]any, error)

func Unmarshal added in v0.3.0

func Unmarshal(data []byte, values []any) error

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) Decode

func (b BoolType) Decode(r *bytes.Reader) (any, error)

Decode decodes a bool value from the given reader.

func (BoolType) EncodeType

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

EncodeType returns the leb128 encoding of the bool type.

func (BoolType) EncodeValue

func (BoolType) EncodeValue(v any) ([]byte, error)

EncodeValue encodes a bool value. Accepted types are: `bool`.

func (BoolType) String

func (BoolType) String() string

String returns the string representation of the type.

func (BoolType) UnmarshalGo added in v0.3.0

func (BoolType) UnmarshalGo(raw any, _v any) error

type DecodeError

type DecodeError struct {
	Types       TupleType
	Description string
}

func (DecodeError) Error

func (e DecodeError) Error() string

type Empty

type Empty struct{}

Empty represents the empty value.

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) Decode

func (EmptyType) Decode(*bytes.Reader) (any, error)

Decode returns an error, as the empty type cannot be decoded.

func (EmptyType) EncodeType

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

EncodeType returns the leb128 encoding of the empty type.

func (EmptyType) EncodeValue

func (EmptyType) EncodeValue(_ any) ([]byte, error)

EncodeValue returns an empty byte slice.

func (EmptyType) String

func (EmptyType) String() string

String returns the string representation of the type.

func (EmptyType) UnmarshalGo added in v0.3.0

func (EmptyType) UnmarshalGo(raw any, _v any) error

type EncodeValueError

type EncodeValueError struct {
	Expected int64
	Value    any
}
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) Base

func (f FloatType) Base() uint

Base returns the base type of the float type. Either `4` (32) or `8` (64).

func (FloatType) Decode

func (f FloatType) Decode(r *bytes.Reader) (any, error)

Decode decodes a float value from the given reader.

func (FloatType) EncodeType

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

EncodeType returns the leb128 encoding of the FloatType.

func (FloatType) EncodeValue

func (f FloatType) EncodeValue(v any) ([]byte, error)

EncodeValue encodes a float value. Accepted types are: `float32` and `float64`.

func (FloatType) String

func (f FloatType) String() string

String returns the string representation of the type.

func (FloatType) UnmarshalGo added in v0.3.0

func (f FloatType) UnmarshalGo(raw any, _v any) error

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) Decode

func (f FunctionType) Decode(r *bytes.Reader) (any, 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 IDLFactory = func(types IDL) *Service

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 NewBigInt

func NewBigInt(bi *big.Int) Int

NewBigInt creates a new Int from a big.Int.

func NewInt

func NewInt[number Integer](i number) Int

NewInt creates a new Int from any integer.

func NewIntFromString

func NewIntFromString(n string) Int

NewIntFromString creates a new Int from a string.

func (Int) BigInt

func (i Int) BigInt() *big.Int

BigInt returns the underlying big.Int.

func (Int) String

func (i Int) String() string

String returns the string representation of the Int.

type IntType

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

IntType is either a type of int8, int16, int32, int64, or int.

func Int16Type

func Int16Type() *IntType

Int16Type returns a type of int16.

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 Int64Type

func Int64Type() *IntType

Int64Type returns a type of int64.

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) Base

func (n IntType) Base() uint

Base returns the base type of the IntType.

func (IntType) Decode

func (n IntType) Decode(r *bytes.Reader) (any, error)

Decode decodes an integer from the given reader.

func (IntType) EncodeType

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

EncodeType returns the leb128 encoding of the IntType.

func (IntType) EncodeValue

func (n IntType) EncodeValue(v any) ([]byte, error)

EncodeValue encodes an int value. Accepted types are: `int`, `int8`, `int16`, `int32`, `int64`, `Int`.

func (IntType) String

func (n IntType) String() string

String returns the string representation of the type.

func (IntType) UnmarshalGo added in v0.3.0

func (n IntType) UnmarshalGo(raw any, _v any) error

type Integer

type Integer interface {
	int | int64 | int32 | int16 | int8
}

Integer contains all integer types.

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 NewBigNat

func NewBigNat(bi *big.Int) Nat

NewBigNat creates a new Nat from a big.Int.

func NewNat

func NewNat[number Natural](n number) Nat

NewNat creates a new Nat from any unsigned integer.

func NewNatFromString

func NewNatFromString(n string) Nat

NewNatFromString creates a new Nat from a string.

func (Nat) BigInt

func (n Nat) BigInt() *big.Int

BigInt returns the underlying big.Int.

func (Nat) String

func (n Nat) String() string

String returns the string representation of the Nat.

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) Base

func (n NatType) Base() uint

Base returns the base type of the NatType.

func (NatType) Decode

func (n NatType) Decode(r *bytes.Reader) (any, error)

Decode decodes an unsigned integer from the given reader.

func (NatType) EncodeType

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

EncodeType returns the leb128 encoding of the NatType.

func (NatType) EncodeValue

func (n NatType) EncodeValue(v any) ([]byte, error)

EncodeValue encodes an nat value. Accepts: `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `Nat`.

func (NatType) String

func (n NatType) String() string

String returns the string representation of the NatType.

func (NatType) UnmarshalGo added in v0.3.0

func (n NatType) UnmarshalGo(raw any, _v any) error

type Natural

type Natural interface {
	uint | uint64 | uint32 | uint16 | uint8
}

Natural contains all unsigned integer types.

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) Decode

func (NullType) Decode(_ *bytes.Reader) (any, error)

func (NullType) EncodeType

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

func (NullType) EncodeValue

func (NullType) EncodeValue(v any) ([]byte, error)

func (NullType) String

func (NullType) String() string

func (NullType) UnmarshalGo added in v0.3.0

func (NullType) UnmarshalGo(raw any, _v any) 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 PrimType

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

type PrincipalMethod

type PrincipalMethod struct {
	Principal principal.Principal
	Method    string
}

type PrincipalType

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

func (PrincipalType) AddTypeDefinition

func (PrincipalType) AddTypeDefinition(_ *TypeDefinitionTable) error

func (PrincipalType) Decode

func (PrincipalType) Decode(r *bytes.Reader) (any, 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) Decode

func (record RecordType) Decode(r_ *bytes.Reader) (any, 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 Reserved

type Reserved struct{}

type ReservedType

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

func (ReservedType) AddTypeDefinition

func (ReservedType) AddTypeDefinition(_ *TypeDefinitionTable) error

func (ReservedType) Decode

func (ReservedType) Decode(*bytes.Reader) (any, 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) Decode

func (s Service) Decode(r *bytes.Reader) (any, error)

func (Service) EncodeType

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

func (Service) EncodeValue

func (s Service) EncodeValue(v any) ([]byte, error)

func (Service) String

func (s Service) String() string

func (Service) UnmarshalGo added in v0.3.0

func (Service) UnmarshalGo(raw any, _v any) error

type Tag added in v0.3.0

type Tag struct {
	// Name is the name of the field in the struct.
	Name        string
	VariantType bool
}

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) Decode

func (TextType) Decode(r *bytes.Reader) (any, error)

Decode decodes the value from the given reader into a string.

func (TextType) EncodeType

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

EncodeType encodes the type into a byte slice.

func (TextType) EncodeValue

func (TextType) EncodeValue(v any) ([]byte, error)

EncodeValue encodes the value into a byte slice.

func (TextType) String

func (TextType) String() string

String returns the string representation of the type.

func (TextType) UnmarshalGo added in v0.3.0

func (t TextType) UnmarshalGo(raw any, _v any) error

type TupleType

type TupleType []Type

TupleType is a collection of types.

func (TupleType) String

func (ts TupleType) String() string

String returns the string representation of the type.

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 Decode

func Decode(bs []byte) ([]Type, []any, error)

func TypeOf

func TypeOf(v any) (Type, error)
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

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

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

type UnmarshalGoError struct {
	Raw any
	V   any
}

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 Variant

type Variant struct {
	Name  string
	Value any
	Type  Type
}

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) Decode

func (variant VariantType) Decode(r *bytes.Reader) (any, 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) Decode

func (vec VectorType) Decode(r *bytes.Reader) (any, 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

Jump to

Keyboard shortcuts

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