Skip to content

Commit

Permalink
Unify marshal and idl package.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 28, 2023
1 parent c33c8c3 commit 496cfee
Show file tree
Hide file tree
Showing 31 changed files with 272 additions and 1,490 deletions.
5 changes: 2 additions & 3 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/aviate-labs/agent-go/candid"
"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/candid/marshal"
"github.com/aviate-labs/agent-go/certificate"
"github.com/aviate-labs/agent-go/identity"
"github.com/aviate-labs/agent-go/principal"
Expand Down Expand Up @@ -88,7 +87,7 @@ func (a Agent) Call(canisterID principal.Principal, methodName string, args []by
if err != nil {
return err
}
return marshal.Unmarshal(raw, values)
return idl.Unmarshal(raw, values)
}

// CallCandid calls a method on a canister and returns the raw Candid result as a list of types and values.
Expand Down Expand Up @@ -180,7 +179,7 @@ func (a Agent) Query(canisterID principal.Principal, methodName string, args []b
if err != nil {
return err
}
return marshal.Unmarshal(raw, values)
return idl.Unmarshal(raw, values)
}

// QueryCandid queries a method on a canister and returns the raw Candid result as a list of types and values.
Expand Down
47 changes: 47 additions & 0 deletions candid/idl/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,53 @@ import (
"github.com/aviate-labs/leb128"
)

func Unmarshal(data []byte, values []any) error {
ts, vs, err := Decode(data)
if err != nil {
return err
}
if len(ts) != len(vs) {
return fmt.Errorf("unequal data types and value lengths: %d %d", len(ts), len(vs))
}

if len(vs) != len(values) {
return fmt.Errorf("unequal value lengths: %d %d", len(vs), len(values))
}

for i, v := range values {
if err := ts[i].UnmarshalGo(vs[i], v); err != nil {
return err
}
}

return nil
}

func checkIsPtr(_v any) (reflect.Value, bool) {
v := reflect.ValueOf(_v)
if v.Kind() != reflect.Ptr {
return v, false
}
v = v.Elem()
if v.Kind() == reflect.Interface {
v = v.Elem()
}
return v, true
}

func checkIsPtrPrim(_v any) (reflect.Value, reflect.Type, bool) {
v := reflect.ValueOf(_v)
if v.Kind() != reflect.Ptr {
return v, v.Type(), false
}
v = v.Elem()
t := v.Type()
if v.Kind() == reflect.Interface {
t = v.Elem().Type()
}
return v, t, true
}

func Decode(bs []byte) ([]Type, []any, error) {
if len(bs) == 0 {
return nil, nil, &FormatError{
Expand Down
12 changes: 12 additions & 0 deletions candid/idl/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ func Encode(argumentTypes []Type, arguments []any) ([]byte, error) {
vs,
), nil
}

func Marshal(args []any) ([]byte, error) {
var types []Type
for _, a := range args {
t, err := TypeOf(a)
if err != nil {
return nil, err
}
types = append(types, t)
}
return Encode(types, args)
}
32 changes: 19 additions & 13 deletions candid/idl/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"encoding/binary"
"fmt"
"math/big"

"github.com/aviate-labs/leb128"
"math/big"
"reflect"
)

func anyToUint16(v any) (uint16, bool) {
Expand Down Expand Up @@ -369,21 +369,27 @@ func (n NatType) UnmarshalGo(raw any, _v any) error {
if !ok {
return NewUnmarshalGoError(raw, _v)
}
switch v := _v.(type) {
case *uint8:
*v = u8
v, t, ok := checkIsPtrPrim(_v)
if !ok {
return NewUnmarshalGoError(raw, _v)
}
switch t.Kind() {
case reflect.Uint8:
v.Set(reflect.ValueOf(u8))
return nil
case *Nat:
*v = NewNat(u8)
case reflect.Uint64:
v.Set(reflect.ValueOf(uint64(u8)))
return nil
case *uint64:
*v = uint64(u8)
case reflect.Uint32:
v.Set(reflect.ValueOf(uint32(u8)))
return nil
case *uint32:
*v = uint32(u8)
case reflect.Uint16:
v.Set(reflect.ValueOf(uint16(u8)))
return nil
case *uint16:
*v = uint16(u8)
}
switch v := _v.(type) {
case *Nat:
*v = NewNat(u8)
return nil
default:
return NewUnmarshalGoError(raw, _v)
Expand Down
6 changes: 3 additions & 3 deletions candid/idl/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ func (record RecordType) UnmarshalGo(raw any, _v any) error {
if v, ok := _v.(*map[string]any); ok {
return record.unmarshalMap(m, v)
}
v := reflect.ValueOf(_v)
if v.Kind() != reflect.Ptr {

v, ok := checkIsPtr(_v)
if !ok {
return NewUnmarshalGoError(raw, _v)
}
v = v.Elem()
if v.Kind() != reflect.Struct {
return NewUnmarshalGoError(raw, _v)
}
Expand Down
6 changes: 3 additions & 3 deletions candid/idl/variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ func (variant VariantType) UnmarshalGo(raw any, _v any) error {
if v, ok := _v.(*map[string]any); ok {
return variant.unmarshalMap(name, value, v)
}
v := reflect.ValueOf(_v)
if v.Kind() != reflect.Ptr {

v, ok := checkIsPtr(_v)
if !ok {
return NewUnmarshalGoError(raw, _v)
}
v = v.Elem()
if v.Kind() != reflect.Struct {
return NewUnmarshalGoError(raw, _v)
}
Expand Down
6 changes: 3 additions & 3 deletions candid/idl/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ func (vec VectorType) String() string {
}

func (vec VectorType) UnmarshalGo(raw any, _v any) error {
v := reflect.ValueOf(_v)
if v.Kind() != reflect.Ptr {
v, ok := checkIsPtr(_v)
if !ok {
return NewUnmarshalGoError(raw, _v)
}
v = v.Elem()

if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
return NewUnmarshalGoError(raw, _v)
}
Expand Down
88 changes: 0 additions & 88 deletions candid/marshal/cons.go

This file was deleted.

22 changes: 0 additions & 22 deletions candid/marshal/cons_test.go

This file was deleted.

32 changes: 0 additions & 32 deletions candid/marshal/context.go

This file was deleted.

28 changes: 0 additions & 28 deletions candid/marshal/decode.go

This file was deleted.

Loading

0 comments on commit 496cfee

Please sign in to comment.