Documentation ¶
Overview ¶
Package confkey looks for tags on a structure and set values based on the tag rather than the struct item names
Features ¶
Defaults are supported and can be fetched from the shell environment ¶
The tags can specify some formating like comma splits and other commonly seen patterns in config files.
Conversion of []string, ints, strings, time.Duration and booleans are support
Validations can be done on a struct basis using the github.com/choria-io/go-validators package
A sample structure might look like this, the package contains utilities to set values, apply defaults and perform validations
type Config struct { Loglevel string `confkey:"loglevel" default:"warn" validate:"enum=debug,info,warn,error"` Mode string `confkey:"mode" default:"server" validate:"enum=server,client"` Servers []string `confkey:"servers" type:"comma_split" environment:"SERVERS"` Path []string `confkey:"path" type:"path_split" default:"/bin:/usr/bin"` I time.Duration `confkey:"interval" type:"duration" default:"1h"` }
The utilities here will let you parse any config file that might have keys like loglevel etc and map the string values read from the text file onto the structure
Example (Basic) ¶
package main import ( "fmt" "os" "strings" confkey "github.com/choria-io/go-confkey" ) type Config struct { Loglevel string `confkey:"loglevel" default:"warn" validate:"enum=debug,info,warn,error"` Mode string `confkey:"mode" default:"server" validate:"enum=server,client"` Servers []string `confkey:"servers" type:"comma_split" environment:"SERVERS"` Path []string `confkey:"path" type:"path_split" default:"/bin:/usr/bin"` // can also be colon_split to always split on : } func main() { c := &Config{} err := confkey.SetStructDefaults(c) if err != nil { panic(err) } fmt.Println("Defaults:") fmt.Printf(" loglevel: %s\n", c.Loglevel) fmt.Printf(" mode: %s\n", c.Mode) fmt.Printf(" path: %s\n", strings.Join(c.Path, ",")) fmt.Println("") // here you would read your config file, but lets just fake it // and set specific values // every call to SetStructFieldWithKey validates what gets set err = confkey.SetStructFieldWithKey(c, "loglevel", "error") if err != nil { panic(err) } err = confkey.SetStructFieldWithKey(c, "mode", "client") if err != nil { panic(err) } // even though we are setting it, if the ENV is set it overrides os.Setenv("SERVERS", "s1:1024, s2:1024") err = confkey.SetStructFieldWithKey(c, "servers", "s:1024") if err != nil { panic(err) } fmt.Println("Loaded:") fmt.Printf(" loglevel: %s\n", c.Loglevel) fmt.Printf(" mode: %s\n", c.Mode) fmt.Printf(" servers: %s\n", strings.Join(c.Servers, ",")) fmt.Println("") // getting a string by name fmt.Println("Retrieved:") fmt.Printf(" loglevel: %s\n", confkey.StringFieldWithKey(c, "loglevel")) fmt.Printf(" servers: %s\n", strings.Join(confkey.StringListWithKey(c, "servers"), ",")) fmt.Println("") // but you can also validate the entire struct if you like, perhaps you // set some stuff directly to its fields err = confkey.Validate(c) if err != nil { fmt.Printf("invalid: %s\n", err) panic(err) } fmt.Println("valid") // setting a specific bad value yields an error err = confkey.SetStructFieldWithKey(c, "loglevel", "fail") if err != nil { fmt.Printf("invalid: %s\n", err) } }
Output: Defaults: loglevel: warn mode: server path: /bin,/usr/bin Loaded: loglevel: error mode: client servers: s1:1024,s2:1024 Retrieved: loglevel: error servers: s1:1024,s2:1024 valid invalid: Loglevel enum validation failed: 'fail' is not in the allowed list: debug, info, warn, error
Index ¶
- func BoolWithKey(target interface{}, key string) bool
- func Int64WithKey(target interface{}, key string) int64
- func IntWithKey(target interface{}, key string) int
- func SetStructDefaults(target interface{}) error
- func SetStructFieldWithKey(target interface{}, key string, value interface{}) error
- func StringFieldWithKey(target interface{}, key string) string
- func StringListWithKey(target interface{}, key string) []string
- func Validate(target interface{}) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolWithKey ¶
BoolWithKey retrieves a bool from target that matches key, false when not found
func Int64WithKey ¶
Int64WithKey retrieves an int from target that matches key, 0 when not found
func IntWithKey ¶
IntWithKey retrieves an int from target that matches key, 0 when not found
func SetStructDefaults ¶
func SetStructDefaults(target interface{}) error
SetStructDefaults extract defaults out of the tags and set them to the key
func SetStructFieldWithKey ¶
SetStructFieldWithKey finds the struct key that matches the confkey on target and assign the value to it
func StringFieldWithKey ¶
StringFieldWithKey retrieves a string from target that matches key, "" when not found
func StringListWithKey ¶
StringListWithKey retrieves a []string from target that matches key, empty when not found
Types ¶
This section is empty.