scale

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 13 Imported by: 32

README

SCALE

Types

golang notes example
[]byte length prefixed byte array with length as u32 compact integer
string same as []byte
[...]byte appended to the result
bool 1 byte, 0 for false, 1 for true
Object{} concatenation of fields
*Object{} Option. 0 for nil, 1 for Object{}. if 1 - decode Object{}
uint8 compact u8 [TODO no need for compact u8]
uint16 compact u16
uint32 compact u32
uint34 compact u64
*uint8 Option (0 for nil, 1 otherwise), followed by compact u8 &64 -> 01FC; &255 -> 01FD03
*uint16 Option (0 for nil, 1 otherwise), followed by compact u16 &255 -> 01FD03
*uint32 Option (0 for nil, 1 otherwise), followed by compact u32 &255 -> 01FD03
*uint64 Option (0 for nil, 1 otherwise), followed by compact u64 &255 -> 01FD03
[]uint16 length prefixed (compact u32) followed by compact u16s [4, 15, 23, u16::MAX] -> 10103C5CFEFF0300
[]uint32 length prefixed (compact u32) followed by compact u32s [4, 15, 23, u32::MAX] -> 10103C5C03FFFFFFFF
[]uint64 length prefixed (compact u32) followed by compact u64s [4, 15, 23, u64::MAX] -> 10103C5C13FFFFFFFFFFFFFFFF
[...]Object array with objects. encoded by consecutively encoding every object
[]Object slice with objects. prefixed with compact u32

Not implemented:

  • pointers to arrays and slices
  • slices with pointers
  • enumerations
  • fixed width integers

Code generation

go install github.com/spacemeshos/go-scale/scalegen

//go:generate scalegen will discover all struct types and derive EncodeScale/DecodeScale methods. To avoid struct auto-discovery use -types=U8,U16.

Documentation

Index

Constants

View Source
const (
	// MaxElements is the maximum number of elements allowed in a collection if not set explicitly during
	// encoding/decoding.
	MaxElements uint32 = 1 << 20

	// MaxNested is the maximum nested level allowed if not set explicitly during encoding/decoding.
	MaxNested uint = 4
)

Variables

View Source
var (
	// ErrDecodeTooManyElements is returned when scale limit tag is used and collection has too many elements to decode.
	ErrDecodeTooManyElements = errors.New("too many elements to decode in collection with scale limit set")

	// ErrDecodeNestedTooDeep is returned when nested level is too deep.
	ErrDecodeNestedTooDeep = errors.New("nested level is too deep")
)
View Source
var (
	// ErrEncodeTooManyElements is returned when scale limit tag is used and collection has too many elements to encode.
	ErrEncodeTooManyElements = errors.New("too many elements to encode in collection with scale limit set")

	// ErrEncodeNestedTooDeep is returned when the depth of nested types exceeds the limit.
	ErrEncodeNestedTooDeep = errors.New("nested level is too deep")
)

Functions

func DecodeBool

func DecodeBool(d *Decoder) (bool, int, error)

func DecodeByte added in v1.1.3

func DecodeByte(d *Decoder) (byte, int, error)

func DecodeByteArray

func DecodeByteArray(d *Decoder, value []byte) (int, error)

func DecodeByteSlice

func DecodeByteSlice(d *Decoder) ([]byte, int, error)

func DecodeByteSliceWithLimit

func DecodeByteSliceWithLimit(d *Decoder, limit uint32) ([]byte, int, error)

func DecodeCompact16

func DecodeCompact16(d *Decoder) (uint16, int, error)

func DecodeCompact16Ptr added in v1.2.0

func DecodeCompact16Ptr(d *Decoder) (*uint16, int, error)

func DecodeCompact32

func DecodeCompact32(d *Decoder) (uint32, int, error)

func DecodeCompact32Ptr added in v1.2.0

func DecodeCompact32Ptr(d *Decoder) (*uint32, int, error)

func DecodeCompact64

func DecodeCompact64(d *Decoder) (uint64, int, error)

func DecodeCompact64Ptr added in v1.2.0

func DecodeCompact64Ptr(d *Decoder) (*uint64, int, error)

func DecodeCompact8

func DecodeCompact8(d *Decoder) (uint8, int, error)

func DecodeCompact8Ptr added in v1.2.0

func DecodeCompact8Ptr(d *Decoder) (*uint8, int, error)

func DecodeLen

func DecodeLen(d *Decoder, limit uint32) (uint32, int, error)

func DecodeOption

func DecodeOption[V any, H DecodablePtr[V]](d *Decoder) (*V, int, error)

func DecodeSliceOfByteSlice

func DecodeSliceOfByteSlice(d *Decoder) ([][]byte, int, error)

func DecodeSliceOfByteSliceWithLimit

func DecodeSliceOfByteSliceWithLimit(d *Decoder, limit uint32) ([][]byte, int, error)

func DecodeString

func DecodeString(d *Decoder) (string, int, error)

func DecodeStringSlice

func DecodeStringSlice(d *Decoder) ([]string, int, error)

func DecodeStringSliceWithLimit

func DecodeStringSliceWithLimit(d *Decoder, limit uint32) ([]string, int, error)

func DecodeStringWithLimit

func DecodeStringWithLimit(d *Decoder, limit uint32) (string, int, error)

func DecodeStruct

func DecodeStruct[V any, H DecodablePtr[V]](d *Decoder) (V, int, error)

func DecodeStructArray

func DecodeStructArray[V any, H DecodablePtr[V]](d *Decoder, value []V) (int, error)

func DecodeStructSlice

func DecodeStructSlice[V any, H DecodablePtr[V]](d *Decoder) ([]V, int, error)

func DecodeStructSliceWithLimit

func DecodeStructSliceWithLimit[V any, H DecodablePtr[V]](d *Decoder, limit uint32) ([]V, int, error)

func DecodeUint16Slice added in v1.2.0

func DecodeUint16Slice(d *Decoder) ([]uint16, int, error)

func DecodeUint16SliceWithLimit added in v1.2.0

func DecodeUint16SliceWithLimit(d *Decoder, limit uint32) ([]uint16, int, error)

func DecodeUint32Slice added in v1.2.0

func DecodeUint32Slice(d *Decoder) ([]uint32, int, error)

func DecodeUint32SliceWithLimit added in v1.2.0

func DecodeUint32SliceWithLimit(d *Decoder, limit uint32) ([]uint32, int, error)

func DecodeUint64Slice added in v1.2.0

func DecodeUint64Slice(d *Decoder) ([]uint64, int, error)

func DecodeUint64SliceWithLimit added in v1.2.0

func DecodeUint64SliceWithLimit(d *Decoder, limit uint32) ([]uint64, int, error)

func EncodeBool

func EncodeBool(e *Encoder, value bool) (int, error)

func EncodeByte

func EncodeByte(e *Encoder, value byte) (int, error)

func EncodeByteArray

func EncodeByteArray(e *Encoder, value []byte) (int, error)

func EncodeByteSlice

func EncodeByteSlice(e *Encoder, value []byte) (int, error)

func EncodeByteSliceWithLimit

func EncodeByteSliceWithLimit(e *Encoder, value []byte, limit uint32) (int, error)

func EncodeCompact16

func EncodeCompact16(e *Encoder, v uint16) (int, error)

func EncodeCompact16Ptr added in v1.2.0

func EncodeCompact16Ptr(e *Encoder, value *uint16) (int, error)

func EncodeCompact32

func EncodeCompact32(e *Encoder, v uint32) (int, error)

func EncodeCompact32Ptr added in v1.2.0

func EncodeCompact32Ptr(e *Encoder, value *uint32) (int, error)

func EncodeCompact64

func EncodeCompact64(e *Encoder, v uint64) (int, error)

func EncodeCompact64Ptr added in v1.2.0

func EncodeCompact64Ptr(e *Encoder, value *uint64) (int, error)

func EncodeCompact8

func EncodeCompact8(e *Encoder, v uint8) (int, error)

func EncodeCompact8Ptr added in v1.2.0

func EncodeCompact8Ptr(e *Encoder, value *uint8) (int, error)

func EncodeLen

func EncodeLen(e *Encoder, v, limit uint32) (int, error)

func EncodeOption

func EncodeOption[V any, H EncodablePtr[V]](e *Encoder, value *V) (int, error)

func EncodeString

func EncodeString(e *Encoder, value string) (int, error)

func EncodeStringSlice

func EncodeStringSlice(e *Encoder, value []string) (int, error)

func EncodeStringSliceWithLimit

func EncodeStringSliceWithLimit(e *Encoder, value []string, limit uint32) (int, error)

func EncodeStringWithLimit

func EncodeStringWithLimit(e *Encoder, value string, limit uint32) (int, error)

func EncodeStruct

func EncodeStruct[V any, H EncodablePtr[V]](e *Encoder, value V) (int, error)

func EncodeStructArray

func EncodeStructArray[V any, H EncodablePtr[V]](e *Encoder, value []V) (int, error)

func EncodeStructSlice

func EncodeStructSlice[V any, H EncodablePtr[V]](e *Encoder, value []V) (int, error)

func EncodeStructSliceWithLimit

func EncodeStructSliceWithLimit[V any, H EncodablePtr[V]](e *Encoder, value []V, limit uint32) (int, error)

func EncodeUint16

func EncodeUint16(e *Encoder, value uint16) (int, error)

func EncodeUint16Slice added in v1.2.0

func EncodeUint16Slice(e *Encoder, value []uint16) (int, error)

func EncodeUint16SliceWithLimit added in v1.2.0

func EncodeUint16SliceWithLimit(e *Encoder, value []uint16, limit uint32) (int, error)

func EncodeUint32

func EncodeUint32(e *Encoder, value uint32) (int, error)

func EncodeUint32Slice added in v1.2.0

func EncodeUint32Slice(e *Encoder, value []uint32) (int, error)

func EncodeUint32SliceWithLimit added in v1.2.0

func EncodeUint32SliceWithLimit(e *Encoder, value []uint32, limit uint32) (int, error)

func EncodeUint64

func EncodeUint64(e *Encoder, value uint64) (int, error)

func EncodeUint64Slice added in v1.2.0

func EncodeUint64Slice(e *Encoder, value []uint64) (int, error)

func EncodeUint64SliceWithLimit added in v1.2.0

func EncodeUint64SliceWithLimit(e *Encoder, value []uint64, limit uint32) (int, error)

func Generate

func Generate(pkg, filepath string, objs ...any) error

func LenSize added in v1.1.13

func LenSize(v uint32) uint32

LenSize returns the size in bytes required to encode a length value.

func MaxScaleElements added in v1.1.14

func MaxScaleElements[T any](fieldName string) (uint32, error)

MaxScaleElements is a generic version of GetMaxElementsFromValue that uses the specified type instead of a struct value.

func MaxScaleElementsForField added in v1.1.14

func MaxScaleElementsForField(v any, fieldName string) (uint32, error)

MaxScaleElementsForField returns the max number of elements for the specified field in the struct passed as the v argument based on the 'scale' tag. It returns an error if v is not a structure, if max is not specified for the field, the field doesn't exist or there's a problem parsing the tag.

func MustGetMaxElements added in v1.1.14

func MustGetMaxElements[T any](fieldName string) uint32

MustGetMaxElements is the same as GetMaxElements, but returns just the max tag value and panics in case of an error.

func WithDecodeMaxElements added in v1.1.10

func WithDecodeMaxElements(elements uint32) decoderOpts

WithDecodeMaxElements sets the maximum number of elements allowed in a collection. The default value is 1 << 20.

func WithDecodeMaxNested added in v1.1.10

func WithDecodeMaxNested(nested uint) decoderOpts

WithDecodeMaxNested sets the nested level of the decoder. A value of 0 means no nesting is allowed. The default value is 4.

func WithEncodeMaxElements added in v1.1.10

func WithEncodeMaxElements(elements uint32) encoderOpts

WithEncodeMaxElements sets the maximum number of elements allowed in a collection. The default value is 1 << 20.

func WithEncodeMaxNested added in v1.1.10

func WithEncodeMaxNested(nested uint) encoderOpts

WithEncodeMaxNested sets the nested level of the encoder. A value of 0 means no nesting is allowed. The default value is 4.

Types

type Decodable

type Decodable interface {
	DecodeScale(dec *Decoder) (int, error)
}

type DecodablePtr

type DecodablePtr[B any] interface {
	Decodable
	*B
}

type Decoder

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

func NewDecoder

func NewDecoder(r io.Reader, opts ...decoderOpts) *Decoder

NewDecoder returns a new decoder that reads from r.

type Encodable

type Encodable interface {
	EncodeScale(enc *Encoder) (int, error)
}

type EncodablePtr

type EncodablePtr[B any] interface {
	Encodable
	*B
}

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer, opts ...encoderOpts) *Encoder

NewEncoder returns a new encoder that writes to w. If w implements io.StringWriter, the returned encoder will be more efficient in encoding strings.

type Type

type Type interface {
	Encodable
	Decodable
}

type TypePtr

type TypePtr[T any] interface {
	Type
	*T
}

type U8

type U8 uint8

func (*U8) DecodeScale

func (u *U8) DecodeScale(d *Decoder) (int, error)

func (*U8) EncodeScale

func (u *U8) EncodeScale(e *Encoder) (int, error)

Directories

Path Synopsis
nolint
nolint
nested
nolint
nolint
nolint
nolint

Jump to

Keyboard shortcuts

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