Skip to content

Commit

Permalink
feat: Add support of []uint16 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko authored Mar 27, 2023
1 parent 076cb6c commit 33f7bb3
Show file tree
Hide file tree
Showing 8 changed files with 446 additions and 4 deletions.
5 changes: 3 additions & 2 deletions 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-97.66%25-brightgreen?longCache=true&style=flat)
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-98.25%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 All @@ -25,6 +25,7 @@ Types supported:
- uint8
- []uint8
- uint16
- []uint16
- uint64
- []uint64
- uint
Expand Down Expand Up @@ -53,7 +54,7 @@ package main

import (
"fmt"
url "net/url"
"net/url"
"os"
"time"

Expand Down
1 change: 1 addition & 0 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// - uint8
// - []uint8
// - uint16
// - []uint16
// - uint64
// - []uint64
// - uint
Expand Down
113 changes: 113 additions & 0 deletions getenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,119 @@ func TestUint8SliceOrDefault(t *testing.T) {
}
}

func TestUint16SliceOrDefault(t *testing.T) {
type args struct {
key string
defaultVal []uint16
sep string
}

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: "1,27",
},
},
args: args{
key: testEnvKey,
defaultVal: []uint16{99},
sep: ",",
},
expected: expected{
val: []uint16{99},
},
},
{
name: "env set - env value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "1,2",
},
},
args: args{
key: testEnvKey,
defaultVal: []uint16{99},
sep: ",",
},
expected: expected{
val: []uint16{1, 2},
},
},
{
name: "env set, no separator - default value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "1,2",
},
},
args: args{
key: testEnvKey,
defaultVal: []uint16{99},
sep: "",
},
expected: expected{
val: []uint16{99},
},
},
{
name: "env set, wrong separator - default value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "1,2",
},
},
args: args{
key: testEnvKey,
defaultVal: []uint16{99},
sep: "|",
},
expected: expected{
val: []uint16{99},
},
},
{
name: "empty env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "",
},
},
args: args{
key: testEnvKey,
defaultVal: []uint16{99},
},
expected: expected{
val: []uint16{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, option.WithSeparator(tt.args.sep))
assert.Equal(t, tt.expected.val, got)
})
}
}

func TestInt16SliceOrDefault(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 @@ -23,7 +23,7 @@ type (

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

// Float is a constraint for floats and slice of floats.
Expand Down
14 changes: 13 additions & 1 deletion internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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, []uint8, uint16, uint32, []uint32, uint64, []uint64:
case uint, []uint, uint8, []uint8, uint16, []uint16, uint32, []uint32, uint64, []uint64:
p = newUintParser(t)
case bool:
p = boolParser(t)
Expand Down Expand Up @@ -91,6 +91,8 @@ func newUintParser(v any) EnvParser {
return uintSliceParser(t)
case uint16:
return uint16Parser(t)
case []uint16:
return uint16SliceParser(t)
case uint32:
return uint32Parser(t)
case []uint32:
Expand Down Expand Up @@ -357,6 +359,16 @@ func (i uint32SliceParser) ParseEnv(key string, defaltVal any, options Parameter
return val
}

type uint16SliceParser []uint16

func (i uint16SliceParser) ParseEnv(key string, defaltVal any, options Parameters) any {
sep := options.Separator

val := uint16SliceOrDefault(key, defaltVal.([]uint16), sep)

return val
}

type uint16Parser uint

func (d uint16Parser) 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 @@ -111,6 +111,14 @@ func TestNewEnvParser(t *testing.T) {
want: uint32Parser(1),
wantPanic: assert.NotPanics,
},
{
name: "[]uint16",
args: args{
v: []uint16{1},
},
want: uint16SliceParser{1},
wantPanic: assert.NotPanics,
},
{
name: "[]uint32",
args: args{
Expand Down Expand Up @@ -455,6 +463,25 @@ func Test_ParseEnv(t *testing.T) {
},
want: []uint64{12, 89},
},
{
name: "uint16SliceParser",
s: uint16SliceParser(nil),
precond: precondition{
setenv: setenv{
isSet: true,
val: "12,89",
},
},
args: args{
key: testEnvKey,
defaltVal: []uint16{99},
in2: Parameters{
Separator: ",",
Layout: "",
},
},
want: []uint16{12, 89},
},
{
name: "uint32Parser",
s: uint32Parser(0),
Expand Down
28 changes: 28 additions & 0 deletions internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,34 @@ func uint8SliceOrDefault(key string, defaultVal []uint8, sep string) []uint8 {
return val
}

// uint16SliceOrDefault retrieves the uint16 slice value of the environment variable named
// by the key and separated by sep.
// If variable not set or value is empty - defaultVal will be returned.
func uint16SliceOrDefault(key string, defaultVal []uint16, sep string) []uint16 {
valraw := stringSliceOrDefault(key, nil, sep)
if valraw == nil {
return defaultVal
}

val := make([]uint16, 0, len(valraw))

const (
base = 10
bitsize = 16
)

for _, s := range valraw {
v, err := strconv.ParseUint(s, base, bitsize)
if err != nil {
return defaultVal
}

val = append(val, uint16(v))
}

return val
}

// uint32SliceOrDefault retrieves the uint32 slice value of the environment variable named
// by the key and separated by sep.
// If variable not set or value is empty - defaultVal will be returned.
Expand Down
Loading

0 comments on commit 33f7bb3

Please sign in to comment.