Documentation
¶
Overview ¶
Package dsco (pronounce /ˈdɪskoʊ/) provides a layered configuration system supporting command line arguments, environment variables, YAML files, and struct-based configurations with strict validation.
Overview ¶
dsco implements a layered configuration system where different configuration sources are organized into layers with configurable precedence. Later layers override earlier ones, with optional strict mode for conflict detection.
The library is designed for microservices environments where configuration safety is critical. It enforces explicit configuration through pointer-based fields, ensuring no silent defaults or ambiguous states.
Quick Start ¶
type Config struct {
Host *string `yaml:"host"`
Port *int `yaml:"port"`
Timeout *time.Duration `yaml:"timeout"`
Verbose *bool `yaml:"verbose"`
}
var config *Config
_, err := dsco.Fill(
&config,
dsco.WithCmdlineLayer(),
dsco.WithEnvLayer("MYAPP"),
dsco.WithStructLayer(&Config{
Host: dsco.R("localhost"),
Port: dsco.R(8080),
Timeout: dsco.R(30 * time.Second),
Verbose: dsco.R(false),
}, "defaults"),
)
Layer Types ¶
The library supports multiple configuration sources:
- Command line arguments (WithCmdlineLayer, WithStrictCmdlineLayer) - Environment variables (WithEnvLayer, WithStrictEnvLayer) - Go structs (WithStructLayer, WithStrictStructLayer) - Custom string providers (WithStringValueProvider, WithStrictStringValueProvider) - File-based providers (via external packages like kfile)
Safety Design ¶
dsco enforces explicit configuration through pointer-based fields:
- nil clearly means "not configured" - Non-nil values mean "explicitly configured" - Zero values (0, "", false) don't mask missing configuration - Services fail fast with clear error messages - No hidden defaults or silent failures
Strict Mode ¶
Strict mode layers detect unused configuration values and conflicts:
_, err := dsco.Fill(
&config,
dsco.WithStrictCmdlineLayer(),
dsco.WithStrictEnvLayer("MYAPP"),
)
This ensures all provided configuration values are actually used, preventing configuration drift and accidental misconfigurations.
Error Handling ¶
The library provides comprehensive error types with location tracking:
if err != nil {
var layerErr LayerErrors
if errors.As(err, &layerErr) {
// Handle layer registration errors
}
var fillerErr FillerErrors
if errors.As(err, &fillerErr) {
// Handle field filling errors
}
}
Testing ¶
The dsco library maintains 100% test coverage across all core packages, ensuring reliability and stability for production use. The test suite includes:
- Unit tests for all public APIs - Integration tests for multi-layer scenarios - Error path testing with comprehensive validation - Concurrent access testing for thread safety - Edge case handling with boundary conditions
The testing infrastructure follows project standards: - Parallel test execution with t.Parallel() - Mock-based testing using testify framework - Table-driven test patterns for comprehensive coverage - Error assertion using testify.ErrorIs, ErrorAs, and Contains
Architecture ¶
The library implements a sophisticated processing pipeline:
1. Layer Registration: Different configuration sources register as layers 2. Model Generation: Target struct is analyzed via reflection 3. Value Collection: Each layer provides field values from its source 4. Precedence Resolution: Later layers override earlier ones 5. Type Conversion: String values converted to target types via YAML 6. Validation: Required fields and custom validation applied 7. Struct Filling: Target struct populated with resolved values
For complete documentation and examples, see: https://pkg.go.dev/github.com/byte4ever/dsco
Package dsco provides configuration processing using layered sources. This file contains the core filling logic that orchestrates the configuration process from source layers to final struct population.
Package dsco layer policy definitions. This file defines constraint policies for configuration layers, distinguishing between strict and normal processing modes.
Index ¶
- Variables
- func Fill(inputModelRef any, layers ...Layer) (plocation.Locations, error)
- func R[T any](v T) *T
- type AliasCollisionError
- type AliasesOption
- type CmdlineAlreadyUsedError
- type CmdlineLayer
- type DuplicateEnvPrefixError
- type DuplicateInputStructError
- type DuplicateStringProviderError
- type DuplicateStructIDError
- type EnvLayer
- type FieldValuesGetter
- type FillerErrors
- type GetError
- type InvalidInputError
- type Layer
- type LayerErrors
- type Layers
- type ModelInterface
- type NamedStringValuesProvider
- type Option
- type OverriddenKeyError
- type ParseError
- type PoliciesGetter
- type StrictCmdlineLayer
- type StrictEnvLayer
- type StrictStringProviderLayer
- type StrictStructLayer
- type StringBasedBuilder
- type StringProviderLayer
- type StringValuesProvider
- type StructBuilder
- type StructLayer
- type UnboundedLocationError
- type UnboundedLocationErrors
Constants ¶
This section is empty.
Variables ¶
var ErrAliasCollision = errors.New("alias collision")
ErrAliasCollision represents an error indicating that an alias is colliding with an actual key in the structure.
var ErrCmdlineAlreadyUsed = errors.New("")
ErrCmdlineAlreadyUsed is the sentinel error for cmdline already used.
var ErrDuplicateEnvPrefix = errors.New("")
ErrDuplicateEnvPrefix is the sentinel error for duplicate env prefix.
var ErrDuplicateInputStruct = errors.New("")
ErrDuplicateInputStruct is the sentinel error for duplicate input struct.
var ErrDuplicateStructID = errors.New("")
ErrDuplicateStructID is the sentinel error for duplicate struct ID.
var ErrFiller = errors.New("")
ErrFiller is the sentinel error for configuration filling failures.
var ErrGet = errors.New("")
var ErrInvalidInput = errors.New("")
ErrInvalidInput is the sentinel error for invalid input types.
var ErrInvalidType = errors.New("invalid type")
ErrInvalidType represent an error where ....
var ErrLayer = errors.New("")
ErrLayer is the sentinel error for layer errors.
var ErrNilInput = errors.New("nil input")
ErrNilInput is dummy...
var ErrNilProvider = errors.New("nil provider")
ErrNilProvider is shitty...
var ErrNoAliasesProvided = errors.New("no aliases provided")
ErrNoAliasesProvided represent an error where no aliases map was provided with option.
var ErrOverriddenKey = errors.New("overridden key")
ErrOverriddenKey represents an error indicating that a potential key binding wha overridden in another layer.
var ErrParse = errors.New("parse error")
ErrParse represents an error indicating that a value cannot be parsed.
var ErrStructTypeDiffer = errors.New("struct type differ")
ErrStructTypeDiffer represent an error where cannot produce a valid value bases because struct source type and struct type to fillHelper differs.
var ErrUnboundedLocation = errors.New("unbound key")
ErrUnboundedLocation represents an error indicating that a key is never bound to the structure.
Functions ¶
Types ¶
type AliasCollisionError ¶
type AliasCollisionError struct {
Path string
}
func (AliasCollisionError) Error ¶
func (a AliasCollisionError) Error() string
func (AliasCollisionError) Is ¶
func (AliasCollisionError) Is(err error) bool
type AliasesOption ¶
AliasesOption defines keys aliasing.
func WithAliases ¶
func WithAliases(mapping map[string]string) AliasesOption
WithAliases returns a keys aliasing option.
type CmdlineAlreadyUsedError ¶
type CmdlineAlreadyUsedError struct {
Index int
}
CmdlineAlreadyUsedError represents an error where cmdline is already used.
func (CmdlineAlreadyUsedError) Error ¶
func (c CmdlineAlreadyUsedError) Error() string
CmdlineAlreadyUsedError methods.
func (CmdlineAlreadyUsedError) Is ¶
func (CmdlineAlreadyUsedError) Is(err error) bool
type CmdlineLayer ¶
type CmdlineLayer struct {
// contains filtered or unexported fields
}
CmdlineLayer is a command line layer.
func WithCmdlineLayer ¶
func WithCmdlineLayer(options ...Option) *CmdlineLayer
WithCmdlineLayer creates a command line layer.
type DuplicateEnvPrefixError ¶
DuplicateEnvPrefixError represents an error where env prefix is duplicated.
func (DuplicateEnvPrefixError) Error ¶
func (c DuplicateEnvPrefixError) Error() string
DuplicateEnvPrefixError methods.
func (DuplicateEnvPrefixError) Is ¶
func (DuplicateEnvPrefixError) Is(err error) bool
type DuplicateInputStructError ¶
type DuplicateInputStructError struct {
Index int
}
DuplicateInputStructError represents an error where input struct is duplicated.
func (DuplicateInputStructError) Error ¶
func (c DuplicateInputStructError) Error() string
DuplicateInputStructError methods.
func (DuplicateInputStructError) Is ¶
func (DuplicateInputStructError) Is(err error) bool
type DuplicateStringProviderError ¶
func (DuplicateStringProviderError) Error ¶
func (d DuplicateStringProviderError) Error() string
type DuplicateStructIDError ¶
DuplicateStructIDError represents an error where struct ID is duplicated.
func (DuplicateStructIDError) Error ¶
func (c DuplicateStructIDError) Error() string
DuplicateStructIDError methods.
func (DuplicateStructIDError) Is ¶
func (DuplicateStructIDError) Is(err error) bool
type EnvLayer ¶
type EnvLayer struct {
// contains filtered or unexported fields
}
EnvLayer is an environnement variable layer.
func WithEnvLayer ¶
WithEnvLayer creates an environment variable layer.
type FieldValuesGetter ¶
type FieldValuesGetter interface {
// GetFieldValuesFrom extracts configuration values from the given model.
// Returns a map of field UIDs to their values, or an error if extraction
// fails.
GetFieldValuesFrom(model ModelInterface) (fvalue.Values, error)
}
FieldValuesGetter defines the ability to get field values from a configuration model. This interface is implemented by configuration sources (layers) to provide their configuration values in a standard format that can be merged and processed.
type FillerErrors ¶
FillerErrors aggregates multiple errors that can occur during the configuration filling process across different layers and validation steps.
func (FillerErrors) Is ¶
func (FillerErrors) Is(err error) bool
Is implements error matching for FillerErrors, allowing error.Is checks against ErrFiller to detect filling-related errors.
type InvalidInputError ¶
InvalidInputError represents an error where the input type is invalid.
func (InvalidInputError) Error ¶
func (c InvalidInputError) Error() string
InvalidInputError methods.
func (InvalidInputError) Is ¶
func (InvalidInputError) Is(err error) bool
type Layer ¶
type Layer interface {
// contains filtered or unexported methods
}
Layer defines a configuration layer.
type LayerErrors ¶
LayerErrors represents multiple layer errors.
func (LayerErrors) Is ¶
func (LayerErrors) Is(err error) bool
type ModelInterface ¶
type ModelInterface interface {
// TypeName returns a human-readable name for the configuration type.
// Used for error reporting and debugging.
TypeName() string
// ApplyOn extracts configuration values using the provided value getter.
// Returns field values mapped by their unique identifiers.
ApplyOn(g internal.ValueGetter) (fvalue.Values, error)
// Expand processes struct expansion using the provided struct expander.
// This handles complex configuration structures and nested types.
Expand(g internal.StructExpander) error
// GetFieldValuesFor extracts field values from the given struct value.
// Used to convert Go structs into the internal field value representation.
GetFieldValuesFor(id string, v reflect.Value) fvalue.Values
// Fill populates the target struct with values from configuration layers.
// Returns location information for debugging and error reporting.
Fill(
inputModelValue reflect.Value,
layers []fvalue.Values,
) (plocation.Locations, error)
}
ModelInterface represents a configuration structure model that defines how configuration data should be processed, validated, and filled into Go structs. It provides the metadata and operations needed for dsco's configuration processing.
type NamedStringValuesProvider ¶
type NamedStringValuesProvider interface {
StringValuesProvider
// GetName returns a human-readable identifier for this provider,
// used in error messages and debugging output.
GetName() string
}
NamedStringValuesProvider extends StringValuesProvider with identification capabilities, allowing the provider to be uniquely named for debugging and error reporting purposes.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is processing option for string based binder.
type OverriddenKeyError ¶
func (OverriddenKeyError) Error ¶
func (a OverriddenKeyError) Error() string
func (OverriddenKeyError) Is ¶
func (OverriddenKeyError) Is(err error) bool
type ParseError ¶
func (ParseError) Error ¶
func (a ParseError) Error() string
func (ParseError) Is ¶
func (ParseError) Is(err error) bool
type PoliciesGetter ¶
type PoliciesGetter interface {
// GetPolicies transforms configuration layers into policy objects
// that determine how values are processed and validated.
GetPolicies() (constraintLayerPolicies, error)
}
PoliciesGetter defines the ability to retrieve layer policies for configuration processing, converting layers into constraint policies.
type StrictCmdlineLayer ¶
type StrictCmdlineLayer struct {
// contains filtered or unexported fields
}
StrictCmdlineLayer is a strict command line layer.
func WithStrictCmdlineLayer ¶
func WithStrictCmdlineLayer(options ...Option) *StrictCmdlineLayer
WithStrictCmdlineLayer creates a strict command line layer. It can be used only once.
type StrictEnvLayer ¶
type StrictEnvLayer struct {
// contains filtered or unexported fields
}
StrictEnvLayer is a strict environnement variables layer.
func WithStrictEnvLayer ¶
func WithStrictEnvLayer(prefix string, options ...Option) *StrictEnvLayer
WithStrictEnvLayer creates a new strict environment layer.
type StrictStringProviderLayer ¶
type StrictStringProviderLayer struct {
StringProviderLayer
}
func WithStrictStringValueProvider ¶
func WithStrictStringValueProvider( provider NamedStringValuesProvider, options ...Option, ) *StrictStringProviderLayer
type StrictStructLayer ¶
type StrictStructLayer struct {
// contains filtered or unexported fields
}
StrictStructLayer is a strict structure layer.
func WithStrictStructLayer ¶
func WithStrictStructLayer(input any, id string) *StrictStructLayer
WithStrictStructLayer creates a new strict structure layer.
type StringBasedBuilder ¶
type StringBasedBuilder struct {
// contains filtered or unexported fields
}
StringBasedBuilder is a value bases builder depending on text values.
func CmdLine ¶
func CmdLine(options ...Option) ( *StringBasedBuilder, error, )
CmdLine builds a command line manager.
func Env ¶
func Env(prefix string, options ...Option) ( *StringBasedBuilder, error, )
Env builds a env manager.
func NewStringBasedBuilder ¶
func NewStringBasedBuilder( provider StringValuesProvider, options ...Option, ) ( *StringBasedBuilder, error, )
NewStringBasedBuilder creates a bases builder for the provided path/text value set.
func (*StringBasedBuilder) ExpandStruct ¶
func (s *StringBasedBuilder) ExpandStruct( path string, _type reflect.Type) ( err error, )
func (*StringBasedBuilder) GetFieldValuesFrom ¶
func (s *StringBasedBuilder) GetFieldValuesFrom( _model ModelInterface, ) ( fvalue.Values, error, )
GetBaseFor creates the bases.
type StringProviderLayer ¶
type StringProviderLayer struct {
// contains filtered or unexported fields
}
func WithStringValueProvider ¶
func WithStringValueProvider( provider NamedStringValuesProvider, options ...Option, ) *StringProviderLayer
type StringValuesProvider ¶
type StringValuesProvider interface {
// GetStringValues retrieves all string values provided by this source,
// returning a map of keys to their corresponding string values and
// locations.
GetStringValues() svalue.Values
}
StringValuesProvider defines the interface for providers that supply string-based configuration values from various sources like environment variables, command line arguments, or configuration files.
type StructBuilder ¶
type StructBuilder struct {
// contains filtered or unexported fields
}
StructBuilder is a structure layer builder.
func NewStructBuilder ¶
func NewStructBuilder(inputStruct any, id string) (*StructBuilder, error)
NewStructBuilder creates a new structure layer builder.
func (*StructBuilder) GetFieldValuesFrom ¶
func (s *StructBuilder) GetFieldValuesFrom(model ModelInterface) ( fvalue.Values, error, )
type StructLayer ¶
type StructLayer struct {
// contains filtered or unexported fields
}
StructLayer is a structure layer.
func WithStructLayer ¶
func WithStructLayer(input any, id string) *StructLayer
WithStructLayer creates a structure layer.
type UnboundedLocationError ¶
type UnboundedLocationError struct {
Location string
}
func (UnboundedLocationError) Error ¶
func (a UnboundedLocationError) Error() string
func (UnboundedLocationError) Is ¶
func (UnboundedLocationError) Is(err error) bool
type UnboundedLocationErrors ¶
type UnboundedLocationErrors []UnboundedLocationError
func (UnboundedLocationErrors) Len ¶
func (u UnboundedLocationErrors) Len() int
func (UnboundedLocationErrors) Less ¶
func (u UnboundedLocationErrors) Less(i, j int) bool
func (UnboundedLocationErrors) Swap ¶
func (u UnboundedLocationErrors) Swap(i, j int)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
dummy
|
|
|
examples
|
|
|
deadsimple
command
|
|
|
deadsimple2
command
|
|
|
simplemain
command
|
|
|
Package fvalue provides field value types with location tracking for dsco's internal configuration processing pipeline.
|
Package fvalue provides field value types with location tracking for dsco's internal configuration processing pipeline. |
|
Package hit provides hash-based utilities for dsco's deduplication and caching systems.
|
Package hit provides hash-based utilities for dsco's deduplication and caching systems. |
|
hprovider
Package hprovider provides hash instance pooling for efficient hash computation in dsco's hit package.
|
Package hprovider provides hash instance pooling for efficient hash computation in dsco's hit package. |
|
cmdline
Package cmdline provides command-line argument parsing functionality for dsco's configuration system.
|
Package cmdline provides command-line argument parsing functionality for dsco's configuration system. |
|
env
Package env provides environment variable parsing functionality for dsco's configuration system.
|
Package env provides environment variable parsing functionality for dsco's configuration system. |
|
fvalue
Package fvalue provides field value representation for dsco's configuration processing system.
|
Package fvalue provides field value representation for dsco's configuration processing system. |
|
ierror
Package ierror provides indexed error handling for dsco's configuration processing system.
|
Package ierror provides indexed error handling for dsco's configuration processing system. |
|
kfile
Package kfile provides file-based configuration value extraction for dsco's configuration system.
|
Package kfile provides file-based configuration value extraction for dsco's configuration system. |
|
merror
Package merror provides multiple error aggregation functionality for dsco's configuration processing system.
|
Package merror provides multiple error aggregation functionality for dsco's configuration processing system. |
|
model
Package model provides reflection-based struct analysis and field mapping for dsco's configuration processing system.
|
Package model provides reflection-based struct analysis and field mapping for dsco's configuration processing system. |
|
plocation
Package plocation provides path location tracking for dsco's configuration debugging and reporting system.
|
Package plocation provides path location tracking for dsco's configuration debugging and reporting system. |
|
utils
Package utils provides utility functions for dsco's configuration processing system.
|
Package utils provides utility functions for dsco's configuration processing system. |
|
Package ref provides utility functions for creating references to values.
|
Package ref provides utility functions for creating references to values. |
|
Package registry provides type name registration and formatting utilities for dsco's reflection-based configuration system.
|
Package registry provides type name registration and formatting utilities for dsco's reflection-based configuration system. |
|
Package svalue provides string value types with location tracking for dsco configuration sources.
|
Package svalue provides string value types with location tracking for dsco configuration sources. |
|
Package url provides an unmarshallable URL type for dsco configuration fields.
|
Package url provides an unmarshallable URL type for dsco configuration fields. |