encoding

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: MIT Imports: 12 Imported by: 9

Documentation

Index

Constants

View Source
const EmptyObject = 0x80

EmptyObject is written when an object would otherwise be empty.

View Source
const MaxValueSize = 1 << 24

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 BigintCopy

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

func BigintFromJSON

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

func BigintToJSON

func BigintToJSON(b *big.Int) *string

func BytesCopy

func BytesCopy(v []byte) []byte

func BytesFromJSON

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

func BytesToJSON

func BytesToJSON(v []byte) *string

func ChainFromJSON

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

func ChainToJSON

func ChainToJSON(v [32]byte) string

func DurationFromJSON

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

func DurationToJSON

func DurationToJSON(v time.Duration) interface{}

func SplitDuration

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

Types

type BinaryValue

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

type DurationFields

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

type EnumValueGetter

type EnumValueGetter interface {
	GetEnumValue() uint64
}

type EnumValueSetter

type EnumValueSetter interface {
	SetEnumValue(uint64) bool
}

type Error

type Error struct {
	E error
}

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

type JsonList

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

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

type JsonUnmarshalListWith

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

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

func (*JsonUnmarshalListWith[T]) UnmarshalJSON

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

type JsonUnmarshalWith

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

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

func (*JsonUnmarshalWith[T]) UnmarshalJSON

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

type Reader

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

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) ReadAll

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

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) ReadTxid

func (r *Reader) ReadTxid(n uint) (*url.TxID, bool)

ReadTxid reads the value as a transaction ID.

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 URL.

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 {
	IgnoreSizeLimit bool
	// 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

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) WriteTxid

func (w *Writer) WriteTxid(n uint, v *url.TxID)

WriteTxid writes the value as a string.

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, marshal func() ([]byte, error))

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

func (*Writer) WriteValueOld

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

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

Jump to

Keyboard shortcuts

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