Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ func set(t reflect.Type, f reflect.Value, value string) error {
break
}

v, err := strconv.Atoi(value)
// Using ParseInt because strconv.Atoi returns int,
// which is not guaranteed to be 64-bit on all platforms
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
f.SetInt(int64(v))
f.SetInt(v)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ValidStruct struct {
Uint uint `env:"UINT"`
Float32 float32 `env:"FLOAT32"`
Float64 float64 `env:"FLOAT64"`
Int64 int64 `env:"INT64"`
Bool bool `env:"BOOL"`

MultipleTags string `env:"npm_config_cache,NPM_CONFIG_CACHE"`
Expand Down Expand Up @@ -146,6 +147,7 @@ func TestUnmarshal(t *testing.T) {
"UINT": "4294967295",
"FLOAT32": "2.3",
"FLOAT64": "4.5",
"INT64": "4294967296",
"BOOL": "true",
"npm_config_cache": "first",
"NPM_CONFIG_CACHE": "second",
Expand Down Expand Up @@ -190,6 +192,10 @@ func TestUnmarshal(t *testing.T) {
t.Errorf("Expected field value to be '%f' but got '%f'", 4.5, validStruct.Float64)
}

if validStruct.Int64 != 4294967296 {
t.Errorf("Expected field value to be '%d' but got '%d'", int64(4294967296), validStruct.Int64)
}

if validStruct.Bool != true {
t.Errorf("Expected field value to be '%t' but got '%t'", true, validStruct.Bool)
}
Expand Down