Documentation ¶
Overview ¶
Package config provides a config manager for any type that implements the config.Interface.
Example ¶
This example demonstrate the basics of the config interface. For more details check the documentation.
package main import ( "github.com/patrickascher/gofw/config/json" "github.com/patrickascher/gofw/config" ) func main() { // import the provider package // import _ "github.com/patrickascher/gofw/config/json" // Cfg is the configuration struct which should get marshaled. type Cfg struct { Database string Port int } // define the config variable cfg := Cfg{} // SetEnv can be used to set a environment variable. By default the os.Env("ENV") will be used. config.SetEnv("staging") // New calls the given config provider and passes the struct, env and options. // cfg must be a pointer, otherwise the value can not be set. err := config.New(config.JSON, &cfg, json.Options{Filepath: "config/app.json"}) if err != nil { return } // The value of the cfg variable will be set. // Cfg{Database:"127.0.0.1",Port:3306} }
Output:
Index ¶
Examples ¶
Constants ¶
const ( // JSON pre-defined config provider. JSON = "json" // ENV is the default name to check the system environment variable os.GetEnv(). ENV = "ENV" )
const ( CallbackBeforeParse = "BeforeParse" CallbackAfterParse = "AfterParse" CallbackBeforeValid = "BeforeValid" CallbackAfterValid = "AfterValid" )
Variables ¶
var ( ErrNoProvider = errors.New("config: empty config-name or config-provider is nil") ErrUnknownProvider = errors.New("config: unknown config-provider %q") ErrProviderAlreadyExists = errors.New("config: config-provider %#v is already registered") ErrConfigPtr = errors.New("config: struct must be a ptr") )
Error messages.
Functions ¶
func New ¶
New will call the parse function on the given provider. The config must be a ptr to the given struct. Options and environment are passed through to the provider. For more information check the provider documentation. If no specific environment was set by SetEnv() before, the os.Env("ENV") will be used. If the provider is not registered, the parsing fails or the cfg kind is not ptr, an error will return. By default validate can be used on the struct to ensure all mandatory data is set. Callbacks BeforeParse, BeforeValid, AfterValid, AfterParse can be used.