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 uint8 #78

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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ bin/
dist/
coverage/
.DS_Store
tests-report.json
tests-report.json
README_doc.md
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ update-readme-cover: prepare-cover-report
$(COMPOSE_TOOLS_CMD_UP) update-readme-coverage update-readme-coverage
.PHONY: update-readme-cover

## Update readme doc.
update-readme-doc:
$(COMPOSE_TOOLS_CMD_UP) update-readme-doc update-readme-doc
.PHONY: update-readme-doc

## Run tests.
test:
$(COMPOSE_TOOLS_CMD_UP) run-tests run-tests
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
Package getenv provides functionality for loading environment variables and parse them into go builtin types.

Types supported:

```text
- string
- []string
- int
Expand All @@ -24,6 +22,7 @@ Types supported:
- []int32
- int64
- []int64
- uint8
- uint64
- []uint64
- uint
Expand All @@ -37,7 +36,6 @@ Types supported:
- []time.Time
- time.Duration
- bool
```

## Examples

Expand Down Expand Up @@ -119,7 +117,7 @@ func main() {

```

Output:
Output:

```
[string]: golly
Expand Down
5 changes: 5 additions & 0 deletions deployments/docker-compose/go-tools-docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ services:
service: tools
entrypoint: /bin/sh -c './scripts/update-readme-coverage.sh'

update-readme-doc:
extends:
service: tools
entrypoint: /bin/sh -c './scripts/update-readme-doc.sh'

lint-full:
extends:
service: tools
Expand Down
1 change: 1 addition & 0 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// - []int32
// - int64
// - []int64
// - uint8
// - 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 @@ -1358,6 +1358,82 @@ func TestDurationOrDefault(t *testing.T) {
}
}

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

type expected struct {
val uint8
}

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 {
uint64 | []uint64 | uint | []uint | []uint32 | uint32
uint | []uint | uint8 | uint32 | []uint32 | uint64 | []uint64
}

// Float is a constraint for floats and slice of floats.
Expand Down
24 changes: 17 additions & 7 deletions 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 uint64, []uint64, uint, []uint, uint32, []uint32:
case uint, []uint, uint8, uint32, []uint32, uint64, []uint64:
p = newUintParser(t)
case bool:
p = boolParser(t)
Expand Down Expand Up @@ -78,18 +78,20 @@ func newIntParser(v any) EnvParser {

func newUintParser(v any) EnvParser {
switch t := v.(type) {
case uint64:
return uint64Parser(t)
case []uint64:
return uint64SliceParser(t)
case uint8:
return uint8Parser(t)
case uint:
return uintParser(t)
case []uint:
return uintSliceParser(t)
case []uint32:
return uint32SliceParser(t)
case uint32:
return uint32Parser(t)
case []uint32:
return uint32SliceParser(t)
case uint64:
return uint64Parser(t)
case []uint64:
return uint64SliceParser(t)
default:
return nil
}
Expand Down Expand Up @@ -302,6 +304,14 @@ func (i uint64SliceParser) ParseEnv(key string, defaltVal any, options Parameter
return val
}

type uint8Parser uint

func (d uint8Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
val := uint8OrDefault(key, defaltVal.(uint8))

return val
}

type uintParser uint

func (d uintParser) ParseEnv(key string, defaltVal any, _ Parameters) any {
Expand Down
37 changes: 35 additions & 2 deletions internal/iface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/stretchr/testify/assert"
)

type notsupported struct {
name string
}

func TestNewEnvParser(t *testing.T) {
type args struct {
v any
Expand Down Expand Up @@ -83,7 +87,15 @@ func TestNewEnvParser(t *testing.T) {
wantPanic: assert.NotPanics,
},
{
name: "uint23",
name: "uint8",
args: args{
v: uint8(1),
},
want: uint8Parser(1),
wantPanic: assert.NotPanics,
},
{
name: "uint32",
args: args{
v: uint32(1),
},
Expand Down Expand Up @@ -221,7 +233,9 @@ func TestNewEnvParser(t *testing.T) {
{
name: "not supported - panics",
args: args{
v: byte(1),
v: notsupported{
name: "name",
},
},
want: nil,
wantPanic: assert.Panics,
Expand Down Expand Up @@ -359,6 +373,25 @@ func Test_ParseEnv(t *testing.T) {
},
want: []float64{-1.2, 0.2},
},
{
name: "uint8Parser",
s: uint8Parser(0),
precond: precondition{
setenv: setenv{
isSet: true,
val: "12",
},
},
args: args{
key: testEnvKey,
defaltVal: uint8(99),
in2: Parameters{
Separator: ",",
Layout: "",
},
},
want: uint8(12),
},
{
name: "uint64Parser",
s: uint64Parser(0),
Expand Down
24 changes: 23 additions & 1 deletion internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,29 @@ func uint64SliceOrDefault(key string, defaultVal []uint64, sep string) []uint64
return val
}

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

const (
base = 10
bitsize = 8
)

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

return uint8(val)
}

// uintOrDefault retrieves the unt value of the environment variable named
// by the key.
// If variable not set or value is empty - defaultVal will be returned.
func uintOrDefault(key string, defaultVal uint) uint {
Expand Down
Loading