Commit 749cf35c authored by Eric Myhre's avatar Eric Myhre

Wiring JSON and CBOR to multicodec in repose! Yas!

Finally.

This is "easy" now that we've got all the generic marshalling
implemented over Node and unmarshalling onto NodeBuilder:
we just glue those pieces together with the JSON and CBOR
parsers and emitters of refmt, and we're off.

These methods in the repose package further conform this to
byte-wise reader and writer interfaces so we can squeeze them
into Multicodec{En|De}codeTable... and thence feed them
into ComposeLink{Loader|Builder} and USE them!

And by use them, I mean not just use them, but use them transparently
and without a fuss, even from the very high level Traverse and
Transform methods, which let users manipulate Nodes and NodeBuilders
with *no idea* of all the details of serialization down here.

Which is awesome.

(Okay, there's a few more todo entries to sort out in the link
handling stuff.  Probably several.)

But this should about wrap it up with encoding stuff!  I'm going
to celebrate with a branch merge, and consider the main topic to
be lifted back up to the Traverse stuff and how to integrate
cleanly with link loaders and link builders.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent f150a81b
package repose
import (
"io"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/encoding"
"github.com/polydawn/refmt/cbor"
)
var (
_ MulticodecDecoder = DecoderDagCbor
_ MulticodecEncoder = EncoderDagCbor
)
func DecoderDagCbor(nb ipld.NodeBuilder, r io.Reader) (ipld.Node, error) {
// Probe for a builtin fast path. Shortcut to that if possible.
// (ipldcbor.NodeBuilder supports this, for example.)
type detectFastPath interface {
DecodeCbor(io.Reader) (ipld.Node, error)
}
if nb2, ok := nb.(detectFastPath); ok {
return nb2.DecodeCbor(r)
}
// Okay, generic builder path.
return encoding.Unmarshal(nb, cbor.NewDecoder(r))
}
func EncoderDagCbor(n ipld.Node, w io.Writer) error {
// Probe for a builtin fast path. Shortcut to that if possible.
// (ipldcbor.Node supports this, for example.)
type detectFastPath interface {
EncodeCbor(io.Writer) error
}
if n2, ok := n.(detectFastPath); ok {
return n2.EncodeCbor(w)
}
// Okay, generic inspection path.
return encoding.Marshal(n, cbor.NewEncoder(w))
}
package repose
import (
"io"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/encoding"
"github.com/polydawn/refmt/json"
)
var (
_ MulticodecDecoder = DecoderDagJson
_ MulticodecEncoder = EncoderDagJson
)
func DecoderDagJson(nb ipld.NodeBuilder, r io.Reader) (ipld.Node, error) {
// Shell out directly to generic builder path.
// (There's not really any fastpaths of note for json.)
return encoding.Unmarshal(nb, json.NewDecoder(r))
}
func EncoderDagJson(n ipld.Node, w io.Writer) error {
// Shell out directly to generic inspection path.
// (There's not really any fastpaths of note for json.)
// Write another function if you need to tune encoding options about whitespace.
return encoding.Marshal(n, json.NewEncoder(w, json.EncodeOptions{
Line: []byte{'\n'},
Indent: []byte{'\t'},
}))
}
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