scale

package module
v1.1.10 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2023 License: MIT Imports: 13 Imported by: 39

README

SCALE

Types

golang notes
[]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
uint32 compact u64
[...]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 DecodeCompact32

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

func DecodeCompact64

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

func DecodeCompact8

func DecodeCompact8(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 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 EncodeCompact32

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

func EncodeCompact64

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

func EncodeCompact8

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

func EncodeLen

func EncodeLen(e *Encoder, v uint32, 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 EncodeUint32

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

func EncodeUint64

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

func Generate

func Generate(pkg string, filepath string, objs ...interface{}) 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(*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(*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