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

Implement handling of Link and []byte in printer #294

Merged
merged 1 commit into from
Nov 10, 2021
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
13 changes: 11 additions & 2 deletions printer/printer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package printer

import (
"encoding/hex"
"io"
"os"
"strconv"
Expand Down Expand Up @@ -350,8 +351,16 @@ func (z *printBuf) doString(indentLevel int, printState uint8, n datamodel.Node)
z.writeString(strconv.QuoteToGraphic(x))
z.writeString("}")
case datamodel.Kind_Bytes:
panic("TODO")
x, _ := n.AsBytes()
z.writeString("{")
dst := make([]byte, hex.EncodedLen(len(x)))
hex.Encode(dst, x)
z.writeString(string(dst))
z.writeString("}")
case datamodel.Kind_Link:
panic("TODO")
x, _ := n.AsLink()
z.writeString("{")
z.writeString(x.String())
z.writeString("}")
}
}
84 changes: 67 additions & 17 deletions printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@ import (
qt "github.com/frankban/quicktest"
"github.com/warpfork/go-wish"

"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/fluent/qp"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"
)

var testLink = func() datamodel.Link {
someCid, _ := cid.Cast([]byte{1, 85, 0, 5, 0, 1, 2, 3, 4})
return cidlink.Link{Cid: someCid}
}()

func TestSimpleData(t *testing.T) {
n, _ := qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "some key", qp.String("some value"))
qp.MapEntry(ma, "another key", qp.String("another value"))
qp.MapEntry(ma, "nested map", qp.Map(2, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "deeper entries", qp.String("deeper values"))
qp.MapEntry(ma, "more deeper entries", qp.String("more deeper values"))
}))
qp.MapEntry(ma, "nested list", qp.List(2, func(la datamodel.ListAssembler) {
qp.ListEntry(la, qp.Int(1))
qp.ListEntry(la, qp.Int(2))
}))
})
qt.Check(t, Sprint(n), qt.CmpEquals(), wish.Dedent(`
t.Run("nested-maps", func(t *testing.T) {
n, _ := qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "some key", qp.String("some value"))
qp.MapEntry(ma, "another key", qp.String("another value"))
qp.MapEntry(ma, "nested map", qp.Map(2, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "deeper entries", qp.String("deeper values"))
qp.MapEntry(ma, "more deeper entries", qp.String("more deeper values"))
}))
qp.MapEntry(ma, "nested list", qp.List(2, func(la datamodel.ListAssembler) {
qp.ListEntry(la, qp.Int(1))
qp.ListEntry(la, qp.Int(2))
}))
})
qt.Check(t, Sprint(n), qt.CmpEquals(), wish.Dedent(`
map{
string{"some key"}: string{"some value"}
string{"another key"}: string{"another value"}
Expand All @@ -39,27 +47,69 @@ func TestSimpleData(t *testing.T) {
1: int{2}
}
}`,
))
))
})

t.Run("map-with-link-and-bytes", func(t *testing.T) {
n, _ := qp.BuildMap(basicnode.Prototype.Any, -1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "some key", qp.Link(testLink))
qp.MapEntry(ma, "another key", qp.String("another value"))
qp.MapEntry(ma, "nested map", qp.Map(2, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "deeper entries", qp.String("deeper values"))
qp.MapEntry(ma, "more deeper entries", qp.Link(testLink))
qp.MapEntry(ma, "yet another deeper entries", qp.Bytes([]byte("fish")))
}))
qp.MapEntry(ma, "nested list", qp.List(2, func(la datamodel.ListAssembler) {
qp.ListEntry(la, qp.Bytes([]byte("ghoti")))
qp.ListEntry(la, qp.Int(1))
qp.ListEntry(la, qp.Link(testLink))
}))
})
qt.Check(t, Sprint(n), qt.CmpEquals(), wish.Dedent(`
map{
string{"some key"}: link{bafkqabiaaebagba}
string{"another key"}: string{"another value"}
string{"nested map"}: map{
string{"deeper entries"}: string{"deeper values"}
string{"more deeper entries"}: link{bafkqabiaaebagba}
string{"yet another deeper entries"}: bytes{66697368}
}
string{"nested list"}: list{
0: bytes{67686f7469}
1: int{1}
2: link{bafkqabiaaebagba}
}
}`,
))
})
}

func TestTypedData(t *testing.T) {
t.Run("structs", func(t *testing.T) {
type FooBar struct {
Foo string
Bar string
Foo string
Bar string
Baz []byte
Jazz datamodel.Link
}
ts := schema.MustTypeSystem(
schema.SpawnString("String"),
schema.SpawnBytes("Bytes"),
schema.SpawnLink("Link"),
schema.SpawnStruct("FooBar", []schema.StructField{
schema.SpawnStructField("foo", "String", false, false),
schema.SpawnStructField("bar", "String", false, false),
schema.SpawnStructField("baz", "Bytes", false, false),
schema.SpawnStructField("jazz", "Link", false, false),
}, nil),
)
n := bindnode.Wrap(&FooBar{"x", "y"}, ts.TypeByName("FooBar"))
n := bindnode.Wrap(&FooBar{"x", "y", []byte("zed"), testLink}, ts.TypeByName("FooBar"))
qt.Check(t, Sprint(n), qt.CmpEquals(), wish.Dedent(`
struct<FooBar>{
foo: string<String>{"x"}
bar: string<String>{"y"}
baz: bytes<Bytes>{7a6564}
jazz: link<Link>{bafkqabiaaebagba}
}`,
))
})
Expand Down