Documentation ¶
Overview ¶
Package ecp can help you convert environments into configurations it's an environment config parser
Index ¶
- func Get(config interface{}, keyName string) (interface{}, error)
- func GetBool(config interface{}, keyName string) (bool, error)
- func GetFloat64(config interface{}, keyName string) (float64, error)
- func GetInt64(config interface{}, keyName string) (int64, error)
- func GetString(config interface{}, keyName string) (string, error)
- func List(config interface{}, prefix ...string) []string
- func New() *ecp
- func Parse(config interface{}, prefix ...string) error
- type BuildKeyFunc
- type LookupValueFunc
- type SetValueFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFloat64 ¶
GetFloat64 returns float64
func List ¶
List all the config environments
Example ¶
type config struct { Age int Name string } for _, key := range List(config{}) { fmt.Printf("env %s", key) } // env AGE= // env NAME=
Output:
func Parse ¶
Parse the configuration through environments starting with the prefix (or not) ecp.Parse(&config) or ecp.Parse(&config, "PREFIX")
Parse will overwrite the existing value if there is an environment configuration matched with the struct name or the "env" tag name.
Also, Parse will set the default value to the config, if it's not set values. For basic types, if the value is zero value, then it will be set to the default value. You can change the basic type to a pointer type, thus Parse will only set the default value when the field is nil, not the zero value. for example:
type config struct { One string `default:"1"` Two int `default:"2"` Three []string `default:"1,2,3"` } c := &config{}
Example ¶
type config struct { Age int `default:"18"` Name string `default:"wrfly"` Duration time.Duration `default:"10d"` } c := &config{} if err := Parse(&c); err != nil { panic(err) } os.Setenv("AGE", "10") if err := Parse(&c); err != nil { panic(err) } // now you'll get a config with // `Age=10` and `Name=wrfly` if c.Age != 10 || c.Name != "wrfly" || c.Duration != time.Hour*24*10 { panic("???") }
Output:
Types ¶
type BuildKeyFunc ¶ added in v0.2.0
BuildKeyFunc how to get the key name
type LookupValueFunc ¶ added in v0.1.3
LookupValueFunc returns the value and whether exist