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

feat(dagcbor): mode to allow parsing undelimited streamed objects #490

Merged
merged 1 commit into from
Feb 10, 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
43 changes: 43 additions & 0 deletions codec/dagcbor/nongreedy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dagcbor

import (
"bytes"
"encoding/hex"
"testing"

qt "github.com/frankban/quicktest"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/fluent/qp"
"github.com/ipld/go-ipld-prime/node/basicnode"
)

func TestNonGreedy(t *testing.T) {
// same as JSON version of this test: {"a": 1}{"b": 2}
buf, err := hex.DecodeString("a1616101a1616202")
qt.Assert(t, err, qt.IsNil)
r := bytes.NewReader(buf)
opts := DecodeOptions{
DontParseBeyondEnd: true,
}

// first object
nb1 := basicnode.Prototype.Map.NewBuilder()
err = opts.Decode(nb1, r)
qt.Assert(t, err, qt.IsNil)
expected, err := qp.BuildMap(basicnode.Prototype.Any, 1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "a", qp.Int(1))
})
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, ipld.DeepEqual(nb1.Build(), expected), qt.IsTrue)

// second object
nb2 := basicnode.Prototype.Map.NewBuilder()
err = opts.Decode(nb2, r)
qt.Assert(t, err, qt.IsNil)
expected, err = qp.BuildMap(basicnode.Prototype.Any, 1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "b", qp.Int(2))
})
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, ipld.DeepEqual(nb2.Build(), expected), qt.IsTrue)
}
12 changes: 12 additions & 0 deletions codec/dagcbor/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ type DecodeOptions struct {
//
// Note that this option is experimental as it only implements partial strictness.
ExperimentalDeterminism bool

// If true, the decoder stops reading from the stream at the end of a full,
// valid CBOR object. This may be useful for parsing a stream of undelimited
// CBOR objects.
// As per standard IPLD behavior, in the default mode the parser considers the
// entire block to be part of the CBOR object and will error if there is
// extraneous data after the end of the object.
DontParseBeyondEnd bool
}

// Decode deserializes data from the given io.Reader and feeds it into the given datamodel.NodeAssembler.
Expand All @@ -77,6 +85,10 @@ func (cfg DecodeOptions) Decode(na datamodel.NodeAssembler, r io.Reader) error {
return err
}

if cfg.DontParseBeyondEnd {
return nil
}

var buf [1]byte
_, err = io.ReadFull(r, buf[:])
switch err {
Expand Down