Documentation
¶
Overview ¶
Package conf is an extensible solution for cascading configuration. Package conf provides configuration processor, that can load configuration layers from different sources and merges them into the one configuration tree. Package conf comes with built-in configuration loaders: fileconf and envconf, and can be extended by third-party configuration loaders. Package conf do not watch for configuration changes, but you can implement this feature in the custom configuration loader. You can find full example in repository.
Configuration processor can expand references, that can be specified in string values, to configuration parameters in the same configuration tree (if you need references to complex structures, see $ref directive).
myapp:
rootDir: "/myapp"
templatesDir: "${myapp.rootDir}/templates"
sessionsDir: "${myapp.rootDir}/sessions"
To escape expansion of references, add one more "$" symbol. For example:
templatesDir: "$${myapp.rootDir}/templates"
Configuration processor supports $ref directive, that can be used to refer to configuration parameters in the same configuration tree. $ref directive can take three forms. In the first form $ref directive just try to get a value by name. In the second form $ref directive tries to get a value by name and if no value is found, uses default value. In the third form $ref directive tries to get a value of a first defined configuration parameter and, if no value is found, uses default value. Default value in second and third forms can be omitted.
db:
stat:
host: { $ref: "db.generic.host" }
port: { $ref: { name: "db.generic.port", default: 5432 } }
dbname: "stat"
username: { $ref: { name: "db.generic.username", default: "some_user" } }
password:
$ref: { firstDefined: [ "MYAPP_DB_STAT_PASS", "db.generic.password" ], default: "some_pass" }
Configuration processor can include additional configuration to main configuration tree from external sources using $include directive. $include directive applies before merge of configuration layers. $include directive accepts a list of configuration locators as argument.
db: { $include: [ "file:connectors.yml" ] }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode method decodes raw configuration data into structure. Note that the conf tags defined in the struct type can indicate which fields the values are mapped to (see the example below). The decoder will make the following conversions:
- bools to string (true = "1", false = "0")
- numbers to string (base 10)
- bools to int/uint (true = 1, false = 0)
- strings to int/uint (base implied by prefix)
- int to bool (true if value != 0)
- string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Anything else is an error)
- empty array = empty map and vice versa
- negative numbers to overflowed uint values (base 10)
- slice of maps to a merged map
- single values are converted to slices if required. Each element also can be converted. For example: "4" can become []int{4} if the target type is an int slice.
Example ¶
type DBConfig struct {
Host string `conf:"server_host"`
Port int `conf:"server_port"`
DBName string
Username string
Password string
}
configRaw := conf.M{
"server_host": "stat.mydb.com",
"server_port": 1234,
"dbname": "stat",
"username": "stat_writer",
"password": "some_pass",
}
var config DBConfig
conf.Decode(configRaw, &config)
fmt.Printf("%v", config)
Output: {stat.mydb.com 1234 stat stat_writer some_pass}
Types ¶
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor loads configuration layers from different sources and merges them into the one configuration tree. In addition configuration processor can expand references on configuration parameters in string values and process $ref and $include directives in resulting configuration tree. Processing can be disabled if not needed.
func NewProcessor ¶
func NewProcessor(config ProcessorConfig) *Processor
NewProcessor method creates new configuration processor instance.
type ProcessorConfig ¶
type ProcessorConfig struct {
// Loaders specifies configuration loaders. Map keys reperesents names of
// configuration loaders, that further can be used in configuration locators.
Loaders map[string]Loader
// DisableProcessing disables expansion of references and processing of
// directives.
DisableProcessing bool
}
ProcessorConfig is a structure with configuration parameters for configuration processor.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package envconf is configuration loader for the conf package.
|
Package envconf is configuration loader for the conf package. |
|
examples
|
|
|
basic
command
|
|
|
Package fileconf is configuration loader for the conf package.
|
Package fileconf is configuration loader for the conf package. |