Documentation
¶
Overview ¶
Package feeders provides configuration feeders for reading data from various sources including environment variables, JSON, YAML, TOML files, and .env files.
Index ¶
- Variables
- func ResetGlobalEnvCatalog()
- type AffixedEnvFeeder
- type DefaultFieldTracker
- type DotEnvFeeder
- type EnvCatalog
- func (c *EnvCatalog) Clear()
- func (c *EnvCatalog) ClearDynamicEnvCache()
- func (c *EnvCatalog) Get(key string) (string, bool)
- func (c *EnvCatalog) GetAll() map[string]string
- func (c *EnvCatalog) GetSource(key string) string
- func (c *EnvCatalog) LoadFromDotEnv(filename string) error
- func (c *EnvCatalog) Set(key, value, source string)
- type EnvFeeder
- type Feeder
- type FieldPopulation
- type FieldTracker
- type InstanceAwareEnvFeeder
- func (f *InstanceAwareEnvFeeder) Feed(structure interface{}) error
- func (f *InstanceAwareEnvFeeder) FeedInstances(instances interface{}) error
- func (f *InstanceAwareEnvFeeder) FeedKey(instanceKey string, structure interface{}) error
- func (f *InstanceAwareEnvFeeder) SetFieldTracker(tracker FieldTracker)
- func (f *InstanceAwareEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ ... })
- type InstancePrefixFunc
- type JSONFeeder
- type TenantAffixedEnvFeeder
- func (f *TenantAffixedEnvFeeder) Feed(structure interface{}) error
- func (f *TenantAffixedEnvFeeder) FeedKey(tenantID string, structure interface{}) error
- func (f *TenantAffixedEnvFeeder) SetFieldTracker(tracker FieldTracker)
- func (f *TenantAffixedEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ ... })
- type TomlFeeder
- type YamlFeeder
Constants ¶
This section is empty.
Variables ¶
var ( ErrDotEnvInvalidStructureType = errors.New("expected pointer to struct") ErrDotEnvUnsupportedType = errors.New("unsupported type") ErrDotEnvInvalidLineFormat = errors.New("invalid .env line format") )
DotEnv feeder errors
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") )
JSON feeder errors
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") )
TOML feeder errors
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") )
YAML feeder errors
var ( )
General feeder errors
var ErrEnvEmptyPrefixAndSuffix = errors.New("env: prefix or suffix cannot be empty")
ErrEnvEmptyPrefixAndSuffix indicates that both prefix and suffix cannot be empty
var ErrEnvInvalidStructure = errors.New("env: invalid structure")
ErrEnvInvalidStructure indicates that the provided structure is not valid for environment variable processing
var ErrFieldCannotBeSet = errors.New("field cannot be set")
ErrFieldCannotBeSet indicates that a field cannot be set
var (
ErrInstancesMustBeMap = fmt.Errorf("instances must be a map")
)
Static errors for err113 compliance
Functions ¶
func ResetGlobalEnvCatalog ¶
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 ¶
func (f *AffixedEnvFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for recording field populations
func (*AffixedEnvFeeder) SetVerboseDebug ¶
func (f *AffixedEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })
SetVerboseDebug enables or disables verbose debug logging
type DefaultFieldTracker ¶
type DefaultFieldTracker struct {
// contains filtered or unexported fields
}
DefaultFieldTracker is a basic implementation of FieldTracker
func NewDefaultFieldTracker ¶
func NewDefaultFieldTracker() *DefaultFieldTracker
NewDefaultFieldTracker creates a new DefaultFieldTracker
func (*DefaultFieldTracker) GetFieldPopulations ¶
func (t *DefaultFieldTracker) GetFieldPopulations() []FieldPopulation
GetFieldPopulations returns all recorded field populations
func (*DefaultFieldTracker) RecordFieldPopulation ¶
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 ¶
func (f *DotEnvFeeder) Feed(structure interface{}) error
Feed reads the .env file and populates the provided structure directly
func (*DotEnvFeeder) SetFieldTracker ¶
func (f *DotEnvFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for recording field populations
func (*DotEnvFeeder) SetVerboseDebug ¶
func (f *DotEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })
SetVerboseDebug enables or disables verbose debug logging
type EnvCatalog ¶
type EnvCatalog struct {
// contains filtered or unexported fields
}
EnvCatalog manages a unified view of environment variables from multiple sources
func GetGlobalEnvCatalog ¶
func GetGlobalEnvCatalog() *EnvCatalog
GetGlobalEnvCatalog returns the global environment catalog
func NewEnvCatalog ¶
func NewEnvCatalog() *EnvCatalog
NewEnvCatalog creates a new environment variable catalog
func (*EnvCatalog) Clear ¶
func (c *EnvCatalog) Clear()
Clear removes all variables from the catalog
func (*EnvCatalog) ClearDynamicEnvCache ¶
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 ¶
func (c *EnvCatalog) Get(key string) (string, bool)
Get retrieves a variable from the catalog, always checking current OS environment
func (*EnvCatalog) GetAll ¶
func (c *EnvCatalog) GetAll() map[string]string
GetAll returns all variables in the catalog
func (*EnvCatalog) GetSource ¶
func (c *EnvCatalog) GetSource(key string) string
GetSource returns the source that provided a variable
func (*EnvCatalog) LoadFromDotEnv ¶
func (c *EnvCatalog) LoadFromDotEnv(filename string) error
LoadFromDotEnv loads variables from a .env file into the catalog
func (*EnvCatalog) Set ¶
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) SetFieldTracker ¶
func (f *EnvFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for this feeder
type Feeder ¶
type Feeder interface {
Feed(target interface{}) error
}
Feeder interface for common operations
type FieldPopulation ¶
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 ¶
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 ¶
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 ¶
func NewInstanceAwareEnvFeeder(prefixFunc InstancePrefixFunc) *InstanceAwareEnvFeeder
NewInstanceAwareEnvFeeder creates a new instance-aware environment variable feeder
func (*InstanceAwareEnvFeeder) Feed ¶
func (f *InstanceAwareEnvFeeder) Feed(structure interface{}) error
Feed implements the basic Feeder interface for single instances (backward compatibility)
func (*InstanceAwareEnvFeeder) FeedInstances ¶
func (f *InstanceAwareEnvFeeder) FeedInstances(instances interface{}) error
FeedInstances feeds multiple instances of the same configuration type
func (*InstanceAwareEnvFeeder) FeedKey ¶
func (f *InstanceAwareEnvFeeder) FeedKey(instanceKey string, structure interface{}) error
FeedKey implements the ComplexFeeder interface for instance-specific feeding
func (*InstanceAwareEnvFeeder) SetFieldTracker ¶
func (f *InstanceAwareEnvFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for this feeder
func (*InstanceAwareEnvFeeder) SetVerboseDebug ¶
func (f *InstanceAwareEnvFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })
SetVerboseDebug enables or disables verbose debug logging
type InstancePrefixFunc ¶
InstancePrefixFunc is a function that generates a prefix for an instance key
type JSONFeeder ¶
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 ¶
func NewJSONFeeder(filePath string) *JSONFeeder
NewJSONFeeder creates a new JSONFeeder that reads from the specified JSON file
func (*JSONFeeder) Feed ¶
func (j *JSONFeeder) Feed(structure interface{}) error
Feed reads the JSON file and populates the provided structure
func (*JSONFeeder) FeedKey ¶
func (j *JSONFeeder) FeedKey(key string, target interface{}) error
FeedKey reads a JSON file and extracts a specific key
func (*JSONFeeder) SetFieldTracker ¶
func (j *JSONFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for recording field populations
func (*JSONFeeder) SetVerboseDebug ¶
func (j *JSONFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })
SetVerboseDebug enables or disables verbose debug logging
type TenantAffixedEnvFeeder ¶
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 ¶
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 ¶
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 ¶
func (f *TenantAffixedEnvFeeder) FeedKey(tenantID string, structure interface{}) error
FeedKey implements the ComplexFeeder interface for tenant-specific feeding
func (*TenantAffixedEnvFeeder) SetFieldTracker ¶
func (f *TenantAffixedEnvFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for recording field populations
func (*TenantAffixedEnvFeeder) SetVerboseDebug ¶
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 ¶
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 ¶
func (t *TomlFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for recording field populations
func (*TomlFeeder) SetVerboseDebug ¶
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 ¶
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 ¶
func (y *YamlFeeder) SetFieldTracker(tracker FieldTracker)
SetFieldTracker sets the field tracker for recording field populations
func (*YamlFeeder) SetVerboseDebug ¶
func (y *YamlFeeder) SetVerboseDebug(enabled bool, logger interface{ Debug(msg string, args ...any) })
SetVerboseDebug enables or disables verbose debug logging