codec

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeBool

func DecodeBool(v any, b bool) error

DecodeBool decodes a boolean value into v. If *v is a pointer to a bool, then a bool will be allocated. If v implements BoolDecoder, then DecodeBool(b) is called.

func DecodeBytes

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

DecodeBytes decodes data into v. The following types are supported: []byte, BytesDecoder, encoding.BinaryUnmarshaler, and encoding.TextUnmarshaler.

func DecodeMap

func DecodeMap[K ~string, V any](dec Decoder, m *map[K]V) error

DecodeMap adapts a string-keyed map m into a FieldDecoder and decodes it.

func DecodeNil

func DecodeNil(v any) error

DecodeNil calls DecodeNil on v if v implements NilDecoder.

func DecodeNumber

func DecodeNumber(v any, n string) error

DecodeNumber decodes a number encoded as a string into v. The following core types are supported: int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, and float64. Pointers to the above types are also supported, and will be allocated if necessary. The interface types IntDecoder, and FloatDecoder are also supported. If unable to decode into a numeric type, it will fall back to DecodeString.

func DecodeSlice

func DecodeSlice[T comparable](dec Decoder, s *[]T) error

DecodeSlice adapts slice s into an ElementDecoder and decodes it.

func DecodeString

func DecodeString(v any, s string) error

DecodeString decodes s into v. The following types are supported: string, *string, and StringDecoder. It will fall back to DecodeBytes to attempt to decode into a byte slice or binary decoder.

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns a slice of keys for map m.

func Must

func Must[T any](p **T) *T

Must ensures that the pointer at *p is non-nil. If *p is nil, a new value of type T will be allocated.

func Resize

func Resize[S ~[]E, E any](s *S, i int) E

Resize resizes the slice s to at least len(s) == i+1, returning the value at s[i].

func SortedKeys

func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K

SortedKeys returns a slice of keys for map m. Map keys must conform to cmp.Ordered.

Types

type BoolDecoder

type BoolDecoder interface {
	DecodeBool(bool) error
}

BoolDecoder is the interface implemented by types that can decode from a bool.

type BytesDecoder

type BytesDecoder interface {
	DecodeBytes([]byte) error
}

BytesDecoder is the interface implemented by types that can decode from a byte slice. It is similar to encoding.BinaryUnmarshaler and encoding.TextUnmarshaler.

type Codec

type Codec any

Codec is any type that can encode or decode itself or an associated type.

type Decoder

type Decoder interface {
	Decode(v any) error
}

Decoder is the interface implemented by types that can decode data into Go type(s).

type ElementDecoder

type ElementDecoder interface {
	DecodeElement(dec Decoder, i int) error
}

ElementDecoder is the interface implemented by types that can decode 0-indexed elements, such as a slice or an array.

func Slice

func Slice[E comparable](s *[]E) ElementDecoder

Slice returns an ElementDecoder for slice s.

type FieldDecoder

type FieldDecoder interface {
	DecodeField(dec Decoder, name string) error
}

FieldDecoder is the interface implemented by types that can decode fields, such as structs or maps.

func Map

func Map[K ~string, V any](m *map[K]V) FieldDecoder

Map returns an FieldDecoder for map m.

type Float

type Float interface {
	float32 | float64
}

Float is the set of floating-point types supported by this package.

type FloatDecoder

type FloatDecoder[T Float] interface {
	DecodeFloat(T) error
}

FloatDecoder is the interface implemented by types that can decode from a floating-point value. See Float for the list of supported types.

type IntDecoder

type IntDecoder[T Integer] interface {
	DecodeInt(T) error
}

IntDecoder is the interface implemented by types that can decode from an integer value. See Integer for the list of supported types.

type Integer

type Integer interface {
	Signed | Unsigned
}

Integer is the set of integer types supported by this package.

type NilDecoder

type NilDecoder interface {
	DecodeNil() error
}

NilDecoder is the interface implemented by types that can decode from nil.

type Resolver

type Resolver interface {
	ResolveCodec(v any) Codec
}

Resolver is the interface implemented by types that return a codec for the value at v. Values returned by Resolver should implement one or more encode or decode methods.

type Resolvers

type Resolvers []Resolver

Resolvers is a slice of Resolver values. It also implements the Resolver interface.

func (Resolvers) ResolveCodec

func (rs Resolvers) ResolveCodec(v any) Codec

ResolveCodec walks the slice of Resolvers, returning the first non-nil value or an error.

type Signed

type Signed interface {
	int | int8 | int16 | int32 | int64
}

Signed is the set of signed integer types supported by this package.

type StringDecoder

type StringDecoder interface {
	DecodeString(string) error
}

StringDecoder is the interface implemented by types that can decode from a string.

type Unsigned

type Unsigned interface {
	uint | uint8 | uint16 | uint32 | uint64
}

Unsigned is the set of unsigned integer types supported by this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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