Skip to content

Commit

Permalink
feat: Add support of uint16 (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko authored Mar 26, 2023
1 parent 5b6e62a commit 607a9e0
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Types supported:
- int64
- []int64
- uint8
- uint16
- uint64
- []uint64
- uint
Expand Down
1 change: 1 addition & 0 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// - int64
// - []int64
// - uint8
// - uint16
// - uint64
// - []uint64
// - uint
Expand Down
76 changes: 76 additions & 0 deletions getenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,82 @@ func TestUint8OrDefault(t *testing.T) {
}
}

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

type expected struct {
val uint16
}

var tests = []struct {
name string
precond precondition
args args
expected expected
}{
{
name: "env not set - default returned",
precond: precondition{
setenv: setenv{
isSet: false,
val: "12",
},
},
args: args{
key: testEnvKey,
defaultVal: 99,
},
expected: expected{
val: 99,
},
},
{
name: "env set - env value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "12",
},
},
args: args{
key: testEnvKey,
defaultVal: 99,
},
expected: expected{
val: 12,
},
},
{
name: "empty env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "",
},
},
args: args{
key: testEnvKey,
defaultVal: 99,
},
expected: expected{
val: 99,
},
},
}

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 TestUint64OrDefault(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 @@ -22,7 +22,7 @@ type (

// Uint is a constraint for unsigned integer and slice of unsigned integers.
Uint interface {
uint | []uint | uint8 | uint32 | []uint32 | uint64 | []uint64
uint | []uint | uint8 | uint16 | uint32 | []uint32 | uint64 | []uint64
}

// Float is a constraint for floats and slice of floats.
Expand Down
12 changes: 11 additions & 1 deletion internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewEnvParser(v any) EnvParser {
p = newStringParser(t)
case int, []int, int8, []int8, int16, []int16, int32, []int32, int64, []int64:
p = newIntParser(t)
case uint, []uint, uint8, uint32, []uint32, uint64, []uint64:
case uint, []uint, uint8, uint16, uint32, []uint32, uint64, []uint64:
p = newUintParser(t)
case bool:
p = boolParser(t)
Expand Down Expand Up @@ -84,6 +84,8 @@ func newUintParser(v any) EnvParser {
return uintParser(t)
case []uint:
return uintSliceParser(t)
case uint16:
return uint16Parser(t)
case uint32:
return uint32Parser(t)
case []uint32:
Expand Down Expand Up @@ -340,6 +342,14 @@ func (i uint32SliceParser) ParseEnv(key string, defaltVal any, options Parameter
return val
}

type uint16Parser uint

func (d uint16Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := uint16OrDefault(key, defaltVal.(uint16))

return val
}

type uint32Parser uint

func (d uint32Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
Expand Down
27 changes: 27 additions & 0 deletions internal/iface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func TestNewEnvParser(t *testing.T) {
want: uint8Parser(1),
wantPanic: assert.NotPanics,
},
{
name: "uint16",
args: args{
v: uint16(1),
},
want: uint16Parser(1),
wantPanic: assert.NotPanics,
},
{
name: "uint32",
args: args{
Expand Down Expand Up @@ -449,6 +457,25 @@ func Test_ParseEnv(t *testing.T) {
},
want: uint32(12),
},
{
name: "uint16Parser",
s: uint16Parser(0),
precond: precondition{
setenv: setenv{
isSet: true,
val: "12",
},
},
args: args{
key: testEnvKey,
defaltVal: uint16(99),
in2: Parameters{
Separator: ",",
Layout: "",
},
},
want: uint16(12),
},
{
name: "int8Parser",
s: int8Parser(0),
Expand Down
22 changes: 22 additions & 0 deletions internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,28 @@ func uint32SliceOrDefault(key string, defaultVal []uint32, sep string) []uint32
return val
}

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

const (
base = 10
bitsize = 16
)

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

return uint16(val)
}

// uint32OrDefault retrieves the unt32 value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
Expand Down
184 changes: 184 additions & 0 deletions internal/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2253,3 +2253,187 @@ func Test_uintOrDefault(t *testing.T) {
})
}
}

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

type expected struct {
val uint16
}

var tests = []struct {
name string
precond precondition
args args
expected expected
}{
{
name: "env not set - default returned",
precond: precondition{
setenv: setenv{
isSet: false,
val: "12",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 999,
},
},
{
name: "env set - env value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "12",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 12,
},
},
{
name: "empty env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 999,
},
},
{
name: "malformed env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "iii99",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 999,
},
},
}

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

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

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

type expected struct {
val uint32
}

var tests = []struct {
name string
precond precondition
args args
expected expected
}{
{
name: "env not set - default returned",
precond: precondition{
setenv: setenv{
isSet: false,
val: "12",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 999,
},
},
{
name: "env set - env value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "12",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 12,
},
},
{
name: "empty env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 999,
},
},
{
name: "malformed env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "iii99",
},
},
args: args{
key: testEnvKey,
defaultVal: 999,
},
expected: expected{
val: 999,
},
},
}

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

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

0 comments on commit 607a9e0

Please sign in to comment.