Skip to content

Commit

Permalink
Supported int8, int16, int32, uint8, uint16, uint32
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Tsarev authored and caarlos0 committed Mar 8, 2019
1 parent 694c6bf commit 7c6118a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
54 changes: 45 additions & 9 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,24 @@ func set(field reflect.Value, refType reflect.StructField, value string, funcMap
return err
}
field.SetInt(intValue)
case reflect.Uint:
uintValue, err := strconv.ParseUint(value, 10, 32)
case reflect.Int8:
intValue, err := strconv.ParseInt(value, 10, 8)
if err != nil {
return err
}
field.SetUint(uintValue)
case reflect.Float32:
v, err := strconv.ParseFloat(value, 32)
field.SetInt(intValue)
case reflect.Int16:
intValue, err := strconv.ParseInt(value, 10, 16)
if err != nil {
return err
}
field.SetFloat(v)
case reflect.Float64:
v, err := strconv.ParseFloat(value, 64)
field.SetInt(intValue)
case reflect.Int32:
intValue, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return err
}
field.Set(reflect.ValueOf(v))
field.SetInt(intValue)
case reflect.Int64:
if refType.Type.String() == "time.Duration" {
dValue, err := time.ParseDuration(value)
Expand All @@ -226,12 +226,48 @@ func set(field reflect.Value, refType reflect.StructField, value string, funcMap
}
field.SetInt(intValue)
}
case reflect.Uint:
uintValue, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return err
}
field.SetUint(uintValue)
case reflect.Uint8:
uintValue, err := strconv.ParseUint(value, 10, 8)
if err != nil {
return err
}
field.SetUint(uintValue)
case reflect.Uint16:
uintValue, err := strconv.ParseUint(value, 10, 16)
if err != nil {
return err
}
field.SetUint(uintValue)
case reflect.Uint32:
uintValue, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return err
}
field.SetUint(uintValue)
case reflect.Uint64:
uintValue, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return err
}
field.SetUint(uintValue)
case reflect.Float32:
v, err := strconv.ParseFloat(value, 32)
if err != nil {
return err
}
field.SetFloat(v)
case reflect.Float64:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
field.Set(reflect.ValueOf(v))
default:
return handleTextUnmarshaler(field, value)
}
Expand Down
27 changes: 23 additions & 4 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ type Config struct {
Some string `env:"somevar"`
Other bool `env:"othervar"`
Port int `env:"PORT"`
Int8Val int8 `env:"INT8VAL"`
Int16Val int16 `env:"INT16VAL"`
Int32Val int32 `env:"INT32VAL"`
Int64Val int64 `env:"INT64VAL"`
UintVal uint `env:"UINTVAL"`
Uint8Val uint8 `env:"UINT8VAL"`
Uint16Val uint16 `env:"UINT16VAL"`
Uint32Val uint32 `env:"UINT32VAL"`
Uint64Val uint64 `env:"UINT64VAL"`
NotAnEnv string
DatabaseURL string `env:"DATABASE_URL" envDefault:"postgres://localhost:5432/db"`
Expand Down Expand Up @@ -90,7 +96,13 @@ func TestParsesEnv(t *testing.T) {
os.Setenv("FLOAT32S", "1.0,2.0,3.0")
os.Setenv("FLOAT64S", "1.0,2.0,3.0")
os.Setenv("UINTVAL", "44")
os.Setenv("UINT8VAL", "88")
os.Setenv("UINT16VAL", "1616")
os.Setenv("UINT32VAL", "3232")
os.Setenv("UINT64VAL", "6464")
os.Setenv("INT8VAL", "-88")
os.Setenv("INT16VAL", "-1616")
os.Setenv("INT32VAL", "-3232")
os.Setenv("INT64VAL", "-7575")
os.Setenv("DURATIONS", "1s,2s,3s")
os.Setenv("UNMARSHALER", "1s")
Expand All @@ -106,8 +118,14 @@ func TestParsesEnv(t *testing.T) {
assert.Equal(t, true, cfg.Other)
assert.Equal(t, 8080, cfg.Port)
assert.Equal(t, uint(44), cfg.UintVal)
assert.Equal(t, int64(-7575), cfg.Int64Val)
assert.Equal(t, uint8(88), cfg.Uint8Val)
assert.Equal(t, uint16(1616), cfg.Uint16Val)
assert.Equal(t, uint32(3232), cfg.Uint32Val)
assert.Equal(t, uint64(6464), cfg.Uint64Val)
assert.Equal(t, int8(-88), cfg.Int8Val)
assert.Equal(t, int16(-1616), cfg.Int16Val)
assert.Equal(t, int32(-3232), cfg.Int32Val)
assert.Equal(t, int64(-7575), cfg.Int64Val)
assert.Equal(t, []string{"string1", "string2", "string3"}, cfg.Strings)
assert.Equal(t, []string{"string1", "string2", "string3"}, cfg.SepStrings)
assert.Equal(t, []int{1, 2, 3, 4}, cfg.Numbers)
Expand Down Expand Up @@ -584,14 +602,15 @@ func TestCustomParserNotCalledForNonAlias(t *testing.T) {
}

func TestCustomParserBasicUnsupported(t *testing.T) {
type ConstT int32
type ConstT struct{
A int
}

type config struct {
Const ConstT `env:"CONST_VAL"`
}

exp := ConstT(123)
os.Setenv("CONST_VAL", fmt.Sprintf("%d", exp))
os.Setenv("CONST_VAL", "42")

cfg := &config{}
err := env.Parse(cfg)
Expand Down

0 comments on commit 7c6118a

Please sign in to comment.