Documentation
¶
Overview ¶
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. Built-in codecs are package-level values such as JSON, YAML, and TOML.
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 ¶
Implement Codec (or Decoder alone when you only load) and pass the value to gopherly.dev/synthra.WithFileAs, gopherly.dev/synthra.WithFileFS, gopherly.dev/synthra.WithFileFSAs, gopherly.dev/synthra.WithContent, gopherly.dev/synthra/source.NewFile, or gopherly.dev/synthra/source.NewFileFS as appropriate.
Example with an explicit decoder and no file extension:
cfg, err := synthra.New(
synthra.WithFileAs("settings.dat", myCodec),
)
where myCodec implements Codec.
Scalar Decoders ¶
The package includes scalar decoders for parsing individual values:
decoder := codec.ParseInt("port")
var m map[string]any
decoder.Decode([]byte("8080"), &m) // m["port"] is int(8080)
Supported scalar parsers include ParseBool, ParseString, ParseInt variants, ParseUint variants, ParseFloat variants, ParseDuration, ParseTime, and ParseAs for custom types.
Index ¶
- type Codec
- type Decoder
- func ParseAs[T any](key string, fn func(string) (T, error)) Decoder
- func ParseBool(key string) Decoder
- func ParseDuration(key string) Decoder
- func ParseFloat32(key string) Decoder
- func ParseFloat64(key string) Decoder
- func ParseInt(key string) Decoder
- func ParseInt8(key string) Decoder
- func ParseInt16(key string) Decoder
- func ParseInt32(key string) Decoder
- func ParseInt64(key string) Decoder
- func ParseString(key string) Decoder
- func ParseTime(key string) Decoder
- func ParseUint(key string) Decoder
- func ParseUint8(key string) Decoder
- func ParseUint16(key string) Decoder
- func ParseUint32(key string) Decoder
- func ParseUint64(key string) Decoder
- type Encoder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
Codec is implemented by format codecs that support both encoding and decoding. Scalar decoders (ParseInt, ParseBool, ParseAs) only implement Decoder.
var JSON Codec = jsonCodec{}
JSON is a Codec that encodes and decodes JSON.
var TOML Codec = tomlCodec{}
TOML is a Codec that encodes and decodes TOML.
var YAML Codec = yamlCodec{}
YAML is a Codec that encodes and decodes YAML.
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.
var EnvVar Decoder = envVarCodec{}
EnvVar is a Decoder that decodes the environment variable format.
func ParseDuration ¶
ParseDuration decodes a time.Duration value and stores it under the key.
func ParseFloat32 ¶
ParseFloat32 decodes a float32 value and stores it under the key.
func ParseFloat64 ¶
ParseFloat64 decodes a float64 value and stores it under the key.
func ParseInt16 ¶
ParseInt16 decodes an int16 value and stores it under the key.
func ParseInt32 ¶
ParseInt32 decodes an int32 value and stores it under the key.
func ParseInt64 ¶
ParseInt64 decodes an int64 value and stores it under the key.
func ParseString ¶
ParseString decodes a string value and stores it under the key.
func ParseUint8 ¶
ParseUint8 decodes an uint8 value and stores it under the key.
func ParseUint16 ¶
ParseUint16 decodes an uint16 value and stores it under the key.
func ParseUint32 ¶
ParseUint32 decodes an uint32 value and stores it under the key.
func ParseUint64 ¶
ParseUint64 decodes an uint64 value and stores it under the key.