Unverified Commit bd402871 authored by Eric Myhre's avatar Eric Myhre Committed by GitHub

Merge pull request #53 from ipld/bugs/cbor-marshall

Fix marshalling error
parents 50e2df1e 2edb45a4
......@@ -134,6 +134,7 @@ func marshal(n ipld.Node, tk *tok.Token, sink shared.TokenSink) error {
tk.Tagged = true
tk.Tag = linkTag
_, err = sink.Step(tk)
tk.Tagged = false
return err
default:
return fmt.Errorf("schemafree link emission only supported by this codec for CID type links!")
......
......@@ -2,11 +2,17 @@ package dagcbor
import (
"bytes"
"context"
"crypto/rand"
"io"
"testing"
cid "github.com/ipfs/go-cid"
. "github.com/warpfork/go-wish"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)
......@@ -62,3 +68,37 @@ func TestRoundtripScalar(t *testing.T) {
Wish(t, nb.Build(), ShouldEqual, simple)
})
}
func TestRoundtripLinksAndBytes(t *testing.T) {
lb := cidlink.LinkBuilder{cid.Prefix{
Version: 1,
Codec: 0x71,
MhType: 0x17,
MhLength: 4,
}}
buf := bytes.Buffer{}
lnk, err := lb.Build(context.Background(), ipld.LinkContext{}, n,
func(ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
return &buf, func(lnk ipld.Link) error { return nil }, nil
},
)
Require(t, err, ShouldEqual, nil)
var linkByteNode = fluent.MustBuildMap(basicnode.Style__Map{}, 4, func(na fluent.MapAssembler) {
nva := na.AssembleEntry("Link")
nva.AssignLink(lnk)
nva = na.AssembleEntry("Bytes")
bytes := make([]byte, 100)
_, _ = rand.Read(bytes)
nva.AssignBytes(bytes)
})
buf.Reset()
err = Encoder(linkByteNode, &buf)
Require(t, err, ShouldEqual, nil)
nb := basicnode.Style__Map{}.NewBuilder()
err = Decoder(nb, &buf)
Require(t, err, ShouldEqual, nil)
reconstructed := nb.Build()
Wish(t, reconstructed, ShouldEqual, linkByteNode)
}
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