Skip to content

Commit

Permalink
chore: remove Default func and ECP prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
wrfly committed Jun 11, 2021
1 parent f3c8032 commit 78d8753
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 70 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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=
```
32 changes: 7 additions & 25 deletions ecp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand All @@ -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"`
Expand All @@ -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...)
}
72 changes: 30 additions & 42 deletions ecp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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("???")
}
}
Expand All @@ -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=
}

0 comments on commit 78d8753

Please sign in to comment.