Documentation
¶
Overview ¶
Package source provides configuration source implementations.
The source package implements the [Source] interface defined in the parent config package. Sources load configuration data from various locations such as files, environment variables, and remote services.
Available Sources ¶
- File: Load configuration from files with various formats
- OSEnvVar: Load configuration from environment variables
- Consul: Load configuration from Consul key-value store
Example ¶
Creating a file source:
decoder, _ := codec.GetDecoder(codec.TypeYAML)
fileSource := source.NewFile("config.yaml", decoder)
config, err := fileSource.Load(context.Background())
Creating an environment variable source:
envSource := source.NewOSEnvVar("APP_")
config, err := envSource.Load(context.Background())
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Consul ¶
type Consul struct {
// contains filtered or unexported fields
}
Consul represents a configuration source that loads data from Consul's key-value store. It supports both regular codecs (JSON, YAML, TOML) and caster codecs for individual values.
The Consul client is configured using environment variables:
- CONSUL_HTTP_ADDR: The address of the Consul server (e.g., "http://localhost:8500")
- CONSUL_HTTP_TOKEN: The access token for authentication (optional)
func NewConsul ¶
NewConsul creates a new Consul configuration source with the given path and decoder. The path parameter specifies the key path in Consul's key-value store. If kv is nil, it uses the default Consul client KV implementation.
The decoder determines how the retrieved value is parsed:
- Regular decoders (JSON, YAML, TOML): Parse structured configuration data
- Caster decoders: Convert single values to specific types
Errors:
- Returns error if the Consul client cannot be created
func (*Consul) Load ¶
Load retrieves configuration data from the Consul key-value store at the configured path. For regular decoders, it returns the decoded configuration structure. For caster decoders, it returns a map with the key name extracted from the path.
If the key does not exist in Consul, it returns an empty map without error.
Errors:
- Returns error if the Consul query fails
- Returns error if decoding the value fails
type ConsulKV ¶
type ConsulKV interface {
Get(key string, q *api.QueryOptions) (*api.KVPair, *api.QueryMeta, error)
}
ConsulKV defines the interface for Consul key-value operations. This interface enables testing by allowing mock implementations.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents a configuration source that loads data from a file or byte content. It supports loading from file paths or directly from byte slices.
func NewFile ¶
NewFile creates a new File source that loads configuration from the specified file path. The decoder parameter determines how the file content is parsed.
func NewFileContent ¶
NewFileContent creates a new File source that loads configuration from the provided byte slice. This is useful for loading configuration from embedded content or dynamically generated data.
func (*File) Load ¶
Load reads the configuration file and decodes its contents into a map[string]any. If the File was created with NewFile, it reads from the file system. If the File was created with NewFileContent, it uses the provided byte content.
Errors:
- Returns error if the file cannot be read (NewFile only)
- Returns error if decoding fails
type OSEnvVar ¶
type OSEnvVar struct {
// contains filtered or unexported fields
}
OSEnvVar represents a configuration source that loads data from environment variables. It filters environment variables by prefix and creates nested configuration structures based on underscore-separated variable names.
For example, with prefix "APP_", the environment variable "APP_SERVER_PORT" becomes the configuration key "server.port".
func NewOSEnvVar ¶
NewOSEnvVar creates a new OSEnvVar source with the specified prefix. Only environment variables starting with this prefix will be loaded. The prefix is stripped from variable names before processing.
func (*OSEnvVar) Load ¶
Load reads environment variables with the configured prefix and decodes them into a map[string]any. Variable names are converted to lowercase and underscores create nested structures.
Example:
APP_SERVER_PORT=8080 -> server.port = "8080" APP_SERVER_HOST=localhost -> server.host = "localhost" APP_DEBUG=true -> debug = "true"
Errors:
- Returns error if decoding fails