codec

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encoder
	Decoder
}

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 ParseAs

func ParseAs[T any](key string, fn func(string) (T, error)) Decoder

ParseAs decodes raw bytes using fn and stores the result under the key.

func ParseBool

func ParseBool(key string) Decoder

ParseBool decodes a bool value and stores it under the key.

func ParseDuration

func ParseDuration(key string) Decoder

ParseDuration decodes a time.Duration value and stores it under the key.

func ParseFloat32

func ParseFloat32(key string) Decoder

ParseFloat32 decodes a float32 value and stores it under the key.

func ParseFloat64

func ParseFloat64(key string) Decoder

ParseFloat64 decodes a float64 value and stores it under the key.

func ParseInt

func ParseInt(key string) Decoder

ParseInt decodes an int value and stores it under the key.

func ParseInt8

func ParseInt8(key string) Decoder

ParseInt8 decodes an int8 value and stores it under the key.

func ParseInt16

func ParseInt16(key string) Decoder

ParseInt16 decodes an int16 value and stores it under the key.

func ParseInt32

func ParseInt32(key string) Decoder

ParseInt32 decodes an int32 value and stores it under the key.

func ParseInt64

func ParseInt64(key string) Decoder

ParseInt64 decodes an int64 value and stores it under the key.

func ParseString

func ParseString(key string) Decoder

ParseString decodes a string value and stores it under the key.

func ParseTime

func ParseTime(key string) Decoder

ParseTime decodes a time.Time value (RFC3339 format) and stores it under the key.

func ParseUint

func ParseUint(key string) Decoder

ParseUint decodes an uint value and stores it under the key.

func ParseUint8

func ParseUint8(key string) Decoder

ParseUint8 decodes an uint8 value and stores it under the key.

func ParseUint16

func ParseUint16(key string) Decoder

ParseUint16 decodes an uint16 value and stores it under the key.

func ParseUint32

func ParseUint32(key string) Decoder

ParseUint32 decodes an uint32 value and stores it under the key.

func ParseUint64

func ParseUint64(key string) Decoder

ParseUint64 decodes an uint64 value and stores it under the key.

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.

Jump to

Keyboard shortcuts

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