codec

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder[T any] func(value []byte) (T, error)

Decoder is an adapter allowing to use a function as a decoder.

var ByteDecoder Decoder[[]byte] = func(b []byte) ([]byte, error) {
	return b, nil
}

ByteDecoder represents a byte decoder.

var Int64Decoder Decoder[int64] = func(b []byte) (int64, error) {
	return strconv.ParseInt(string(b), 10, 64)
}

Int64Decoder represents an int64 decoder.

var NilDecoder Decoder[any] = func([]byte) (any, error) { return nil, nil }

NilDecoder is a decoder that always returns a nil, no matter the input.

var StringDecoder Decoder[string] = func(b []byte) (string, error) {
	return string(b), nil
}

StringDecoder ...

func (Decoder[T]) Decode

func (f Decoder[T]) Decode(value []byte) (T, error)

Decode transforms byte data to the desired type.

type Encoder

type Encoder[T any] func(T) ([]byte, error)

Encoder is an adapter allowing to use a function as an encoder.

var ByteEncoder Encoder[[]byte] = func(v []byte) ([]byte, error) {
	return v, nil
}

ByteEncoder represents a byte encoder.

var Int64Encoder Encoder[int64] = func(v int64) ([]byte, error) {
	return []byte(strconv.FormatInt(v, 10)), nil
}

Int64Encoder represents an int64 encoder.

var StringEncoder Encoder[string] = func(v string) ([]byte, error) {
	return []byte(v), nil
}

StringEncoder ...

func (Encoder[T]) Encode

func (f Encoder[T]) Encode(value T) ([]byte, error)

Encode transforms the typed data to bytes.

Jump to

Keyboard shortcuts

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