cbor

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2023 License: MIT Imports: 21 Imported by: 0

README

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedEnd = errors.New("cbor: unexpected end")

ErrUnexpectedEnd is returned when the CBOR data ends abruptly.

View Source
var Undefined undefined = nil

Functions

func Marshal

func Marshal(v any) ([]byte, error)

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal parses the CBOR-encoded data and stores the result in the value pointed to by v.

func WellFormed

func WellFormed(data []byte) bool

WellFormed reports whether data is a valid CBOR encoding.

Types

type Base64String

type Base64String string

Base64String is a base64 with padding encoded data. CBOR tags that has tag number 34 is converted to this type. The decoder and the encoder validate that it is a valid base64-encoded string. See RFC 8949 Section 3.4.5.3.

type Base64URLString

type Base64URLString string

Base64URLString is a base64url with no padding encoded string. CBOR tags that has tag number 33 is converted to this type. The decoder and the encoder validate that it is a valid base64url-encoded string. See RFC 8949 Section 3.4.5.3.

type CBORMarshaler

type CBORMarshaler interface {
	// MarshalCBOR returns the CBOR encoding of the receiver.
	MarshalCBOR() ([]byte, error)
}

type Decoder

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

A Decoder reads and decodes CBOR values from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(v any) error

Decode reads the next CBOR-encoded value from its input and stores it in the value pointed to by v.

func (*Decoder) UseAnyKey

func (dec *Decoder) UseAnyKey()

UseAnyKey allows decoding maps to map[any]any instead of map[string]any.

func (*Decoder) UseInteger

func (dec *Decoder) UseInteger()

UseInteger allows decoding integers to Integer instead of int64.

type EncodedData

type EncodedData []byte

EncodedData is a CBOR encoded data. CBOR tags that has tag number 24 is converted to this type. See RFC 8949 Section 3.4.5.1.

type Encoder

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

An Encoder writes CBOR to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (enc *Encoder) Encode(v any) error

Encode writes the CBOR encoding of v to the stream.

type ExpectedBase16

type ExpectedBase16 struct {
	Content any
}

ExpectedBase16 is data expected to be encoded as base16-encoding (as known as hex-encoding). CBOR tags that has tag number 23 is converted to this type. When encoded to JSON, []byte in Content is converted to a base16-encoded string. See RFC 8949 Section 3.4.5.2.

Example
data, _ := json.Marshal(cbor.ExpectedBase16{
	Content: []byte("🍣"),
})
fmt.Println(string(data))
Output:

"f09f8da3"

func (ExpectedBase16) MarshalJSON

func (e ExpectedBase16) MarshalJSON() ([]byte, error)

type ExpectedBase64

type ExpectedBase64 struct {
	Content any
}

ExpectedBase64 is data expected to be encoded as base64-encoding. CBOR tags that has tag number 22 is converted to this type. When encoded to JSON, []byte in Content is converted to a base64-encoded string. See RFC 8949 Section 3.4.5.2.

Example
data, _ := json.Marshal(cbor.ExpectedBase64{
	Content: []byte("🍣"),
})
fmt.Println(string(data))
Output:

"8J+Now=="

func (ExpectedBase64) MarshalJSON

func (e ExpectedBase64) MarshalJSON() ([]byte, error)

type ExpectedBase64URL

type ExpectedBase64URL struct {
	Content any
}

ExpectedBase64URL is data expected to be encoded as base64url-encoding. CBOR tags that has tag number 21 is converted to this type. When encoded to JSON, []byte in Content is converted to a base64url-encoded string. See RFC 8949 Section 3.4.5.2.

Example
data, _ := json.Marshal(cbor.ExpectedBase64URL{
	Content: []byte("🍣"),
})
fmt.Println(string(data))
Output:

"8J-Now"

func (ExpectedBase64URL) MarshalJSON

func (e ExpectedBase64URL) MarshalJSON() ([]byte, error)

type Integer

type Integer struct {
	// Sign is true if the integer is negative.
	Sign bool

	// Value presents the value of the integer.
	// If the integer is positive, the value is Value itself.
	// If the integer is negative, the value is -Value-1.
	Value uint64
}

Integer is a CBOR integer type.

func (Integer) BigInt

func (i Integer) BigInt() *big.Int

BigInt returns the integer as *big.Int.

func (Integer) Int64

func (i Integer) Int64() (int64, error)

Int64 returns the integer as int64.

func (Integer) MarshalJSON

func (i Integer) MarshalJSON() ([]byte, error)

func (Integer) String

func (i Integer) String() string

func (Integer) Uint64

func (i Integer) Uint64() (uint64, error)

Uint64 returns the integer as uint64.

func (*Integer) UnmarshalJSON

func (i *Integer) UnmarshalJSON(b []byte) error

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)

func (*InvalidUnmarshalError) Error

func (e *InvalidUnmarshalError) Error() string

type Options

type Options struct {
	// UseInteger will decode CBOR integers as Integer instead of Go int64.
	UseInteger bool

	// UseAnyKey will decode CBOR map keys as Go map[any]any instead of map[string]any.
	UseAnyKey bool
}

func (Options) Unmarshal

func (o Options) Unmarshal(data []byte, v any) error

type RawMessage

type RawMessage []byte

RawMessage is a raw encoded CBOR value. It implements Marshaler and Unmarshaler and can be used to delay CBOR decoding or precompute a CBOR encoding. nil RawMessage encodes as the CBOR undefined value.

func DecodeEDN added in v0.2.0

func DecodeEDN(data []byte) (RawMessage, error)

DecodeEDN parses the Extended Diagnostic Notation encoded data and returns the result.

func (RawMessage) EncodeEDN added in v0.2.0

func (m RawMessage) EncodeEDN() ([]byte, error)

EncodeEDN returns the Extended Diagnostic Notation encoding of msg.

func (RawMessage) MarshalCBOR

func (m RawMessage) MarshalCBOR() ([]byte, error)

MarshalCBOR returns m as the CBOR encoding of m.

func (*RawMessage) UnmarshalCBOR

func (m *RawMessage) UnmarshalCBOR(data []byte) error

UnmarshalCBOR sets *m to a copy of data.

type RawTag

type RawTag struct {
	Number  TagNumber
	Content RawMessage
}

func (RawTag) Decode

func (tag RawTag) Decode(v any, opts Options) error

Decode decodes the tag content.

type SemanticError

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

SemanticError is a description of a CBOR semantic error.

func (*SemanticError) Error

func (e *SemanticError) Error() string

func (*SemanticError) Unwrap

func (e *SemanticError) Unwrap() error

type Simple

type Simple byte

Simple is a CBOR simple type.

type SyntaxError

type SyntaxError struct {
	Offset int64 // error occurred after reading Offset bytes
	// contains filtered or unexported fields
}

A SyntaxError is a description of a CBOR syntax error. Unmarshal will return a SyntaxError if the CBOR can't be parsed.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type Tag

type Tag struct {
	Number  TagNumber
	Content any
}

Tag is a CBOR tag.

func (Tag) Decode

func (tag Tag) Decode(v any, opts Options) error

Decode decodes the tag content. The following tags are supported:

  • tag number 0: date/time string is decoded as time.Time.
  • tag number 1: epoch-based date/time is decoded as time.Time.
  • tag number 2: positive bignum is decoded as *big.Int.
  • tag number 3: negative bignum is decoded as *big.Int.
  • tag number 4: decimal fraction is not implemented.
  • tag number 5: bigfloat is decoded as *big.Float.
  • tag number 21: expected conversion to base64url is decoded as ExpectedBase64URL.
  • tag number 22: expected conversion to base64 is decoded as ExpectedBase64.
  • tag number 23: expected conversion to base16 is decoded as ExpectedBase16.
  • tag number 24: encoded CBOR data item is decoded as EncodedData.
  • tag number 32: URI is decoded as *url.URL.
  • tag number 33: base64url is decoded as Base64URLString.
  • tag number 34: base64 is decoded as Base64String.
  • tag number 55799: Self-Described CBOR return the content as is.

Other tags returns tag itself.

type TagNumber

type TagNumber uint64

TagNumber is a CBOR tag number type.

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value  string       // description of CBOR value - "bool", "array", "number -5"
	Type   reflect.Type // type of Go value it could not be assigned to
	Offset int64        // error occurred after reading Offset bytes
	Struct string       // name of the struct type containing the field
	Field  string       // the full path from root node to the field
}

An UnmarshalTypeError describes a CBOR value that was not appropriate for a value of a specific Go type.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalCBOR([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a CBOR description of themselves. The input can be assumed to be a valid encoding of a CBOR value. UnmarshalCBOR must copy the CBOR data if it wishes to retain the data after returning.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

type UnsupportedValueError

type UnsupportedValueError struct {
	Value reflect.Value
	Str   string
}

An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.

func (*UnsupportedValueError) Error

func (e *UnsupportedValueError) Error() string

Jump to

Keyboard shortcuts

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