cbornode

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2024 License: MIT Imports: 21 Imported by: 476

README

go-ipld-cbor

Coverage Status Travis CI

An implementation of a cbor encoded merkledag object.

Status

This library has alternatives available: For new projects, prefer using the cbor codec included with go-ipld-prime.

This library is in standby mode. It works, but we recommend migrating to alternatives if possible. New features are unlikely to be added here.

Lead Maintainer

Eric Myhre

Table of Contents

Install

make install

Usage

Note: This package isn't the easiest to use.

// Make an object
obj := map[interface{}]interface{}{
	"foo": "bar",
	"baz": &Link{
		Target: myCid,
	},
}

// Parse it into an ipldcbor node
nd, err := WrapMap(obj)

fmt.Println(nd.Links())

Contribute

PRs are welcome!

Small note: If editing the Readme, please conform to the standard-readme specification.

License

MIT © Jeromy Johnson

Documentation

Index

Constants

View Source
const CBORTagLink = 42

CBORTagLink is the integer used to represent tags in CBOR.

View Source
const DefaultMultihash = uint64(mh.BLAKE2B_MIN + 31)

Variables

View Source
var (
	// ErrNoSuchLink is returned when no link with the given name was found.
	ErrNoSuchLink       = errors.New("no such link found")
	ErrNonLink          = errors.New("non-link found at given path")
	ErrInvalidLink      = errors.New("link value should have been bytes")
	ErrInvalidKeys      = errors.New("map keys must be strings")
	ErrArrayOutOfRange  = errors.New("array index out of range")
	ErrNoLinks          = errors.New("tried to resolve through object that had no links")
	ErrEmptyLink        = errors.New("link value was empty")
	ErrInvalidMultibase = errors.New("invalid multibase on IPLD link")
	ErrNonStringLink    = errors.New("link should have been a string")
)
View Source
var BigIntAtlasEntry = atlas.BuildEntry(big.Int{}).Transform().
	TransformMarshal(atlas.MakeMarshalTransformFunc(
		func(i big.Int) ([]byte, error) {
			return i.Bytes(), nil
		})).
	TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
		func(x []byte) (big.Int, error) {
			return *big.NewInt(0).SetBytes(x), nil
		})).
	Complete()

BigIntAtlasEntry gives a reasonable default encoding for big.Int. It is not included in the entries by default.

View Source
var CborAtlas atlas.Atlas

CborAtlas is the refmt.Atlas used by the CBOR IPLD decoder/encoder.

Functions

func DecodeBlock

func DecodeBlock(block blocks.Block) (node.Node, error)

DecodeBlock decodes a CBOR encoded Block into an IPLD Node.

This method *does not* canonicalize and *will* preserve the CID. As a matter of fact, it will assume that `block.Cid()` returns the correct CID and will make no effort to validate this assumption.

In general, you should not be calling this method directly. Instead, you should be calling the `Decode` method from the `go-ipld-format` package. That method will pick the right decoder based on the Block's CID.

Note: This function keeps a reference to `block` and assumes that it is immutable.

func DecodeInto

func DecodeInto(b []byte, v interface{}) error

DecodeInto decodes a serialized IPLD cbor object into the given object.

func DecodeReader added in v0.0.3

func DecodeReader(r io.Reader, v interface{}) error

DecodeReader reads from the given reader and decodes a serialized IPLD cbor object into the given object.

func DumpObject

func DumpObject(obj interface{}) (out []byte, err error)

DumpObject marshals any object into its CBOR serialized byte representation Deprecated: use Encode instead.

func Encode added in v0.2.0

func Encode(obj interface{}) (out []byte, err error)

Encode marshals any object into its CBOR serialized byte representation

func EncodeWriter added in v0.2.0

func EncodeWriter(obj interface{}, w io.Writer) error

EncodeWriter marshals into the writer any object as its CBOR serialized byte representation.

func HumanReadable

func HumanReadable(blob []byte) (string, error)

HumanReadable returns a string representation of a CBOR blob

func NewSerializationError added in v0.0.4

func NewSerializationError(err error) error

func RegisterCborType

func RegisterCborType(i interface{})

RegisterCborType allows to register a custom cbor type

Types

type BasicIpldStore added in v0.0.4

type BasicIpldStore struct {
	Blocks IpldBlockstore
	Viewer IpldBlockstoreViewer

	Atlas *atlas.Atlas

	DefaultMultihash uint64
}

BasicIpldStore wraps and IpldBlockstore and implements the IpldStore interface.

func NewCborStore added in v0.0.4

func NewCborStore(bs IpldBlockstore) *BasicIpldStore

NewCborStore returns an IpldStore implementation backed by the provided IpldBlockstore.

func (*BasicIpldStore) Get added in v0.0.4

func (s *BasicIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) error

Get reads and unmarshals the content at `c` into `out`.

func (*BasicIpldStore) Put added in v0.0.4

func (s *BasicIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)

Put marshals and writes content `v` to the backing blockstore returning its CID.

type IpldBlockstore added in v0.0.4

type IpldBlockstore interface {
	Get(context.Context, cid.Cid) (block.Block, error)
	Put(context.Context, block.Block) error
}

IpldBlockstore defines a subset of the go-ipfs-blockstore Blockstore interface providing methods for storing and retrieving block-centered data.

type IpldBlockstoreViewer added in v0.0.5

type IpldBlockstoreViewer interface {
	// View provides zero-copy access to blocks in a blockstore. The callback
	// function will be invoked with the value for the key. The user MUST not
	// modify the byte array, as it could be memory-mapped.
	View(cid.Cid, func([]byte) error) error
}

IpldBlockstoreViewer is a trait that enables zero-copy access to blocks in a blockstore.

type IpldStore added in v0.0.4

type IpldStore interface {
	Get(ctx context.Context, c cid.Cid, out interface{}) error
	Put(ctx context.Context, v interface{}) (cid.Cid, error)
}

IpldStore wraps a Blockstore and provides an interface for storing and retrieving CBOR encoded data.

func NewMemCborStore added in v0.0.5

func NewMemCborStore() IpldStore

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents an IPLD node.

func Decode

func Decode(b []byte, mhType uint64, mhLen int) (*Node, error)

Decode decodes a CBOR object into an IPLD Node.

If passed a non-canonical CBOR node, this function will canonicalize it. Therefore, `bytes.Equal(b, Decode(b).RawData())` may not hold. If you already have a CID for this data and want to ensure that it doesn't change, you should use `DecodeBlock`. mhType is multihash code to use for hashing, for example mh.SHA2_256

Note: This function does not hold onto `b`. You may reuse it.

func FromJSON

func FromJSON(r io.Reader, mhType uint64, mhLen int) (*Node, error)

FromJSON converts incoming JSON into a Node.

func WrapObject

func WrapObject(m interface{}, mhType uint64, mhLen int) (*Node, error)

WrapObject converts an arbitrary object into a Node.

func (*Node) Cid

func (n *Node) Cid() cid.Cid

Cid returns the canonical Cid of the NOde.

func (*Node) Copy

func (n *Node) Copy() node.Node

Copy creates a copy of the Node.

func (n *Node) Links() []*node.Link

Links lists all known links of the Node.

func (*Node) Loggable

func (n *Node) Loggable() map[string]interface{}

Loggable returns a loggable representation of the Node.

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

MarshalJSON converts the Node into its JSON representation.

func (*Node) RawData

func (n *Node) RawData() []byte

RawData returns the raw bytes that represent the Node as serialized CBOR.

func (*Node) Resolve

func (n *Node) Resolve(path []string) (interface{}, []string, error)

Resolve resolves a given path, and returns the object found at the end, as well as the possible tail of the path that was not resolved.

func (n *Node) ResolveLink(path []string) (*node.Link, []string, error)

ResolveLink resolves a path and returns the raw Link at the end, as well as the possible tail of the path that was not resolved.

func (*Node) Size

func (n *Node) Size() (uint64, error)

Size returns the size of the binary representation of the Node.

func (*Node) Stat

func (n *Node) Stat() (*node.NodeStat, error)

Stat returns stats about the Node. TODO: implement?

func (*Node) String

func (n *Node) String() string

String returns the string representation of the CID of the Node.

func (*Node) Tree

func (n *Node) Tree(path string, depth int) []string

Tree returns a flattend array of paths at the given path for the given depth.

type SerializationError added in v0.0.4

type SerializationError struct {
	// contains filtered or unexported fields
}

func (SerializationError) Error added in v0.0.4

func (se SerializationError) Error() string

func (SerializationError) Is added in v0.0.4

func (se SerializationError) Is(o error) bool

func (SerializationError) Unwrap added in v0.0.4

func (se SerializationError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL