Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 9 & 10 #11

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions candid/idl/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (vec VectorType) UnmarshalGo(raw any, _v any) error {
return nil
}

if v.Kind() == reflect.Slice && v.IsNil() {
// Create new slice.
v.Set(reflect.MakeSlice(v.Type(), 0, 0))
}

rv := reflect.ValueOf(raw)
if v.Kind() == reflect.Array {
// Check if array is big enough.
Expand Down
26 changes: 25 additions & 1 deletion candid/idl/vector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package idl_test

import (
"errors"
"github.com/aviate-labs/agent-go/candid/idl"
"testing"
)
Expand All @@ -18,7 +19,8 @@ func TestVectorType_UnmarshalGo(t *testing.T) {
if err == nil {
t.Fatal("expected error")
} else {
_, ok := err.(*idl.UnmarshalGoError)
var unmarshalGoError *idl.UnmarshalGoError
ok := errors.As(err, &unmarshalGoError)
if !ok {
t.Fatal("expected UnmarshalGoError")
}
Expand Down Expand Up @@ -87,3 +89,25 @@ func TestVectorType_UnmarshalGo(t *testing.T) {
}).UnmarshalGo([2]any{}, &nv))
})
}

func TestVectorType_empty(t *testing.T) {
typ := idl.VectorType{Type: idl.Nat8Type()}

var x []byte
t.Run("non-nil", func(t *testing.T) {
if err := typ.UnmarshalGo([]byte{}, &x); err != nil {
t.Fatal(err)
}
if x == nil {
t.Error("expected non-nil")
}
})
t.Run("nil", func(t *testing.T) {
if err := typ.UnmarshalGo(nil, &x); err != nil {
t.Fatal(err)
}
if x != nil {
t.Error("expected nil")
}
})
}
2 changes: 1 addition & 1 deletion gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func (g *Generator) dataToString(prefix string, data did.Data) string {
if strings.HasPrefix(r.typ, "*") {
tag += ",omitempty" // optional value.
}
record += fmt.Sprintf("\t%-*s %-*s `ic:\"%s\"`\n", sizeName, r.name, sizeType, r.typ, tag)
record += fmt.Sprintf("\t%-*s %-*s `ic:\"%s\" json:\"%s\"`\n", sizeName, r.name, sizeType, r.typ, tag, tag)
}
return fmt.Sprintf("struct {\n%s}", record)
case did.Variant:
Expand Down
Loading