encoding

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const EmptyObject = 0x80

EmptyObject is written when an object would otherwise be empty.

Variables

View Source
var ErrFieldsOutOfOrder = errors.New("fields are out of order")
View Source
var ErrInvalidFieldNumber = errors.New("field number is invalid")

ErrInvalidFieldNumber is returned when an invalid field number is encountered.

View Source
var ErrMalformedBigInt = errors.New("invalid big integer string")
View Source
var ErrNotEnoughData = errors.New("not enough data")
View Source
var ErrOverflow = errors.New("overflow")

Functions

func AnyFromJSON

func AnyFromJSON(v interface{}) (interface{}, error)

AnyFromJSON converts v to a duration if it appears to be a duration. AnyFromJSON never returns an error.

func AnyToJSON

func AnyToJSON(v interface{}) interface{}

func AsBytes

func AsBytes(v interface{}) []byte

func BigintBinarySize

func BigintBinarySize(v *big.Int) int

func BigintCopy added in v0.5.1

func BigintCopy(v *big.Int) *big.Int

func BigintFromJSON

func BigintFromJSON(s *string) (*big.Int, error)

func BigintMarshalBinary

func BigintMarshalBinary(v *big.Int) []byte

func BigintToJSON

func BigintToJSON(b *big.Int) *string

func BigintUnmarshalBinary

func BigintUnmarshalBinary(b []byte) (*big.Int, error)

func BoolBinarySize

func BoolBinarySize(_ bool) int

func BoolMarshalBinary

func BoolMarshalBinary(v bool) []byte

func BoolUnmarshalBinary

func BoolUnmarshalBinary(b []byte) (bool, error)

func BytesBinarySize

func BytesBinarySize(b []byte) int

func BytesCopy added in v0.5.1

func BytesCopy(v []byte) []byte

func BytesFromJSON

func BytesFromJSON(s *string) ([]byte, error)

func BytesMarshalBinary

func BytesMarshalBinary(b []byte) []byte

func BytesToJSON

func BytesToJSON(v []byte) *string

func BytesUnmarshalBinary

func BytesUnmarshalBinary(b []byte) ([]byte, error)

func ChainBinarySize

func ChainBinarySize(v *[32]byte) int

ToDo: Why a parameter? It does nothing...

func ChainFromJSON

func ChainFromJSON(s string) ([32]byte, error)

func ChainMarshalBinary

func ChainMarshalBinary(v *[32]byte) []byte

func ChainSetBinarySize

func ChainSetBinarySize(v [][32]byte) int

func ChainSetFromJSON

func ChainSetFromJSON(s []string) ([][32]byte, error)

func ChainSetMarshalBinary

func ChainSetMarshalBinary(v [][32]byte) []byte

func ChainSetToJSON

func ChainSetToJSON(v [][32]byte) []string

func ChainSetUnmarshalBinary

func ChainSetUnmarshalBinary(b []byte) ([][32]byte, error)

func ChainToJSON

func ChainToJSON(v [32]byte) string

func ChainUnmarshalBinary

func ChainUnmarshalBinary(b []byte) ([32]byte, error)

func DurationBinarySize

func DurationBinarySize(d time.Duration) int

func DurationFromJSON

func DurationFromJSON(v interface{}) (time.Duration, error)

func DurationMarshalBinary

func DurationMarshalBinary(d time.Duration) []byte

func DurationToJSON

func DurationToJSON(v time.Duration) interface{}

func DurationUnmarshalBinary

func DurationUnmarshalBinary(b []byte) (time.Duration, error)

func SetPtr added in v0.5.1

func SetPtr(value, target interface{}) (err error)

SetPtr sets *target = value

func SplitDuration

func SplitDuration(d time.Duration) (sec, ns uint64)

func StringBinarySize

func StringBinarySize(s string) int

func StringMarshalBinary

func StringMarshalBinary(s string) []byte

func StringUnmarshalBinary

func StringUnmarshalBinary(b []byte) (string, error)

func TimeBinarySize

func TimeBinarySize(v time.Time) int

func TimeMarshalBinary

func TimeMarshalBinary(v time.Time) []byte

func TimeUnmarshalBinary

func TimeUnmarshalBinary(b []byte) (time.Time, error)

func UnmarshalEnumType

func UnmarshalEnumType(r io.Reader, value EnumValueSetter) error

func UvarintBinarySize

func UvarintBinarySize(v uint64) int

func UvarintMarshalBinary

func UvarintMarshalBinary(v uint64) []byte

func UvarintUnmarshalBinary

func UvarintUnmarshalBinary(b []byte) (uint64, error)

func VarintBinarySize

func VarintBinarySize(v int64) int

func VarintMarshalBinary

func VarintMarshalBinary(v int64) []byte

func VarintUnmarshalBinary

func VarintUnmarshalBinary(b []byte) (int64, error)

Types

type BinaryValue

type BinaryValue interface {
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
	CopyAsInterface() interface{}
	UnmarshalBinaryFrom(io.Reader) error
}

type Byter

type Byter interface {
	Bytes() []byte
}

Byter is implemented by any value that has a Bytes method.

type DurationFields

type DurationFields struct {
	Seconds     uint64 `json:"seconds,omitempty"`
	Nanoseconds uint64 `json:"nanoseconds,omitempty"`
}

type EnumValueGetter added in v0.5.1

type EnumValueGetter interface {
	GetEnumValue() uint64
}

type EnumValueSetter added in v0.5.1

type EnumValueSetter interface {
	SetEnumValue(uint64) bool
}

type JsonList added in v0.6.0

type JsonList[T any] []T

JsonList attempts to unmarshal JSON into a slice of T and falls back to unmarshalling it into a single value of type T. Thus, fields of type JsonList can be unmarshaled from a single value or a list.

func (*JsonList[T]) UnmarshalJSON added in v0.6.0

func (j *JsonList[T]) UnmarshalJSON(data []byte) error

type JsonUnmarshalListWith added in v0.6.0

type JsonUnmarshalListWith[T any] struct {
	Value []T
	Func  func([]byte) (T, error)
}

JsonUnmarshalListWith combines the functionality of JsonList and JsonUnmarshalWith, using the given function to unmarshal JSON into a slice of T and falls back to unmarshalling it into a single value of type T.

func (*JsonUnmarshalListWith[T]) MarshalJSON added in v0.6.0

func (j *JsonUnmarshalListWith[T]) MarshalJSON() ([]byte, error)

func (*JsonUnmarshalListWith[T]) UnmarshalJSON added in v0.6.0

func (j *JsonUnmarshalListWith[T]) UnmarshalJSON(data []byte) error

type JsonUnmarshalWith added in v0.6.0

type JsonUnmarshalWith[T any] struct {
	Value T
	Func  func([]byte) (T, error)
}

JsonUnmarshalWith uses the given function to unmarshal JSON into a value of type T.

func (*JsonUnmarshalWith[T]) MarshalJSON added in v0.6.0

func (j *JsonUnmarshalWith[T]) MarshalJSON() ([]byte, error)

func (*JsonUnmarshalWith[T]) UnmarshalJSON added in v0.6.0

func (j *JsonUnmarshalWith[T]) UnmarshalJSON(data []byte) error

type Reader

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

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) ReadAll added in v0.6.0

func (r *Reader) ReadAll() ([]byte, error)

ReadAll reads the entire value from the current position

func (*Reader) ReadBigInt

func (r *Reader) ReadBigInt(n uint) (*big.Int, bool)

ReadBigInt reads the value as a big-endian byte slice.

func (*Reader) ReadBool

func (r *Reader) ReadBool(n uint) (bool, bool)

ReadBool reads the value as a varint-encoded unsigned integer. An error is recorded if the value is not 0 or 1.

func (*Reader) ReadBytes

func (r *Reader) ReadBytes(n uint) ([]byte, bool)

ReadBytes reads the length of the value as a varint-encoded unsigned integer followed by the value.

func (*Reader) ReadDuration

func (r *Reader) ReadDuration(n uint) (time.Duration, bool)

ReadDuration reads the value as seconds and nanoseconds, each as a varint-encoded unsigned integer.

func (*Reader) ReadEnum

func (r *Reader) ReadEnum(n uint, v EnumValueSetter) bool

func (*Reader) ReadFloat added in v0.6.0

func (r *Reader) ReadFloat(n uint) (float64, bool)

ReadFloat reads the value as a varint-encoded unsigned integer.

func (*Reader) ReadHash

func (r *Reader) ReadHash(n uint) (*[32]byte, bool)

ReadHash reads 32 bytes.

func (*Reader) ReadInt

func (r *Reader) ReadInt(n uint) (int64, bool)

ReadInt reads the value as a varint-encoded signed integer.

func (*Reader) ReadString

func (r *Reader) ReadString(n uint) (string, bool)

ReadString reads the length of the value as a varint-encoded unsigned integer followed by the value.

func (*Reader) ReadTime

func (r *Reader) ReadTime(n uint) (time.Time, bool)

ReadTime reads the value as a varint-encoded UTC Unix timestamp (signed).

func (*Reader) ReadUint

func (r *Reader) ReadUint(n uint) (uint64, bool)

ReadUint reads the value as a varint-encoded unsigned integer.

func (*Reader) ReadUrl

func (r *Reader) ReadUrl(n uint) (*url.URL, bool)

ReadUrl reads the value as a string.

func (*Reader) ReadValue

func (r *Reader) ReadValue(n uint, unmarshal func([]byte) error) bool

ReadValue reads the value as a byte slice and unmarshals it.

func (*Reader) Reset

func (r *Reader) Reset(fieldNames []string) (seen []bool, err error)

Reset returns a list of seen fields and an error, if one occurred, and resets the reader.

If a list of field names is provided, the error will be formatted as "field <name>: <err>".

type Writer

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

A Writer is used to binary-encode a struct and write it to a io.Writer.

If any attempt to write fails, all subsequent write methods are no-ops until Err is called.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer.

func (*Writer) Reset

func (w *Writer) Reset(fieldNames []string) (written int, lastField uint, err error)

Reset returns the total number of bytes written, the last field written, and an error, if one occurred, and resets the writer.

If a list of field names is provided, the error will be formatted as "field <name>: <err>".

func (*Writer) WriteBigInt

func (w *Writer) WriteBigInt(n uint, v *big.Int)

WriteBigInt writes the value as a big-endian byte slice.

func (*Writer) WriteBool

func (w *Writer) WriteBool(n uint, v bool)

WriteBool writes the value as a varint-encoded unsigned integer.

func (*Writer) WriteBytes

func (w *Writer) WriteBytes(n uint, v []byte)

WriteBytes writes the length of the value as a varint-encoded unsigned integer followed by the value.

func (*Writer) WriteDuration

func (w *Writer) WriteDuration(n uint, v time.Duration)

WriteDuration writes the value as seconds and nanoseconds, each as a varint-encoded unsigned integer.

func (*Writer) WriteEnum

func (w *Writer) WriteEnum(n uint, v EnumValueGetter)

WriteEnum writes the value as a varint-encoded unsigned integer.

func (*Writer) WriteFloat added in v0.6.0

func (w *Writer) WriteFloat(n uint, v float64)

WriteFloat writes the value as a IEEE 754 encoded floating point number.

func (*Writer) WriteHash

func (w *Writer) WriteHash(n uint, v *[32]byte)

WriteHash writes the value without modification.

func (*Writer) WriteInt

func (w *Writer) WriteInt(n uint, v int64)

WriteInt writes the value as a varint-encoded signed integer.

func (*Writer) WriteString

func (w *Writer) WriteString(n uint, v string)

WriteString writes the length of the value as a varint-encoded unsigned integer followed by the value.

func (*Writer) WriteTime

func (w *Writer) WriteTime(n uint, v time.Time)

WriteTime writes the value as a varint-encoded UTC Unix timestamp (signed).

func (*Writer) WriteUint

func (w *Writer) WriteUint(n uint, v uint64)

WriteUint writes the value as a varint-encoded unsigned integer.

func (*Writer) WriteUrl

func (w *Writer) WriteUrl(n uint, v *url.URL)

WriteUrl writes the value as a string.

func (*Writer) WriteValue

func (w *Writer) WriteValue(n uint, v encoding.BinaryMarshaler)

WriteValue marshals the value and writes it as a byte slice.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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