feeders

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package feeders provides configuration feeders for reading data from various sources including environment variables, JSON, YAML, TOML files, and .env files.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDotEnvInvalidStructureType = errors.New("expected pointer to struct")
	ErrDotEnvUnsupportedType      = errors.New("unsupported type")
	ErrDotEnvInvalidLineFormat    = errors.New("invalid .env line format")
)

DotEnv feeder errors

View Source
var (
	ErrJSONExpectedMapForStruct      = errors.New("expected map for struct field")
	ErrJSONCannotConvert             = errors.New("cannot convert value to field type")
	ErrJSONCannotConvertSliceElement = errors.New("cannot convert slice element")
	ErrJSONExpectedArrayForSlice     = errors.New("expected array for slice field")
	ErrJSONFieldCannotBeSet          = errors.New("field cannot be set")
	ErrJSONArraySizeExceeded         = errors.New("array size exceeded")
)

JSON feeder errors

View Source
var (
	ErrTomlExpectedMapForStruct      = errors.New("expected map for struct field")
	ErrTomlCannotConvert             = errors.New("cannot convert value to field type")
	ErrTomlCannotConvertSliceElement = errors.New("cannot convert slice element")
	ErrTomlExpectedArrayForSlice     = errors.New("expected array for slice field")
	ErrTomlFieldCannotBeSet          = errors.New("field cannot be set")
	ErrTomlArraySizeExceeded         = errors.New("array size exceeded")
)

TOML feeder errors

View Source
var (
	ErrYamlFieldCannotBeSet     = errors.New("field cannot be set")
	ErrYamlUnsupportedFieldType = errors.New("unsupported field type")
	ErrYamlTypeConversion       = errors.New("type conversion error")
	ErrYamlBoolConversion       = errors.New("cannot convert string to bool")
	ErrYamlExpectedMap          = errors.New("expected map for field")
	ErrYamlExpectedArray        = errors.New("expected array for field")
	ErrYamlArraySizeExceeded    = errors.New("array size exceeded")
	ErrYamlExpectedMapForSlice  = errors.New("expected map for slice element")
)

YAML feeder errors

View Source
var (
	ErrJsonFeederUnavailable = errors.New("json feeder unavailable")
	ErrTomlFeederUnavailable = errors.New("toml feeder unavailable")
)

General feeder errors

View Source
var ErrEnvEmptyPrefixAndSuffix = errors.New("env: prefix or suffix cannot be empty")

ErrEnvEmptyPrefixAndSuffix indicates that both prefix and suffix cannot be empty

View Source
var ErrEnvInvalidStructure = errors.New("env: invalid structure")

ErrEnvInvalidStructure indicates that the provided structure is not valid for environment variable processing

View Source
var ErrFieldCannotBeSet = errors.New("field cannot be set")

ErrFieldCannotBeSet indicates that a field cannot be set

View Source
var (
	ErrInstancesMustBeMap = fmt.Errorf("instances must be a map")
)

Static errors for err113 compliance

Functions

func GetAvailableEnvironments added in v1.4.3

func GetAvailableEnvironments(configDir string) []string

GetAvailableEnvironments returns the list of available environments in the config directory in alphabetical order for consistent, deterministic behavior

func IsBaseConfigStructure added in v1.4.3

func IsBaseConfigStructure(configDir string) bool

IsBaseConfigStructure checks if the given directory has the expected base config structure

func ResetGlobalEnvCatalog added in v1.3.4

func ResetGlobalEnvCatalog()

ResetGlobalEnvCatalog resets the global environment catalog (useful for testing)

Types

type AffixedEnvFeeder

type AffixedEnvFeeder struct {
	Prefix string
	Suffix string
	// contains filtered or unexported fields
}

AffixedEnvFeeder is a feeder that reads environment variables with a prefix and/or suffix

func NewAffixedEnvFeeder

func NewAffixedEnvFeeder(prefix, suffix string) AffixedEnvFeeder

NewAffixedEnvFeeder creates a new AffixedEnvFeeder with the specified prefix and suffix

func (*AffixedEnvFeeder) Feed

func (f *AffixedEnvFeeder) Feed(structure interface{}) error

Feed reads environment variables and populates the provided structure

func (*AffixedEnvFeeder) SetFieldTracker added in v1.3.4

func (f *AffixedEnvFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for recording field populations

func (*AffixedEnvFeeder) SetVerboseDebug added in v1.3.3

func (f *AffixedEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type BaseConfigFeeder added in v1.4.3

type BaseConfigFeeder struct {
	BaseDir     string // Directory containing base/ and environments/ subdirectories
	Environment string // Environment name (e.g., "prod", "staging", "dev")
	// contains filtered or unexported fields
}

BaseConfigFeeder supports layered configuration loading with base configs and environment-specific overrides

func NewBaseConfigFeeder added in v1.4.3

func NewBaseConfigFeeder(baseDir, environment string) *BaseConfigFeeder

NewBaseConfigFeeder creates a new base configuration feeder baseDir should contain base/ and environments/ subdirectories environment specifies which environment overrides to apply (e.g., "prod", "staging", "dev")

func (*BaseConfigFeeder) Feed added in v1.4.3

func (b *BaseConfigFeeder) Feed(structure interface{}) error

Feed loads and merges base configuration with environment-specific overrides

func (*BaseConfigFeeder) FeedKey added in v1.4.3

func (b *BaseConfigFeeder) FeedKey(key string, target interface{}) error

FeedKey loads and merges configurations for a specific key

func (*BaseConfigFeeder) SetFieldTracker added in v1.4.3

func (b *BaseConfigFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for recording field populations

func (*BaseConfigFeeder) SetVerboseDebug added in v1.4.3

func (b *BaseConfigFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type DefaultFieldTracker added in v1.3.4

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

DefaultFieldTracker is a basic implementation of FieldTracker

func NewDefaultFieldTracker added in v1.3.4

func NewDefaultFieldTracker() *DefaultFieldTracker

NewDefaultFieldTracker creates a new DefaultFieldTracker

func (*DefaultFieldTracker) GetFieldPopulations added in v1.3.4

func (t *DefaultFieldTracker) GetFieldPopulations() []FieldPopulation

GetFieldPopulations returns all recorded field populations

func (*DefaultFieldTracker) RecordFieldPopulation added in v1.3.4

func (t *DefaultFieldTracker) RecordFieldPopulation(fp FieldPopulation)

RecordFieldPopulation records that a field was populated by a feeder

type DotEnvFeeder

type DotEnvFeeder struct {
	Path string
	// contains filtered or unexported fields
}

DotEnvFeeder is a feeder that reads .env files and populates configuration directly from the parsed values

func NewDotEnvFeeder

func NewDotEnvFeeder(filePath string) *DotEnvFeeder

NewDotEnvFeeder creates a new DotEnvFeeder that reads from the specified .env file

func (*DotEnvFeeder) Feed added in v1.3.3

func (f *DotEnvFeeder) Feed(structure interface{}) error

Feed reads the .env file and populates the provided structure directly

func (*DotEnvFeeder) SetFieldTracker added in v1.3.4

func (f *DotEnvFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for recording field populations

func (*DotEnvFeeder) SetVerboseDebug added in v1.3.3

func (f *DotEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type EnvCatalog added in v1.3.4

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

EnvCatalog manages a unified view of environment variables from multiple sources

func GetGlobalEnvCatalog added in v1.3.4

func GetGlobalEnvCatalog() *EnvCatalog

GetGlobalEnvCatalog returns the global environment catalog

func NewEnvCatalog added in v1.3.4

func NewEnvCatalog() *EnvCatalog

NewEnvCatalog creates a new environment variable catalog

func (*EnvCatalog) Clear added in v1.3.4

func (c *EnvCatalog) Clear()

Clear removes all variables from the catalog

func (*EnvCatalog) ClearDynamicEnvCache added in v1.3.4

func (c *EnvCatalog) ClearDynamicEnvCache()

ClearDynamicEnvCache clears dynamically loaded environment variables from cache This is useful for testing when environment variables change between tests

func (*EnvCatalog) Get added in v1.3.4

func (c *EnvCatalog) Get(key string) (string, bool)

Get retrieves a variable from the catalog, always checking current OS environment

func (*EnvCatalog) GetAll added in v1.3.4

func (c *EnvCatalog) GetAll() map[string]string

GetAll returns all variables in the catalog

func (*EnvCatalog) GetSource added in v1.3.4

func (c *EnvCatalog) GetSource(key string) string

GetSource returns the source that provided a variable

func (*EnvCatalog) LoadFromDotEnv added in v1.3.4

func (c *EnvCatalog) LoadFromDotEnv(filename string) error

LoadFromDotEnv loads variables from a .env file into the catalog

func (*EnvCatalog) Set added in v1.3.4

func (c *EnvCatalog) Set(key, value, source string)

Set manually sets a variable in the catalog

type EnvFeeder

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

EnvFeeder is a feeder that reads environment variables with optional verbose debug logging and field tracking

func NewEnvFeeder

func NewEnvFeeder() *EnvFeeder

NewEnvFeeder creates a new EnvFeeder that reads from environment variables

func (*EnvFeeder) Feed added in v1.3.3

func (f *EnvFeeder) Feed(structure interface{}) error

Feed implements the Feeder interface with optional verbose logging

func (*EnvFeeder) FeedWithModuleContext added in v1.4.3

func (f *EnvFeeder) FeedWithModuleContext(structure interface{}, moduleName string) error

FeedWithModuleContext implements module-aware feeding that searches for module-prefixed environment variables

func (*EnvFeeder) SetFieldTracker added in v1.3.4

func (f *EnvFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for this feeder

func (*EnvFeeder) SetVerboseDebug added in v1.3.3

func (f *EnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type Feeder added in v1.2.6

type Feeder interface {
	Feed(target interface{}) error
}

Feeder interface for common operations

type FieldPopulation added in v1.3.4

type FieldPopulation struct {
	FieldPath   string      // Full path to the field (e.g., "Connections.primary.DSN")
	FieldName   string      // Name of the field
	FieldType   string      // Type of the field
	FeederType  string      // Type of feeder that populated it
	SourceType  string      // Type of source (env, yaml, etc.)
	SourceKey   string      // Source key that was used (e.g., "DB_PRIMARY_DSN")
	Value       interface{} // Value that was set
	InstanceKey string      // Instance key for instance-aware fields
	SearchKeys  []string    // All keys that were searched for this field
	FoundKey    string      // The key that was actually found
}

FieldPopulation represents a single field population event

type FieldTracker added in v1.3.4

type FieldTracker interface {
	// RecordFieldPopulation records that a field was populated by a feeder
	RecordFieldPopulation(fp FieldPopulation)
}

FieldTracker interface allows feeders to report which fields they populate

type InstanceAwareEnvFeeder added in v1.3.1

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

InstanceAwareEnvFeeder is a feeder that can handle environment variables for multiple instances of the same configuration type using instance-specific prefixes with field tracking support

func NewInstanceAwareEnvFeeder added in v1.3.1

func NewInstanceAwareEnvFeeder(prefixFunc InstancePrefixFunc) *InstanceAwareEnvFeeder

NewInstanceAwareEnvFeeder creates a new instance-aware environment variable feeder

func (*InstanceAwareEnvFeeder) Feed added in v1.3.1

func (f *InstanceAwareEnvFeeder) Feed(structure interface{}) error

Feed implements the basic Feeder interface for single instances (backward compatibility)

func (*InstanceAwareEnvFeeder) FeedInstances added in v1.3.1

func (f *InstanceAwareEnvFeeder) FeedInstances(instances interface{}) error

FeedInstances feeds multiple instances of the same configuration type

func (*InstanceAwareEnvFeeder) FeedKey added in v1.3.1

func (f *InstanceAwareEnvFeeder) FeedKey(instanceKey string, structure interface{}) error

FeedKey implements the ComplexFeeder interface for instance-specific feeding

func (*InstanceAwareEnvFeeder) SetFieldTracker added in v1.3.4

func (f *InstanceAwareEnvFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for this feeder

func (*InstanceAwareEnvFeeder) SetVerboseDebug added in v1.3.3

func (f *InstanceAwareEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type InstancePrefixFunc added in v1.3.1

type InstancePrefixFunc func(instanceKey string) string

InstancePrefixFunc is a function that generates a prefix for an instance key

type JSONFeeder added in v1.2.6

type JSONFeeder struct {
	Path string
	// contains filtered or unexported fields
}

JSONFeeder is a feeder that reads JSON files with optional verbose debug logging

func NewJSONFeeder added in v1.2.6

func NewJSONFeeder(filePath string) *JSONFeeder

NewJSONFeeder creates a new JSONFeeder that reads from the specified JSON file

func (*JSONFeeder) Feed added in v1.3.3

func (j *JSONFeeder) Feed(structure interface{}) error

Feed reads the JSON file and populates the provided structure

func (*JSONFeeder) FeedKey added in v1.2.6

func (j *JSONFeeder) FeedKey(key string, target interface{}) error

FeedKey reads a JSON file and extracts a specific key

func (*JSONFeeder) SetFieldTracker added in v1.3.4

func (j *JSONFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for recording field populations

func (*JSONFeeder) SetVerboseDebug added in v1.3.3

func (j *JSONFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type TenantAffixedEnvFeeder added in v1.1.0

type TenantAffixedEnvFeeder struct {
	*AffixedEnvFeeder
	SetPrefixFunc func(string)
	SetSuffixFunc func(string)
	// contains filtered or unexported fields
}

TenantAffixedEnvFeeder is a feeder that reads environment variables with tenant-specific prefixes and suffixes

func NewTenantAffixedEnvFeeder added in v1.1.0

func NewTenantAffixedEnvFeeder(prefix, suffix func(string) string) *TenantAffixedEnvFeeder

NewTenantAffixedEnvFeeder creates a new TenantAffixedEnvFeeder with the given prefix and suffix functions The prefix and suffix functions are used to modify the prefix and suffix of the environment variables before they are used to set the struct fields The prefix function is used to modify the prefix of the environment variables The suffix function is used to modify the suffix of the environment variables

func (*TenantAffixedEnvFeeder) Feed added in v1.3.9

func (f *TenantAffixedEnvFeeder) Feed(structure interface{}) error

Feed implements the basic Feeder interface but requires tenant context For TenantAffixedEnvFeeder, use FeedKey instead to provide tenant context

func (*TenantAffixedEnvFeeder) FeedKey added in v1.3.9

func (f *TenantAffixedEnvFeeder) FeedKey(tenantID string, structure interface{}) error

FeedKey implements the ComplexFeeder interface for tenant-specific feeding

func (*TenantAffixedEnvFeeder) SetFieldTracker added in v1.3.4

func (f *TenantAffixedEnvFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for recording field populations

func (*TenantAffixedEnvFeeder) SetVerboseDebug added in v1.3.3

func (f *TenantAffixedEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type TomlFeeder

type TomlFeeder struct {
	Path string
	// contains filtered or unexported fields
}

TomlFeeder is a feeder that reads TOML files with optional verbose debug logging

func NewTomlFeeder

func NewTomlFeeder(filePath string) *TomlFeeder

NewTomlFeeder creates a new TomlFeeder that reads from the specified TOML file

func (*TomlFeeder) Feed added in v1.3.3

func (t *TomlFeeder) Feed(structure interface{}) error

Feed reads the TOML file and populates the provided structure

func (*TomlFeeder) FeedKey

func (t *TomlFeeder) FeedKey(key string, target interface{}) error

FeedKey reads a TOML file and extracts a specific key

func (*TomlFeeder) SetFieldTracker added in v1.3.4

func (t *TomlFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for recording field populations

func (*TomlFeeder) SetVerboseDebug added in v1.3.3

func (t *TomlFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

type YamlFeeder

type YamlFeeder struct {
	Path string
	// contains filtered or unexported fields
}

YamlFeeder is a feeder that reads YAML files with optional verbose debug logging

func NewYamlFeeder

func NewYamlFeeder(filePath string) *YamlFeeder

NewYamlFeeder creates a new YamlFeeder that reads from the specified YAML file

func (*YamlFeeder) Feed added in v1.3.3

func (y *YamlFeeder) Feed(structure interface{}) error

Feed reads the YAML file and populates the provided structure

func (*YamlFeeder) FeedKey

func (y *YamlFeeder) FeedKey(key string, target interface{}) error

FeedKey reads a YAML file and extracts a specific key

func (*YamlFeeder) SetFieldTracker added in v1.3.4

func (y *YamlFeeder) SetFieldTracker(tracker FieldTracker)

SetFieldTracker sets the field tracker for recording field populations

func (*YamlFeeder) SetVerboseDebug added in v1.3.3

func (y *YamlFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })

SetVerboseDebug enables or disables verbose debug logging

Jump to

Keyboard shortcuts

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