Skip to content

Commit

Permalink
Add support for conversion between numeric types
Browse files Browse the repository at this point in the history
  • Loading branch information
sonmezonur committed Nov 6, 2019
1 parent ca12549 commit 514fee3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,19 @@ func setDstElem(dst interface{}, field *structs.Field, fVal string) error {
} else {
dstElem.SetBool(p)
}
case "int":
case "int", "int8", "int16", "int32", "int64":
if p, err := strconv.ParseInt(fVal, 10, 0); err != nil {
return err
} else {
dstElem.SetInt(p)
}
case "uint":
case "uint", "uint8", "uint16", "uint32", "uint64", "uintptr":
if p, err := strconv.ParseUint(fVal, 10, 0); err != nil {
return err
} else {
dstElem.SetUint(p)
}
case "float64":
case "float64", "float32":
if p, err := strconv.ParseFloat(fVal, 64); err != nil {
return err
} else {
Expand Down
71 changes: 71 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ func TestLoad_ErrorIfFlagTypeMismatch(t *testing.T) {
}

tmp, _ := ioutil.TempFile("", "")
defer os.Remove(tmp.Name())

// flag
fs := flag.NewFlagSet("tmp", flag.ExitOnError)
Expand All @@ -489,6 +490,7 @@ func TestLoad_ErrorIfEnvTypeMismatch(t *testing.T) {
}

tmp, _ := ioutil.TempFile("", "")
defer os.Remove(tmp.Name())

// env
os.Setenv("key_float", "key_float_env")
Expand All @@ -497,3 +499,72 @@ func TestLoad_ErrorIfEnvTypeMismatch(t *testing.T) {
t.Fatalf("expected error, got nil")
}
}

func TestLoad_CheckNumericTypes(t *testing.T) {
var cfg struct {
Float32 float32 `flag:"float32"`
Int8 int8 `toml:"int8"`
Int16 int16 `env:"int16"`
Uint32 uint32 `toml:"uint32"`
Uint64 uint64 `env:"uint64"`
UintPtr uintptr `env:"uintptr"`
Bool bool `flag:"bool"`
}

tmp, _ := ioutil.TempFile("", "")
defer os.Remove(tmp.Name())

_, err := tmp.WriteString(`
int8 = -2
uint32 = 1
`)
if err != nil {
t.Fatalf("write config file failed: %v", err)
}

// flag
fs := flag.NewFlagSet("tmp", flag.ExitOnError)
_ = fs.Bool("bool", false, "")
_ = fs.Float64("float32", 0.0, "")

flag.CommandLine = fs
flag.CommandLine.Parse([]string{"-bool", "true"}) // flag given
flag.CommandLine.Parse([]string{"-float32", "1.3"}) // flag given

// env
os.Setenv("uint64", "100000000000")
os.Setenv("uintptr", "20")
os.Setenv("int16", "3")

if err := Load(tmp.Name(), &cfg); err != nil {
t.Fatalf("unexpected error %v", err)
}

if cfg.Float32 != 1.3 {
t.Errorf("got: %v, expected: %v", cfg.Float32, 1.3)
}

if cfg.Int8 != -2 {
t.Errorf("got: %v, expected: %v", cfg.Int8, -2)
}

if cfg.Int16 != 3 {
t.Errorf("got: %v, expected: %v", cfg.Int16, 3)
}

if cfg.Uint32 != 1 {
t.Errorf("got: %v, expected: %v", cfg.Uint32, 1)
}

if cfg.Uint64 != 100000000000 {
t.Errorf("got: %v, expected: %v", cfg.Uint64, 100000000000)
}

if cfg.UintPtr != 20 {
t.Errorf("got: %v, expected: %v", cfg.UintPtr, 20)
}

if cfg.Bool != true {
t.Errorf("got: %v, expected: %v", cfg.Bool, true)
}
}

0 comments on commit 514fee3

Please sign in to comment.