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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
type JSONCodec ¶
type JSONCodec struct{}
The JSONCodec struct implements the Encode and Decode methods to provide JSON serialization and deserialization functionality.
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.
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.