diff --git a/env.go b/env.go index 61ffd2a..084dc12 100644 --- a/env.go +++ b/env.go @@ -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 { diff --git a/env_test.go b/env_test.go index 3d120af..dfd3230 100644 --- a/env_test.go +++ b/env_test.go @@ -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"` @@ -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", @@ -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) }