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

Add toInterface #304

Merged
merged 5 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
65 changes: 65 additions & 0 deletions fluent/toInterfaceValue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fluent

import (
"errors"

"github.com/ipld/go-ipld-prime/datamodel"
)

var ErrInvalidKind = errors.New("invalid kind")
var ErrUnknownKind = errors.New("unknown kind")
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved

func ToInterface(node datamodel.Node) (interface{}, error) {
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
switch k := node.Kind(); k {
case datamodel.Kind_Invalid:
return nil, ErrInvalidKind
case datamodel.Kind_Null:
return nil, nil
case datamodel.Kind_Bool:
return node.AsBool()
case datamodel.Kind_Int:
return node.AsInt()
case datamodel.Kind_Float:
return node.AsFloat()
case datamodel.Kind_String:
return node.AsString()
case datamodel.Kind_Bytes:
return node.AsBytes()
case datamodel.Kind_Link:
return node.AsLink()
case datamodel.Kind_Map:
outMap := make(map[string]interface{}, node.Length())
for mi := node.MapIterator(); !mi.Done(); {
k, v, err := mi.Next()
if err != nil {
return nil, err
}
kVal, err := k.AsString()
mvdan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
vVal, err := ToInterface(v)
if err != nil {
return nil, err
}
outMap[kVal] = vVal
}
return outMap, nil
case datamodel.Kind_List:
outList := make([]interface{}, 0, node.Length())
for li := node.ListIterator(); !li.Done(); {
_, v, err := li.Next()
if err != nil {
return nil, err
}
vVal, err := ToInterface(v)
if err != nil {
return nil, err
}
outList = append(outList, vVal)
}
return outList, nil
default:
return nil, ErrUnknownKind
}
}
51 changes: 51 additions & 0 deletions fluent/toInterfaceValue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fluent_test

import (
"encoding/json"
"testing"

qt "github.com/frankban/quicktest"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
)

var roundTripTestCases = []struct {
desc string
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
value interface{}
}{
{desc: "Number", value: 100},
{desc: "String", value: "hi"},
{desc: "Bool", value: "hi"},
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
{desc: "Bytes", value: []byte("hi")},
{desc: "Map", value: map[string]interface{}{"a": "1", "b": int64(2), "c": 3.14, "d": true}},
{desc: "Array", value: []string{"a", "b", "c"}},
}

func TestRoundTrip(t *testing.T) {
for _, testCase := range roundTripTestCases {
t.Run(testCase.desc, func(t *testing.T) {
c := qt.New(t)
n, err := fluent.Reflect(basicnode.Prototype.Any, testCase.value)
c.Assert(err, qt.IsNil)
out, err := fluent.ToInterface(n)
c.Assert(err, qt.IsNil)
outJson, err := json.Marshal(out)
qt.Check(t, err, qt.IsNil)
qt.Check(t, outJson, qt.JSONEquals, testCase.value)
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

func TestLink(t *testing.T) {
c := qt.New(t)
var someCid, err = cid.Parse("bafybeihrqe2hmfauph5yfbd6ucv7njqpiy4tvbewlvhzjl4bhnyiu6h7pm")
c.Assert(err, qt.IsNil)
var link = cidlink.Link{Cid: someCid}
var node = basicnode.NewLink(link)
MarcoPolo marked this conversation as resolved.
Show resolved Hide resolved
v, err := fluent.ToInterface(node)
c.Assert(err, qt.IsNil)

c.Assert(v.(cidlink.Link), qt.Equals, link)
}