Documentation
¶
Overview ¶
Package conflex provides a flexible configuration package.
Package conflex provides a flexible configuration package.
Package conflex provides a flexible configuration package.
Index ¶
- type ConfigError
- type Conflex
- func (c *Conflex) Dump(ctx context.Context) error
- func (c *Conflex) Get(key string) any
- func (c *Conflex) GetBool(key string) bool
- func (c *Conflex) GetBoolE(key string) (bool, error)
- func (c *Conflex) GetDuration(key string) time.Duration
- func (c *Conflex) GetDurationE(key string) (time.Duration, error)
- func (c *Conflex) GetFloat64(key string) float64
- func (c *Conflex) GetFloat64E(key string) (float64, error)
- func (c *Conflex) GetInt(key string) int
- func (c *Conflex) GetInt32(key string) int32
- func (c *Conflex) GetInt32E(key string) (int32, error)
- func (c *Conflex) GetInt64(key string) int64
- func (c *Conflex) GetInt64E(key string) (int64, error)
- func (c *Conflex) GetIntE(key string) (int, error)
- func (c *Conflex) GetIntSlice(key string) []int
- func (c *Conflex) GetIntSliceE(key string) ([]int, error)
- func (c *Conflex) GetString(key string) string
- func (c *Conflex) GetStringE(key string) (string, error)
- func (c *Conflex) GetStringMap(key string) map[string]any
- func (c *Conflex) GetStringMapE(key string) (map[string]any, error)
- func (c *Conflex) GetStringMapString(key string) map[string]string
- func (c *Conflex) GetStringMapStringE(key string) (map[string]string, error)
- func (c *Conflex) GetStringMapStringSlice(key string) map[string][]string
- func (c *Conflex) GetStringMapStringSliceE(key string) (map[string][]string, error)
- func (c *Conflex) GetStringSlice(key string) []string
- func (c *Conflex) GetStringSliceE(key string) ([]string, error)
- func (c *Conflex) GetTime(key string) time.Time
- func (c *Conflex) GetTimeE(key string) (time.Time, error)
- func (c *Conflex) GetUint(key string) uint
- func (c *Conflex) GetUint8(key string) uint8
- func (c *Conflex) GetUint8E(key string) (uint8, error)
- func (c *Conflex) GetUint16(key string) uint16
- func (c *Conflex) GetUint16E(key string) (uint16, error)
- func (c *Conflex) GetUint32(key string) uint32
- func (c *Conflex) GetUint32E(key string) (uint32, error)
- func (c *Conflex) GetUint64(key string) uint64
- func (c *Conflex) GetUint64E(key string) (uint64, error)
- func (c *Conflex) GetUintE(key string) (uint, error)
- func (c *Conflex) Load(ctx context.Context) error
- func (c *Conflex) Values() *map[string]any
- type Dumper
- type Option
- func WithBinding(v any) Option
- func WithConsulSource(path string, codecType codec.Type) Option
- func WithContentSource(data []byte, codecType codec.Type) Option
- func WithDumper(dumper Dumper) Option
- func WithFileDumper(path string, codecType codec.Type) Option
- func WithFileSource(path string, codecType codec.Type) Option
- func WithJSONSchema(schema []byte) Option
- func WithOSEnvVarSource(prefix string) Option
- func WithSource(loader Source) Option
- func WithValidator(fn func(map[string]any) error) Option
- type Source
- type Validator
- type Watcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConfigError ¶ added in v0.2.0
type ConfigError struct {
Source string // The source where the error occurred (e.g., "source[0]", "json-schema", "binding")
Field string // The specific field where the error occurred (optional)
Operation string // The operation being performed (e.g., "load", "validate", "bind", "merge")
Err error // The underlying error
}
ConfigError represents a configuration error with detailed context. It provides information about where the error occurred (source, field), what operation was being performed, and the underlying error.
func NewConfigError ¶ added in v0.2.0
func NewConfigError(source, operation string, err error) *ConfigError
NewConfigError creates a new ConfigError with the provided context. This is a convenience function for creating ConfigError instances.
func NewConfigFieldError ¶ added in v0.2.0
func NewConfigFieldError(source, field, operation string, err error) *ConfigError
NewConfigFieldError creates a new ConfigError with field information. This is useful when the error is specific to a particular configuration field.
func (*ConfigError) Error ¶ added in v0.2.0
func (e *ConfigError) Error() string
Error returns a formatted error message with context information. If Field is provided, it includes the field in the error message.
func (*ConfigError) Unwrap ¶ added in v0.2.0
func (e *ConfigError) Unwrap() error
Unwrap returns the underlying error, allowing for error chain inspection. This enables the use of errors.Is() and errors.As() with ConfigError.
type Conflex ¶
type Conflex struct {
// contains filtered or unexported fields
}
Conflex is the main struct that holds the configuration data and the sources used to load the data. The values field is a pointer to a map that holds the configuration data. The sources field is a slice of Source instances that are used to load the configuration data. The mu field is a sync.RWMutex that is used to synchronize access to the configuration data.
func New ¶
New creates a new Conflex instance with the provided options. It iterates through the options and applies each one to the Conflex instance. If any of the options return an error, the errors are collected and returned.
func (*Conflex) Get ¶
Get returns the value associated with the given key as an any type. If the key is not found, it returns nil.
func (*Conflex) GetBool ¶
GetBool returns the value associated with the given key as a boolean. If the value is not found or cannot be converted to a boolean, false is returned.
func (*Conflex) GetBoolE ¶
GetBoolE returns the value associated with the given key as a boolean. If the value is not found or cannot be converted to a boolean, it returns an error.
func (*Conflex) GetDuration ¶
GetDuration returns the value associated with the given key as a time.Duration. If the value is not found or cannot be converted to a time.Duration, the zero value is returned.
func (*Conflex) GetDurationE ¶
GetDurationE returns the value associated with the given key as a time.Duration. If the value is not found or cannot be converted to a time.Duration, it returns an error.
func (*Conflex) GetFloat64 ¶
GetFloat64 returns the value associated with the given key as a float64. If the value is not found or cannot be converted to a float64, 0.0 is returned.
func (*Conflex) GetFloat64E ¶
GetFloat64E returns the value associated with the given key as a float64. If the value is not found or cannot be converted to a float64, it returns an error.
func (*Conflex) GetInt ¶
GetInt returns the value associated with the given key as an integer. If the value is not found or cannot be converted to an integer, 0 is returned.
func (*Conflex) GetInt32 ¶
GetInt32 returns the value associated with the given key as an int32. If the value is not found or cannot be converted to an int32, 0 is returned.
func (*Conflex) GetInt32E ¶
GetInt32E returns the value associated with the given key as an int32. If the value is not found or cannot be converted to an int32, it returns an error.
func (*Conflex) GetInt64 ¶
GetInt64 returns the value associated with the given key as an int64. If the value is not found or cannot be converted to an int64, 0 is returned.
func (*Conflex) GetInt64E ¶
GetInt64E returns the value associated with the given key as an int64. If the value is not found or cannot be converted to an int64, it returns an error.
func (*Conflex) GetIntE ¶
GetIntE returns the value associated with the given key as an integer. If the value is not found or cannot be converted to an integer, it returns an error.
func (*Conflex) GetIntSlice ¶
GetIntSlice returns the value associated with the given key as a slice of integers. If the value is not found or cannot be converted to a slice of integers, an empty slice is returned.
func (*Conflex) GetIntSliceE ¶
GetIntSliceE returns the value associated with the given key as a slice of integers. If the value is not found or cannot be converted to a slice of integers, it returns an error.
func (*Conflex) GetString ¶
GetString returns the value associated with the given key as a string. If the value is not found or cannot be converted to a string, an empty string is returned.
func (*Conflex) GetStringE ¶
GetStringE returns the value associated with the given key as a string. If the value is not found or cannot be converted to a string, it returns an error.
func (*Conflex) GetStringMap ¶
GetStringMap returns the value associated with the given key as a map[string]any. If the value is not found or cannot be converted to a map[string]any, the zero value is returned.
func (*Conflex) GetStringMapE ¶
GetStringMapE returns the value associated with the given key as a map[string]any. If the value is not found or cannot be converted to a map[string]any, it returns an error.
func (*Conflex) GetStringMapString ¶
GetStringMapString returns the value associated with the given key as a map[string]string. If the value is not found or cannot be converted to a map[string]string, the zero value is returned.
func (*Conflex) GetStringMapStringE ¶
GetStringMapStringE returns the value associated with the given key as a map[string]string. If the value is not found or cannot be converted to a map[string]string, it returns an error.
func (*Conflex) GetStringMapStringSlice ¶
GetStringMapStringSlice returns the value associated with the given key as a map[string][]string. If the value is not found or cannot be converted to a map[string][]string, the zero value is returned.
func (*Conflex) GetStringMapStringSliceE ¶
GetStringMapStringSliceE returns the value associated with the given key as a map[string][]string. If the value is not found or cannot be converted to a map[string][]string, it returns an error.
func (*Conflex) GetStringSlice ¶
GetStringSlice returns the value associated with the given key as a slice of strings. If the value is not found or cannot be converted to a slice of strings, an empty slice is returned.
func (*Conflex) GetStringSliceE ¶
GetStringSliceE returns the value associated with the given key as a slice of strings. If the value is not found or cannot be converted to a slice of strings, it returns an error.
func (*Conflex) GetTime ¶
GetTime returns the value associated with the given key as a time.Time. If the value is not found or cannot be converted to a time.Time, the zero value is returned.
func (*Conflex) GetTimeE ¶
GetTimeE returns the value associated with the given key as a time.Time. If the value is not found or cannot be converted to a time.Time, it returns an error.
func (*Conflex) GetUint ¶
GetUint returns the value associated with the given key as an uint. If the value is not found or cannot be converted to an uint, 0 is returned.
func (*Conflex) GetUint8 ¶
GetUint8 returns the value associated with the given key as an uint8. If the value is not found or cannot be converted to an uint8, 0 is returned.
func (*Conflex) GetUint8E ¶
GetUint8E returns the value associated with the given key as an uint8. If the value is not found or cannot be converted to an uint8, it returns an error.
func (*Conflex) GetUint16 ¶
GetUint16 returns the value associated with the given key as an uint16. If the value is not found or cannot be converted to an uint16, 0 is returned.
func (*Conflex) GetUint16E ¶
GetUint16E returns the value associated with the given key as an uint16. If the value is not found or cannot be converted to an uint16, it returns an error.
func (*Conflex) GetUint32 ¶
GetUint32 returns the value associated with the given key as an uint32. If the value is not found or cannot be converted to an uint32, 0 is returned.
func (*Conflex) GetUint32E ¶
GetUint32E returns the value associated with the given key as an uint32. If the value is not found or cannot be converted to an uint32, it returns an error.
func (*Conflex) GetUint64 ¶
GetUint64 returns the value associated with the given key as an uint64. If the value is not found or cannot be converted to an uint64, 0 is returned.
func (*Conflex) GetUint64E ¶
GetUint64E returns the value associated with the given key as an uint64. If the value is not found or cannot be converted to an uint64, it returns an error.
func (*Conflex) GetUintE ¶
GetUintE returns the value associated with the given key as an uint. If the value is not found or cannot be converted to an uint, it returns an error.
func (*Conflex) Load ¶
Load loads configuration data from the registered sources and merges it into the internal values map. The method validates the configuration data (including binding validation) before acquiring a write lock to atomically update the values map. If any of the sources fail to load or validate, it returns an error.
type Dumper ¶
Dumper is an interface that defines a method for dumping a map of string to any values.
type Option ¶
Option is a functional option that can be used to configure a Conflex instance.
func WithBinding ¶
WithBinding returns an Option that configures the Conflex instance to bind configuration data to a struct.
func WithConsulSource ¶
WithConsulSource returns an Option that configures the Conflex instance to load configuration data from a Consul server. The path parameter specifies the key path in Consul's key-value store to load configuration from. The codecType parameter specifies the codec type (e.g., JSON, YAML) to use for decoding the configuration data. Required environment variables:
- CONSUL_HTTP_ADDR: The address of the Consul server (e.g., "http://localhost:8500")
- CONSUL_HTTP_TOKEN: The access token for authentication with Consul (optional)
func WithContentSource ¶
WithContentSource returns an Option that configures the Conflex instance to load configuration data from a byte slice.
func WithDumper ¶
WithDumper adds a dumper to the configuration loader.
func WithFileDumper ¶
WithFileDumper returns an Option that configures the Conflex instance to dump configuration data to a file.
func WithFileSource ¶
WithFileSource returns an Option that configures the Conflex instance to load configuration data from a file.
func WithJSONSchema ¶
WithJSONSchema adds a JSON Schema for validation.
func WithOSEnvVarSource ¶
WithOSEnvVarSource returns an Option that configures the Conflex instance to load configuration data from environment variables. The prefix parameter specifies the prefix for the environment variables to be loaded.
func WithSource ¶
WithSource adds a source to the configuration loader.
type Source ¶
type Source interface {
// Load loads configuration data from a source.
Load(ctx context.Context) (map[string]any, error)
}
Source is an interface that defines a method to load configuration data.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package codec provides functionality for encoding and decoding data.
|
Package codec provides functionality for encoding and decoding data. |
|
Package dumper provides functionality for dumping configuration data to a target.
|
Package dumper provides functionality for dumping configuration data to a target. |
|
examples
|
|
|
basic
command
|
|
|
comprehensive
command
|
|
|
environment
command
|
|
|
mixed
command
|
|
|
Package source provides functionality for loading configuration data from various sources.
|
Package source provides functionality for loading configuration data from various sources. |