Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support of float32 #77

Merged
merged 2 commits into from
Mar 26, 2023
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/obalunenko/getenv.svg)](https://pkg.go.dev/github.com/obalunenko/getenv)
[![Go Report Card](https://goreportcard.com/badge/github.com/obalunenko/getenv)](https://goreportcard.com/report/github.com/obalunenko/getenv)
[![codecov](https://codecov.io/gh/obalunenko/getenv/branch/master/graph/badge.svg)](https://codecov.io/gh/obalunenko/getenv)
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-96.45%25-brightgreen?longCache=true&style=flat)
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-97.03%25-brightgreen?longCache=true&style=flat)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=obalunenko_getenv&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=obalunenko_getenv)

# getenv
Expand Down Expand Up @@ -30,6 +30,7 @@ Types supported:
- []uint
- uint32
- []uint32
- float32
- float64
- []float64
- time.Time
Expand Down
1 change: 1 addition & 0 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// - []uint
// - uint32
// - []uint32
// - float32
// - float64
// - []float64
// - time.Time
Expand Down
76 changes: 76 additions & 0 deletions getenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,82 @@ func TestInt32OrDefault(t *testing.T) {
}
}

func TestFloat32OrDefault(t *testing.T) {
type args struct {
key string
defaultVal float32
}

type expected struct {
val float32
}

var tests = []struct {
name string
precond precondition
args args
expected expected
}{
{
name: "env not set - default returned",
precond: precondition{
setenv: setenv{
isSet: false,
val: "newval",
},
},
args: args{
key: testEnvKey,
defaultVal: float32(956.02),
},
expected: expected{
val: float32(956.02),
},
},
{
name: "env set - env value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "1024.123",
},
},
args: args{
key: testEnvKey,
defaultVal: float32(42),
},
expected: expected{
val: float32(1024.123),
},
},
{
name: "invalid env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "128s7",
},
},
args: args{
key: testEnvKey,
defaultVal: float32(44),
},
expected: expected{
val: float32(44),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.precond.maybeSetEnv(t, tt.args.key)

got := getenv.EnvOrDefault(tt.args.key, tt.args.defaultVal)
assert.Equal(t, tt.expected.val, got)
})
}
}

func TestFloat64OrDefault(t *testing.T) {
type args struct {
key string
Expand Down
2 changes: 1 addition & 1 deletion internal/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type (

// Float is a constraint for floats and slice of floats.
Float interface {
float64 | []float64
float32 | float64 | []float64
}

// Time is a constraint for time.Time and time.Duration.
Expand Down
12 changes: 11 additions & 1 deletion internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewEnvParser(v any) EnvParser {
p = newUintParser(t)
case bool:
p = boolParser(t)
case float64, []float64:
case float32, float64, []float64:
p = newFloatParser(t)
case time.Time:
p = timeParser(t)
Expand Down Expand Up @@ -97,6 +97,8 @@ func newUintParser(v any) EnvParser {

func newFloatParser(v any) EnvParser {
switch t := v.(type) {
case float32:
return float32Parser(t)
case float64:
return float64Parser(t)
case []float64:
Expand Down Expand Up @@ -229,6 +231,14 @@ func (i int64SliceParser) ParseEnv(key string, defaltVal any, options Parameters
return val
}

type float32Parser float32

func (f float32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := float32OrDefault(key, defaltVal.(float32))

return val
}

type float64Parser float64

func (f float64Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
Expand Down
24 changes: 24 additions & 0 deletions internal/iface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ func TestNewEnvParser(t *testing.T) {
want: boolParser(true),
wantPanic: assert.NotPanics,
},
{
name: "float32",
args: args{
v: float32(1.1),
},
want: float32Parser(float32(1.1)),
wantPanic: assert.NotPanics,
},
{
name: "float64",
args: args{
Expand Down Expand Up @@ -316,6 +324,22 @@ func Test_ParseEnv(t *testing.T) {
},
want: -1.2,
},
{
name: "float32Parser",
s: float32Parser(0),
precond: precondition{
setenv: setenv{
isSet: true,
val: "-1.2",
},
},
args: args{
key: testEnvKey,
defaltVal: float32(99),
in2: Parameters{},
},
want: float32(-1.2),
},
{
name: "float64Parser",
s: float64SliceParser(nil),
Expand Down
21 changes: 21 additions & 0 deletions internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,27 @@ func int32OrDefault(key string, defaultVal int32) int32 {
return int32(val)
}

// float32OrDefault retrieves the float32 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
func float32OrDefault(key string, defaultVal float32) float32 {
env := stringOrDefault(key, "")
if env == "" {
return defaultVal
}

const (
bitsize = 32
)

val, err := strconv.ParseFloat(env, bitsize)
if err != nil {
return defaultVal
}

return float32(val)
}

// float64OrDefault retrieves the float64 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
Expand Down
76 changes: 76 additions & 0 deletions internal/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,82 @@ func Test_int32OrDefault(t *testing.T) {
}
}

func Test_float32OrDefault(t *testing.T) {
type args struct {
key string
defaultVal float32
}

type expected struct {
val float32
}

var tests = []struct {
name string
precond precondition
args args
expected expected
}{
{
name: "env not set - default returned",
precond: precondition{
setenv: setenv{
isSet: false,
val: "newval",
},
},
args: args{
key: testEnvKey,
defaultVal: float32(956.02),
},
expected: expected{
val: float32(956.02),
},
},
{
name: "env set - env value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "1024.123",
},
},
args: args{
key: testEnvKey,
defaultVal: float32(42),
},
expected: expected{
val: float32(1024.123),
},
},
{
name: "invalid env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "128s7",
},
},
args: args{
key: testEnvKey,
defaultVal: float32(44),
},
expected: expected{
val: float32(44),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.precond.maybeSetEnv(t, tt.args.key)

got := float32OrDefault(tt.args.key, tt.args.defaultVal)
assert.Equal(t, tt.expected.val, got)
})
}
}

func Test_float64OrDefault(t *testing.T) {
type args struct {
key string
Expand Down