Commit b5a8551a authored by hannahhoward's avatar hannahhoward

fix(encoding): fix decode for simple nodes

Decoding of nodes that are just simple values was erroring for both CBOR & JSON. This fixes the
issue as well as fixing it in the general unmarshal.
parent 1dac0811
...@@ -43,3 +43,19 @@ func TestRoundtrip(t *testing.T) { ...@@ -43,3 +43,19 @@ func TestRoundtrip(t *testing.T) {
Wish(t, n2, ShouldEqual, n) Wish(t, n2, ShouldEqual, n)
}) })
} }
func TestRoundtripSimple(t *testing.T) {
simple := fnb.CreateString("applesauce")
t.Run("encoding", func(t *testing.T) {
var buf bytes.Buffer
err := Encoder(simple, &buf)
Wish(t, err, ShouldEqual, nil)
Wish(t, buf.String(), ShouldEqual, `japplesauce`)
})
t.Run("decoding", func(t *testing.T) {
buf := bytes.NewBufferString(`japplesauce`)
simple2, err := Decoder(ipldfree.NodeBuilder(), buf)
Wish(t, err, ShouldEqual, nil)
Wish(t, simple2, ShouldEqual, simple)
})
}
...@@ -24,7 +24,10 @@ var ( ...@@ -24,7 +24,10 @@ var (
func Unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource) (ipld.Node, error) { func Unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource) (ipld.Node, error) {
var tk tok.Token var tk tok.Token
done, err := tokSrc.Step(&tk) done, err := tokSrc.Step(&tk)
if done || err != nil { if err != nil {
return nil, err
}
if done && !tk.Type.IsValue() {
return nil, err return nil, err
} }
return unmarshal(nb, tokSrc, &tk) return unmarshal(nb, tokSrc, &tk)
......
...@@ -37,7 +37,7 @@ func Decoder(nb ipld.NodeBuilder, r io.Reader) (ipld.Node, error) { ...@@ -37,7 +37,7 @@ func Decoder(nb ipld.NodeBuilder, r io.Reader) (ipld.Node, error) {
for { for {
_, err := r.Read(buf[:]) _, err := r.Read(buf[:])
switch buf[0] { switch buf[0] {
case ' ', '\t', '\r', '\n': // continue case ' ', 0x0, '\t', '\r', '\n': // continue
default: default:
return n, fmt.Errorf("unexpected content after end of json object") return n, fmt.Errorf("unexpected content after end of json object")
} }
......
...@@ -59,3 +59,19 @@ func TestRoundtrip(t *testing.T) { ...@@ -59,3 +59,19 @@ func TestRoundtrip(t *testing.T) {
Wish(t, n2, ShouldEqual, n) Wish(t, n2, ShouldEqual, n)
}) })
} }
func TestRoundtripSimple(t *testing.T) {
simple := fnb.CreateString("applesauce")
t.Run("encoding", func(t *testing.T) {
var buf bytes.Buffer
err := Encoder(simple, &buf)
Wish(t, err, ShouldEqual, nil)
Wish(t, buf.String(), ShouldEqual, `"applesauce"`)
})
t.Run("decoding", func(t *testing.T) {
buf := bytes.NewBufferString(`"applesauce"`)
simple2, err := Decoder(ipldfree.NodeBuilder(), buf)
Wish(t, err, ShouldEqual, nil)
Wish(t, simple2, ShouldEqual, simple)
})
}
...@@ -19,7 +19,10 @@ import ( ...@@ -19,7 +19,10 @@ import (
func Unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource) (ipld.Node, error) { func Unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource) (ipld.Node, error) {
var tk tok.Token var tk tok.Token
done, err := tokSrc.Step(&tk) done, err := tokSrc.Step(&tk)
if done || err != nil { if err != nil {
return nil, err
}
if done && !tk.Type.IsValue() {
return nil, err return nil, err
} }
return unmarshal(nb, tokSrc, &tk) return unmarshal(nb, tokSrc, &tk)
......
...@@ -30,7 +30,10 @@ import ( ...@@ -30,7 +30,10 @@ import (
func Unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource) (ipld.Node, error) { func Unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource) (ipld.Node, error) {
var tk tok.Token var tk tok.Token
done, err := tokSrc.Step(&tk) done, err := tokSrc.Step(&tk)
if done || err != nil { if err != nil {
return nil, err
}
if done && !tk.Type.IsValue() {
return nil, err return nil, err
} }
return unmarshal(nb, tokSrc, &tk) return unmarshal(nb, tokSrc, &tk)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment