cbor

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2019 License: BSL-1.0 Imports: 7 Imported by: 0

README

CBOR encoder and decoder for Golang

Important information:

  • This package still needs additional tests
  • Decoding into a user supplied type is not yet possible. The decoder will return an interface you will have to try to convert yourself. This feature is WIP and will be added shortly.
  • This is a go module, not a normal package. The master branch is the main development branch and may therefore receive breaking changes. Since all supported go versions now support modules this shoud not be an issue.

This go module implements a general purpose CBOR encoder and decoder as defined in RFC7049. There are a few notable departures from the recommendations made there:

  • The decoder will ignore unknown tags and won't forward them to the caller
  • The decoder will fail when receiving unknown simple values

Encoder

When encountering custom types the encoder will first try to call EncodeCBOR() []byte, then try MarshalCBOR() ([]byte, error) and panic when an error is returned. In case no specific functions for CBOR are present, the encoder will try MarshalBinary() ([]byte, error) with the same behavior on errors and encode it as a byte string. Then the normal type-based encoding will attempt to determine the type of the input and encode it accordingly. Should that not be possible for some reason, the encoder will try MarshalText() ([]byte, error) as a last resort and encode the output as a string. In case that function is not available either, the encoder will fall trough and panic.

The types supported by the type-based encoding are:

  • All standard signed and unsigned integers (BigNums are WIP)
  • All standard floating point value (BigFloats are WIP)
  • Strings, arrays and slices of supported types (will always be encoded with fixed length)
  • Maps of supported types (will always be encoded with fixed length)
  • Structs (only exported fields, cbor tag will be checked for name, will be encoded as indefinite length maps)
  • Booleans

Decoder

The decoder supports all types defined in the CBOR standard including 16-bit floats which will be decoded as 64-bit floats. All individual decoding functions are exported by this package and do return values with the most solid typing possible. Please not that these functions do not check the major type of the input.

License

This module is released under the Boost Software License 1.0 which can be found in LICENSE.md in the repository. This means that you can freely use the code according to the license and do not have to give attribution for binary distributions although this is always highly appreciated.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(in []byte) (interface{}, error)

Decode decodes a cbor value to an arbitrary type or data structure

func DecodeArray

func DecodeArray(fb byte, r *bytes.Reader) ([]interface{}, error)

DecodeArray decodes a cbor value to an array of arbitrary types

func DecodeBytes

func DecodeBytes(fb byte, r *bytes.Reader) ([]byte, error)

DecodeBytes decodes a cbor value to a byte slice

func DecodeFloat16

func DecodeFloat16(_ byte, r *bytes.Reader) (interface{}, error)

DecodeFloat16 decodes a cbor value to a 64-bit floating point value (the cbor value was 16-bit though)

func DecodeFloat32

func DecodeFloat32(_ byte, r *bytes.Reader) (interface{}, error)

DecodeFloat32 decodes a cbor value to a 32-bit floating point value

func DecodeFloat64

func DecodeFloat64(_ byte, r *bytes.Reader) (interface{}, error)

DecodeFloat64 decodes a cbor value to a 64-bit floating point value

func DecodeInt

func DecodeInt(fb byte, r *bytes.Reader) (int64, error)

DecodeInt decodes a cbor value to an int64

func DecodeMap

func DecodeMap(fb byte, r *bytes.Reader) (map[interface{}]interface{}, error)

DecodeMap decodes a cbor value to a map using arbitrary types as keys and values

func DecodeSimple

func DecodeSimple(fb byte, r *bytes.Reader) (interface{}, error)

DecodeSimple decodes the second byte of a simple cbor value - no values are defined in this range in RFC7049 so this will always cause an error. This behavior is not strictly conformant to RFC7049 but there is no easy way to return the raw value without it being ambigous.

func DecodeString

func DecodeString(fb byte, r *bytes.Reader) (string, error)

DecodeString decodes a cbor value to a string

func DecodeTag

func DecodeTag(fb byte, r *bytes.Reader) (interface{}, error)

DecodeTag decodes a tag and following data element, if possible using a registered tag decoder

func DecodeUint

func DecodeUint(fb byte, r *bytes.Reader) (uint64, error)

DecodeUint decodes a cbor value to an uint64

func Encode

func Encode(in interface{}) []byte

Encode encodes an arbitrary type or data structure as a cbor value

func EncodeArray

func EncodeArray(arr interface{}) []byte

EncodeArray encodes an array of arbitrary types to a cbor value

func EncodeBytes

func EncodeBytes(b []byte) []byte

EncodeBytes encodes a byte slice to a cbor value

func EncodeFloat

func EncodeFloat(in float64) []byte

EncodeFloat encodes a 64-bit floating point value to a cbor value

func EncodeInt

func EncodeInt(i int64) []byte

EncodeInt encodes an int64 as a cbor value

func EncodeMap

func EncodeMap(in interface{}) []byte

EncodeMap encodes a map using arbitrary types as keys and values to a cbor value

func EncodeString

func EncodeString(s string) []byte

EncodeString encodes a string to a cbor value

func EncodeStruct

func EncodeStruct(in interface{}) []byte

EncodeStruct encodes a struct (treated as a map) to a cbor value

func EncodeUint

func EncodeUint(i uint64) []byte

EncodeUint encodes an uint64 as a cbor value

Types

type Encoder

type Encoder interface {
	EncodeCBOR() []byte
}

Encoder is an interface for types that know how to turn themselfes into cbor

type Marshaler

type Marshaler interface {
	MarshalCBOR() ([]byte, error)
}

Marshaler is an interface (derived from encoding.*Marshaler) for types that know how to turn themselfes into cbor (encoding will panic when error is not nil)

Jump to

Keyboard shortcuts

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