Documentation
¶
Overview ¶
Package configuration provides a flexible, multi-source configuration system inspired by .NET's IConfiguration pattern. Configuration values can be loaded from environment variables, JSON files, command-line flags, and custom sources, then merged with priority ordering.
Index ¶
- type Builder
- type Configuration
- func (c *Configuration) Bind(target any) error
- func (c *Configuration) Get(key string) (any, bool)
- func (c *Configuration) GetBool(key string) (bool, bool)
- func (c *Configuration) GetInt(key string) (int, bool)
- func (c *Configuration) GetSection(prefix string) *Configuration
- func (c *Configuration) GetString(key string) (string, bool)
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent API for assembling a Configuration from multiple sources with priority ordering (last added wins).
Example:
cfg := configuration.NewBuilder().
AddEnv("APP_").
AddJSONFile("config.json").
Build()
type Configuration ¶
type Configuration struct {
// contains filtered or unexported fields
}
Configuration holds merged configuration data from multiple providers.
Example:
cfg := configuration.NewBuilder().
AddEnv("APP_").
AddJSONFile("config.json").
Build()
val := cfg.Get("db:host")
func (*Configuration) Bind ¶
func (c *Configuration) Bind(target any) error
Bind populates a struct from the configuration data. Fields are matched using the `conf` tag. If no tag is present, the field name is used as key.
Example:
type Config struct {
Host string `conf:"db:host"`
Port int `conf:"db:port" default:"5432"`
}
var cfg Config
err := configuration.Bind(&cfg)
func (*Configuration) Get ¶
func (c *Configuration) Get(key string) (any, bool)
Get returns the value for the given key. The key is case-insensitive. Nested keys can be accessed with colon syntax, e.g. "db:host".
Returns:
The value and true if found, otherwise nil and false.
func (*Configuration) GetBool ¶
func (c *Configuration) GetBool(key string) (bool, bool)
GetBool returns the bool value for the given key.
Returns:
The value and true if the key exists and can be converted to bool.
func (*Configuration) GetInt ¶
func (c *Configuration) GetInt(key string) (int, bool)
GetInt returns the int value for the given key.
Returns:
The value and true if the key exists and can be converted to int.
func (*Configuration) GetSection ¶
func (c *Configuration) GetSection(prefix string) *Configuration
GetSection returns a new Configuration containing only keys with the given prefix.
type Provider ¶
type Provider interface {
// Name returns a human-readable identifier for this source.
Name() string
// Load retrieves configuration key-value pairs.
Load(ctx context.Context) (map[string]any, error)
}
Provider is the interface that configuration sources must implement.
Example:
type MyProvider struct{}
func (p *MyProvider) Name() string { return "my" }
func (p *MyProvider) Load(ctx) (map[string]any, error) { return map[string]any{"key": "val"}, nil }