From 78d8753b4bffb6f39b86460b2ccd1285eaa0d9c2 Mon Sep 17 00:00:00 2001 From: wrfly Date: Sat, 12 Jun 2021 00:14:11 +0800 Subject: [PATCH] chore: remove Default func and ECP prefix --- README.md | 6 ++--- ecp.go | 32 ++++++------------------ ecp_test.go | 72 ++++++++++++++++++++++------------------------------- 3 files changed, 40 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index d57fbce..9bcdac4 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ func main() { // set some env envs := map[string]string{ - "ECP_LOGLEVEL": "info", + "LOGLEVEL": "info", "PORT": "1234", } for k, v := range envs { @@ -73,11 +73,11 @@ Outputs: ```txt default log level: [ debug ] -export ECP_LOGLEVEL=info +export LOGLEVEL=info export PORT=1234 new log level: [ info ], port: [ 1234 ] -ECP_LOGLEVEL=debug +LOGLEVEL=debug PORT= ``` \ No newline at end of file diff --git a/ecp.go b/ecp.go index 8c347a4..20b0a7f 100644 --- a/ecp.go +++ b/ecp.go @@ -38,22 +38,17 @@ func New() *ecp { func (e *ecp) Parse(config interface{}, prefix ...string) error { if prefix == nil { - prefix = []string{"ECP"} + prefix = []string{""} } _, err := e.rangeOver(roOption{config, true, prefix[0], ""}) return err } -func (e *ecp) Default(config interface{}) error { - _, err := e.rangeOver(roOption{config, true, "", ""}) - return err -} - func (e *ecp) List(config interface{}, prefix ...string) []string { list := []string{} if prefix == nil { - prefix = []string{"ECP"} + prefix = []string{""} } parentName := prefix[0] @@ -87,8 +82,8 @@ func List(config interface{}, prefix ...string) []string { return globalEcp.List(config, prefix...) } -// Parse the configuration through environments starting with the prefix -// or you can ignore the prefix and the default prefix key will be `ECP` +// 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 @@ -100,12 +95,7 @@ func List(config interface{}, prefix ...string) []string { // 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. -func Parse(config interface{}, prefix ...string) error { - return globalEcp.Parse(config, prefix...) -} - -// the default value of the config is set by a tag named "default" -// for example, you can define a struct like: +// for example: // // type config struct { // One string `default:"1"` @@ -114,14 +104,6 @@ func Parse(config interface{}, prefix ...string) error { // } // c := &config{} // -// then you can use ecp.Default(&c) to parse the default value to the struct. -// note, the Default function will not overwrite the existing value, if the -// config key has already been set no matter whether it has a default tag. -// And the default value will be nil (nil of the type) if the "default" tag is -// empty. - -// Default set config with its default value -// DEPRECATED: just use `Parse` -func Default(config interface{}) error { - return globalEcp.Default(config) +func Parse(config interface{}, prefix ...string) error { + return globalEcp.Parse(config, prefix...) } diff --git a/ecp_test.go b/ecp_test.go index 237edd0..588390f 100644 --- a/ecp_test.go +++ b/ecp_test.go @@ -138,18 +138,18 @@ func TestList(t *testing.T) { func TestParse(t *testing.T) { ENV := map[string]string{ - "INT_SLICE": "1 2 3", - "ECP_SUB_INT": "-2333", - "ECP_SUB_BOOL": "true", - "STRING_SLICE": "a b c", - "ECP_SUB_UINT": "123456789", - "ECP_SUB_INT64": "6666", - "ECP_LOG-LEVEL": "info", - "ECP_SUB_DURATION": "10s", - "ECP_NILINT": "2", - "ECP_NILINT8": "9", - "ECP_NILINT16": "17", - // "ECP_P_VALUE": "yoo", + "INT_SLICE": "1 2 3", + "SUB_INT": "-2333", + "SUB_BOOL": "true", + "STRING_SLICE": "a b c", + "SUB_UINT": "123456789", + "SUB_INT64": "6666", + "LOG-LEVEL": "info", + "SUB_DURATION": "10s", + "NILINT": "2", + "NILINT8": "9", + "NILINT16": "17", + // "P_VALUE": "yoo", } for k, v := range ENV { @@ -199,7 +199,7 @@ func TestParse(t *testing.T) { // } } -func TestDefault(t *testing.T) { +func TestParseDefault(t *testing.T) { empty := "" _int8 := int8(8) _bool := true @@ -211,7 +211,7 @@ func TestDefault(t *testing.T) { NilInt8: &_int8, NilBool: &_bool, } - if err := Default(&config); err != nil { + if err := Parse(&config); err != nil { t.Errorf("set default error: %s", err) } @@ -237,7 +237,7 @@ func TestDefault(t *testing.T) { // test pointers config.Nil = nil config.NilBool = nil - if err := Default(&config); err != nil { + if err := Parse(&config); err != nil { t.Errorf("set default error: %s", err) } if config.Nil == nil { @@ -299,7 +299,7 @@ func TestGetKeyLookupValue(t *testing.T) { func TestIgnoreFunc(t *testing.T) { config1 := configType{} - if err := Default(&config1); err != nil { + if err := Parse(&config1); err != nil { t.Error(err) } @@ -313,7 +313,7 @@ func TestIgnoreFunc(t *testing.T) { } return true } - if err := Default(&config2); err != nil { + if err := Parse(&config2); err != nil { t.Fatal(err) } @@ -328,17 +328,23 @@ func TestIgnoreFunc(t *testing.T) { func ExampleParse() { type config struct { - Age int - Name string + Age int `default:"18"` + Name string `default:"wrfly"` + Duration time.Duration `default:"10d"` } c := &config{} - os.Setenv("ECP_AGE", "10") if err := Parse(&c); err != nil { panic(err) } - // c.Age=10 - if c.Age != 10 { + 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("???") } } @@ -352,24 +358,6 @@ func ExampleList() { fmt.Printf("env %s", key) } - // env ECP_AGE= - // env ECP_NAME= -} - -func ExampleDefault() { - type config struct { - Age int `default:"10"` - Name string `default:"wrfly"` - Duration time.Duration `default:"10d"` - } - c := &config{} - if err := Default(&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("???") - } + // env AGE= + // env NAME= }