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. In addition configuration processor can expand references on configuration parameters in string values, and process _ref and _include directives in resulting configuration tree (see below). 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 on configuration parameters in string values (if you need reference on complex structures see _ref directive). Reference names can be absolute or relative. Relative reference names begins with "." (dot). The section, in which a value of relative reference will be searched, determines by number of dots in reference name. For example, we have a YAML file:
myapp:
mediaFormats: ["images", "audio", "video"]
dirs:
rootDir: "/myapp"
templatesDir: "${myapp.dirs.rootDir}/templates"
sessionsDir: "${.rootDir}/sessions"
mediaDirs:
- "${..rootDir}/media/${myapp.mediaFormats.0}"
- "${..rootDir}/media/${myapp.mediaFormats.1}"
- "${..rootDir}/media/${myapp.mediaFormats.2}"
After processing of the file we will get a map:
"myapp": conf.M{
"mediaFormats": conf.S{"images", "audio", "video"},
"dirs": conf.M{
"rootDir": "/myapp",
"templatesDir": "/myapp/templates",
"sessionsDir": "/myapp/sessions",
"mediaDirs": conf.S{
"/myapp/media/images",
"/myapp/media/audio",
"/myapp/media/video",
},
},
}
To escape expansion of reference, add one more "$" symbol before reference name.
templatesDir: "$${myapp.dirs.rootDir}/templates"
After processing we will get:
templatesDir: "${myapp.dirs.rootDir}/templates"
Package conf supports special directives in configuration layers: _ref and _include. _ref directive retrieves a value by reference on configuration parameter and assigns this value to another configuration parameter. _ref directive can take three forms:
_ref: <name>
_ref: {name: <name>, default: <value>}
_ref: {firstDefined: [<name1>, ...], default: <value>}
In the first form _ref directive just assings a value retrieved by reference. In the second form _ref directive tries to retrieve a value by reference and, if no value retrieved, assigns default value. And in the third form _ref directive tries to retrive a value from the first defined reference and, if no value retrieved, assigns default value. Default value in second and third forms can be omitted. Reference names in _ref directive can be relative or absolute.
db:
defaultOptions:
serverPrepare: true
expandArray: true
errorLevel: 2
connectors:
stat:
host: "stat.mydb.com"
port: 1234
dbname: "stat"
username: "stat"
password:
_ref: {name: "MYAPP_DB_STAT_PASSWORD", default: "stat_pass"}
options: {_ref: "db.defaultOptions"}
metrics:
host: "metrics.mydb.com"
port: 1234
dbname: "metrics"
username: "metrics"
password:
_ref: {name: "MYAPP_DB_METRICS_PASSWORD", default: "metrics_pass"}
options: {_ref: "...defaultOptions"}
_include directive loads configuration layer from external sources and inserts it to configuration tree. _include directive accepts as argument a list of configuration locators.
db:
defaultOptions:
serverPrepare: true
expandArray: true
errorLevel: 2
connectors: {_include: ["file:connectors.yml"]}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶ added in v1.4.1
func Decode(configRaw, config interface{}) error
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 ¶
package main
import (
"fmt"
"github.com/iph0/conf"
)
func main() {
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 Locator ¶ added in v1.2.1
Locator is used by configuration processor and loaders to load configuration layers.
func ParseLocator ¶ added in v1.2.1
ParseLocator method creates Locator instance from the string.
type M ¶ added in v1.4.1
type M = map[string]interface{}
M type is a convenient alias for a map[string]interface{} map.
type Processor ¶ added in v1.2.1
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 ¶ added in v1.2.1
func NewProcessor(config ProcessorConfig) *Processor
NewProcessor method creates new configuration processor instance.
func (*Processor) Load ¶ added in v1.2.1
Load method loads configuration tree using configuration locators. Configuration locator can be a string or a map of type map[string]interface{}. Map type can be used to specify default configuration layers. The merge priority of loaded configuration layers depends on the order of configuration locators. Layers loaded by rightmost locator have highest priority.
type ProcessorConfig ¶ added in v1.2.1
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. |