multicodec

package
v0.0.0-...-8b9b725 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2019 License: MIT, MIT Imports: 4 Imported by: 52

README

Deprecated

NOTE: Multicodec is not deprecated, just this library.

This library has been deprecated to reduce confusion concerning the multicodec project. It is not an implementation of multicodec as it exists today (as described in the project's README). Instead, it's closer to the multistream protocol but we already have an actively used go-multistream repository (for multistream-select).

This library will not be maintained.


go-multicodec

Travis CI codecov.io GoDoc

multicodec implementation in Go.

Supported codecs
  • /cbor
  • /json
  • /msgio
  • /msgpack
  • /protobuf

Table of Contents

Install

go get github.com/multiformats/go-multicodec

Usage

Look at the Godocs:

import (
  "os"
  "io"

  cbor "github.com/multiformats/go-multicodec/cbor"
  json "github.com/multiformats/go-multicodec/json"
)

func main() {
  dec := cbor.Multicodec().Decoder(os.Stdin)
  enc := json.Multicodec(false).Encoder(os.Stdout)

  for {
    var item interface{}

    if err := dec.Decode(&item); err == io.EOF {
      break
    } else if err != nil {
      panic(err)
    }

    if err := enc.Encode(&item); err != nil {
      panic(err)
    }
  }
}

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

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

License

MIT © 2014 Juan Batiz-Benet

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrType          = errors.New("multicodec type error")
	ErrHeaderInvalid = errors.New("multicodec header invalid")
	ErrMismatch      = errors.New("multicodec did not match")
	ErrVarints       = errors.New("multicodec varints not yet implemented")
)

Functions

func ConsumeHeader

func ConsumeHeader(r io.Reader, header []byte) (err error)

ConsumeHeader reads a multicodec header from a reader, verifying it matches given header. If it does not, it returns ErrProtocolMismatch

func ConsumePath

func ConsumePath(r io.Reader, path []byte) (err error)

ConsumePath reads a multicodec header from a reader, verifying it matches given path. If it does not, it returns ErrProtocolMismatch

func Header(path []byte) []byte

Header returns a multicodec header with the given path.

func HeaderPath

func HeaderPath(hdr []byte) []byte

HeaderPath returns the multicodec path from header

func HeaderSafe

func HeaderSafe(path []byte) ([]byte, error)

HeaderSafe works like Header but it returns error instead of calling panic

func Marshal

func Marshal(c Codec, o interface{}) ([]byte, error)

Marshal serializes an object to a []byte.

func MarshalTo

func MarshalTo(c Codec, w io.Writer, o interface{}) error

MarshalTo serializes an object to a writer.

func ReadHeader

func ReadHeader(r io.Reader) (path []byte, err error)

ReadHeader reads a multicodec header from a reader. Returns the header found, or an error if the header mismatched.

func ReadPath

func ReadPath(r io.Reader) (path []byte, err error)

ReadPath reads a multicodec header from a reader. Returns the path found, or an error if the header mismatched.

func Unmarshal

func Unmarshal(c Codec, buf []byte, o interface{}) error

Unmarshal deserializes an object to a []byte.

func UnmarshalFrom

func UnmarshalFrom(c Codec, r io.Reader, o interface{}) error

UnmarshalFrom deserializes an objects from a reader.

func WrapHeaderReader

func WrapHeaderReader(hdr []byte, r io.Reader) io.Reader

WrapHeaderReader returns a reader that first reads the given header, and then the given reader, using io.MultiReader. It is useful if the header has been read through, but still needed to pass to a decoder.

func WrapTransformPathToHeader

func WrapTransformPathToHeader(r io.Reader) (io.Reader, error)

func WriteHeader

func WriteHeader(w io.Writer, path []byte) error

WriteHeader writes a multicodec header to a writer. It uses the given path.

Types

type Codec

type Codec interface {
	// Decoder wraps given io.Reader and returns an object which
	// will decode bytes into objects.
	Decoder(r io.Reader) Decoder

	// Encoder wraps given io.Writer and returns an Encoder
	Encoder(w io.Writer) Encoder
}

Codec is an algorithm for coding data from one representation to another. For convenience, we define a codec in the usual sense: a function and its inverse, to encode and decode.

type Decoder

type Decoder interface {
	Decode(n interface{}) error
}

Decoder decodes objects from bytes from an underlying io.Reader, into given object. Works like encoding.Unmarshal

type Encoder

type Encoder interface {
	Encode(n interface{}) error
}

Encoder encodes objects into bytes and writes them to an underlying io.Writer. Works like encoding.Marshal

type Multicodec

type Multicodec interface {
	Codec

	Header() []byte
}

Multicodec is the interface for a multicodec

func NewMulticodecFromCodec

func NewMulticodecFromCodec(c Codec, header []byte) Multicodec

Directories

Path Synopsis
b64
bin
hex
mux

Jump to

Keyboard shortcuts

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