codec

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package codec provides functionality for encoding and decoding data.

Package codec provides encoding and decoding functionality for configuration data.

The codec package defines Encoder and Decoder interfaces for converting configuration data between different formats (JSON, YAML, TOML, etc.) and Go types. It includes a registry system for registering and retrieving codec implementations.

Built-in Codecs

The package includes built-in support for common formats:

  • JSON: Standard JSON encoding/decoding
  • YAML: YAML encoding/decoding
  • TOML: TOML encoding/decoding
  • EnvVar: Environment variable format

Custom Codecs

Register custom codecs using RegisterEncoder and RegisterDecoder:

type MyCodec struct{}

func (c MyCodec) Encode(v any) ([]byte, error) {
    // Custom encoding logic
    return data, nil
}

func (c MyCodec) Decode(data []byte, v any) error {
    // Custom decoding logic
    return nil
}

codec.RegisterEncoder(codec.Type("myformat"), MyCodec{})
codec.RegisterDecoder(codec.Type("myformat"), MyCodec{})

Type Casting

The package includes caster codecs for automatic type conversion:

decoder, _ := codec.GetDecoder(codec.TypeCasterInt)
var value any
decoder.Decode([]byte("42"), &value)  // value is int(42)

Supported caster types include bool, string, int variants, uint variants, float variants, time.Time, and time.Duration.

Package codec provides functionality for encoding and decoding data.

Package codec provides functionality for encoding and decoding data.

Package codec provides functionality for encoding and decoding data.

Package codec provides functionality for encoding and decoding data.

Package codec provides functionality for encoding and decoding data.

Index

Constants

View Source
const (
	CastTypeBool       CastType = "bool"
	TypeCasterBool     Type     = "caster-bool"
	CastTypeTime       CastType = "time"
	TypeCasterTime     Type     = "caster-time"
	CastTypeDuration   CastType = "duration"
	TypeCasterDuration Type     = "caster-duration"
	CastTypeFloat64    CastType = "float64"
	TypeCasterFloat64  Type     = "caster-float64"
	CastTypeFloat32    CastType = "float32"
	TypeCasterFloat32  Type     = "caster-float32"
	CastTypeInt64      CastType = "int64"
	TypeCasterInt64    Type     = "caster-int64"
	CastTypeInt32      CastType = "int32"
	TypeCasterInt32    Type     = "caster-int32"
	CastTypeInt16      CastType = "int16"
	TypeCasterInt16    Type     = "caster-int16"
	CastTypeInt8       CastType = "int8"
	TypeCasterInt8     Type     = "caster-int8"
	CastTypeInt        CastType = "int"
	TypeCasterInt      Type     = "caster-int"
	CastTypeUint       CastType = "uint"
	TypeCasterUint     Type     = "caster-uint"
	CastTypeUint64     CastType = "uint64"
	TypeCasterUint64   Type     = "caster-uint64"
	CastTypeUint32     CastType = "uint32"
	TypeCasterUint32   Type     = "caster-uint32"
	CastTypeUint16     CastType = "uint16"
	TypeCasterUint16   Type     = "caster-uint16"
	CastTypeUint8      CastType = "uint8"
	TypeCasterUint8    Type     = "caster-uint8"
	CastTypeString     CastType = "string"
	TypeCasterString   Type     = "caster-string"
)

revive:disable:exported

Variables

This section is empty.

Functions

func RegisterDecoder

func RegisterDecoder(name Type, decoder Decoder)

RegisterDecoder registers a decoder for the given type. The decoder can be used to decode values of the given type using the GetDecoder function.

func RegisterEncoder

func RegisterEncoder(name Type, encoder Encoder)

RegisterEncoder registers an encoder for the given type. The encoder can be used to encode values of the given type using the GetEncoder function.

Types

type CastType

type CastType string

CastType is a string type that represents the type of a value that can be cast.

type CasterCodec

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

CasterCodec is a codec that casts the input data to a specific type. The castType field determines the type to which the data will be cast.

func NewCaster

func NewCaster(castType CastType) *CasterCodec

NewCaster creates a new CasterCodec instance with the specified castType. The CasterCodec is used to cast input data to a specific type during decoding.

func (*CasterCodec) Decode

func (c *CasterCodec) Decode(data []byte, v any) error

Decode implements the Decoder interface for the CasterCodec. It takes the input data and casts it to the type specified by the castType field of the CasterCodec. The result is stored in the value pointed to by the v parameter.

type Decoder

type Decoder interface {
	// Decode converts the encoded data into the value pointed to by v.
	// It returns an error if decoding fails or if v is not a valid target.
	Decode(data []byte, v any) error
}

Decoder converts encoded byte representations into Go values. Implementations must be safe for concurrent use.

func GetDecoder

func GetDecoder(name Type) (Decoder, error)

GetDecoder retrieves the registered decoder for the given type. If no decoder is registered for the given type, an error is returned.

type Encoder

type Encoder interface {
	// Encode converts the value v into an encoded byte slice.
	// It returns an error if encoding fails.
	Encode(v any) ([]byte, error)
}

Encoder converts Go values into encoded byte representations. Implementations must be safe for concurrent use.

func GetEncoder

func GetEncoder(name Type) (Encoder, error)

GetEncoder retrieves the registered encoder for the given type. If no encoder is registered for the given type, an error is returned.

type EnvVarCodec

type EnvVarCodec struct{}

EnvVarCodec is a struct that implements the Codec interface for decoding environment variables.

func (EnvVarCodec) Decode

func (EnvVarCodec) Decode(data []byte, v any) error

Decode decodes the provided data bytes into a configuration map. The data is expected to be in the format of environment variables, with each line containing a key-value pair separated by an equals sign.

func (EnvVarCodec) Encode

func (EnvVarCodec) Encode(_ any) ([]byte, error)

Encode encodes the provided value to environment variable format. This method is provided for interface compatibility but environment variables are typically read-only.

type JSONCodec

type JSONCodec struct{}

The JSONCodec struct implements the Encode and Decode methods to provide JSON serialization and deserialization functionality.

func (JSONCodec) Decode

func (JSONCodec) Decode(data []byte, v any) error

Decode unmarshalls the provided JSON-encoded byte slice into the value pointed to by v. It wraps the standard library's json.Unmarshal function.

func (JSONCodec) Encode

func (JSONCodec) Encode(v any) ([]byte, error)

Encode converts the provided value v into a JSON-encoded byte slice. It wraps the standard library's json.Marshal function.

type Registry

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

Registry is a struct that holds the registered encoders and decoders. It provides methods to register and retrieve encoders and decoders.

type TOMLCodec

type TOMLCodec struct{}

TOMLCodec is a struct that implements the Codec interface for TOML encoding and decoding.

func (TOMLCodec) Decode

func (TOMLCodec) Decode(data []byte, v any) error

Decode decodes the TOML-encoded data into the value pointed to by v.

func (TOMLCodec) Encode

func (TOMLCodec) Encode(v any) ([]byte, error)

Encode encodes the given value 'v' to a TOML-encoded byte slice.

type Type

type Type string

Type represents a codec type identifier.

const TypeEnvVar Type = "env_var"

TypeEnvVar is a constant representing the type of an environment variable codec.

const TypeJSON Type = "json"

TypeJSON is a constant representing the "json" encoding type.

const TypeTOML Type = "toml"

TypeTOML is a constant representing the "toml" encoding type.

const TypeYAML Type = "yaml"

TypeYAML is a constant representing the "yaml" encoding type.

type YAMLCodec

type YAMLCodec struct{}

YAMLCodec is a struct that implements the Codec interface for YAML encoding and decoding.

func (YAMLCodec) Decode

func (YAMLCodec) Decode(data []byte, v any) error

Decode decodes the YAML-encoded data into the value pointed to by v.

func (YAMLCodec) Encode

func (YAMLCodec) Encode(v any) ([]byte, error)

Encode encodes the given value 'v' to a YAML-encoded byte slice.

Jump to

Keyboard shortcuts

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