Skip to content

Commit

Permalink
feat: more pointer types
Browse files Browse the repository at this point in the history
refactor: set default value with Parse func
  • Loading branch information
wrfly committed Mar 22, 2019
1 parent 062a217 commit 3122716
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 29 deletions.
17 changes: 12 additions & 5 deletions ecp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ import (
"strings"
)

// note Parse function will overwrite the existing value if there is a
// environment configration matched with the struct name or the "env" tag
// name.

// Parse the configuration through environments starting with the prefix
// or you can ignore the prefix and the default prefix key will be `ECP`
// ecp.Parse(&config) or ecp.Parse(&config, "PREFIX")
//
// Parse will overwrite the existing value if there is an environment
// configration 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.
func Parse(config interface{}, prefix ...string) error {
if prefix == nil {
prefix = []string{"ECP"}
}
_, err := rangeOver(roOption{config, false, prefix[0], ""})
_, err := rangeOver(roOption{config, true, prefix[0], ""})
return err
}

Expand All @@ -40,6 +46,7 @@ func Parse(config interface{}, prefix ...string) error {
// empty.

// Default set config with its default value
// DEPRECATED: just use `Parse`
func Default(config interface{}) error {
_, err := rangeOver(roOption{config, true, "", ""})
return err
Expand Down
23 changes: 20 additions & 3 deletions ecp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ type configType struct {
NilInt64 *int64 `default:"64"`

NilUInt *uint `default:"1"`
NilUInt8 *uint8 `default:"8"`
NilUInt16 *uint16 `default:"16"`
NilUInt8 *uint8 `default:"88"`
NilUInt16 *uint16 `default:"168"`
NilUInt32 *uint32 `default:"32"`
NilUInt64 *uint64 `default:"64"`

PointerFloat *float32 `default:"1.2"`
PointerFloat64 *float64 `default:"4.3"`

Empty string `json:"empty"`

// ignore unexposed fields
x struct {
X int
Expand Down Expand Up @@ -149,6 +154,10 @@ func TestParse(t *testing.T) {
LogLevel: "debug",
Port: 999,
}

six := int8(6)
config.NilInt8 = &six

if err := Parse(&config); err != nil {
t.Error(err)
}
Expand All @@ -162,12 +171,18 @@ func TestParse(t *testing.T) {
if config.NilInt8 == nil {
t.Error("parse pointer failed")
}
if *config.NilInt8 != 8 {

if *config.NilInt8 != 6 {
t.Error("???")
}

if *config.NilInt16 != 16 {
t.Error("???")
}

if *config.PointerFloat64 != 4.3 {
t.Error("???")
}
}

func TestDefault(t *testing.T) {
Expand All @@ -193,6 +208,8 @@ func TestDefault(t *testing.T) {
case config.Sub.F32 != 3.14:
case config.SubStruct.Int != 111:
case *config.Nil != "":
case *config.NilInt64 != 64:
case *config.NilInt8 != 8:
default:
passed = true
}
Expand Down
51 changes: 33 additions & 18 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,65 +183,80 @@ func parseSlice(v string, field reflect.Value) error {
return nil
}

func parsePointer(typeString, defaultV string) (interface{}, error) {
var defaultValue interface{}
func parsePointer(typeString, value string) (interface{}, error) {
var rValue interface{}
switch typeString {
case reflect.String.String():
defaultValue = &defaultV
rValue = &value

case reflect.Int.String(), reflect.Int8.String(), reflect.Int16.String(),
reflect.Int32.String(), reflect.Int64.String():
vInt, err := strconv.ParseInt(defaultV, 10, 64)
vInt, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, err
}
switch typeString {
case reflect.Int.String():
parsed := int(vInt)
defaultValue = &parsed
rValue = &parsed
case reflect.Int8.String():
parsed := int8(vInt)
defaultValue = &parsed
rValue = &parsed
case reflect.Int16.String():
parsed := int16(vInt)
defaultValue = &parsed
rValue = &parsed
case reflect.Int32.String():
parsed := int32(vInt)
defaultValue = &parsed
rValue = &parsed
case reflect.Int64.String():
defaultValue = &vInt
rValue = &vInt
}

case reflect.Uint.String(), reflect.Uint8.String(), reflect.Uint16.String(),
reflect.Uint32.String(), reflect.Uint64.String():
v, err := strconv.ParseUint(defaultV, 10, 64)
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return nil, err
}
switch typeString {
case reflect.Uint.String():
parsed := uint(v)
defaultValue = &parsed
rValue = &parsed
case reflect.Uint8.String():
parsed := uint8(v)
defaultValue = &parsed
rValue = &parsed
case reflect.Uint16.String():
parsed := uint16(v)
defaultValue = &parsed
rValue = &parsed
case reflect.Uint32.String():
parsed := uint32(v)
defaultValue = &parsed
rValue = &parsed
case reflect.Uint64.String():
defaultValue = &v
rValue = &v
}

case reflect.Bool.String():
if b, err := strconv.ParseBool(strings.ToLower(defaultV)); err == nil {
defaultValue = &b
if b, err := strconv.ParseBool(strings.ToLower(value)); err == nil {
rValue = &b
} else {
return nil, err
}

case reflect.Float32.String():
v, err := strconv.ParseFloat(value, 32)
if err != nil {
return nil, err
}
x := float32(v)
rValue = &x

case reflect.Float64.String():
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, err
}
rValue = &v
}

return defaultValue, nil
return rValue, nil
}
7 changes: 4 additions & 3 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,17 @@ func rangeOver(opts roOption) (reflect.Value, error) {

case reflect.Ptr:
// only set default value to nil pointer
if field.Pointer() != 0 {
if !field.IsNil() {
continue
}
typeString := field.Type().String()[1:]
value, err := parsePointer(typeString, defaultV)
if err != nil {
return field, fmt.Errorf("convert %s error: %s", keyName, err)
}

field.Set(reflect.ValueOf(value))
if value != nil {
field.Set(reflect.ValueOf(value))
}
}

}
Expand Down

0 comments on commit 3122716

Please sign in to comment.