Skip to content

Commit 5b6e62a

Browse files
authored
feat: Add support of uint8 (#78)
* chore: Add helper for generating readme doc * feat: Add support of uint8
1 parent cccd593 commit 5b6e62a

File tree

12 files changed

+278
-16
lines changed

12 files changed

+278
-16
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ bin/
1919
dist/
2020
coverage/
2121
.DS_Store
22-
tests-report.json
22+
tests-report.json
23+
README_doc.md

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ update-readme-cover: prepare-cover-report
5454
$(COMPOSE_TOOLS_CMD_UP) update-readme-coverage update-readme-coverage
5555
.PHONY: update-readme-cover
5656

57+
## Update readme doc.
58+
update-readme-doc:
59+
$(COMPOSE_TOOLS_CMD_UP) update-readme-doc update-readme-doc
60+
.PHONY: update-readme-doc
61+
5762
## Run tests.
5863
test:
5964
$(COMPOSE_TOOLS_CMD_UP) run-tests run-tests

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
Package getenv provides functionality for loading environment variables and parse them into go builtin types.
1111

1212
Types supported:
13-
14-
```text
1513
- string
1614
- []string
1715
- int
@@ -24,6 +22,7 @@ Types supported:
2422
- []int32
2523
- int64
2624
- []int64
25+
- uint8
2726
- uint64
2827
- []uint64
2928
- uint
@@ -37,7 +36,6 @@ Types supported:
3736
- []time.Time
3837
- time.Duration
3938
- bool
40-
```
4139

4240
## Examples
4341

@@ -119,7 +117,7 @@ func main() {
119117

120118
```
121119

122-
Output:
120+
Output:
123121

124122
```
125123
[string]: golly

deployments/docker-compose/go-tools-docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ services:
3737
service: tools
3838
entrypoint: /bin/sh -c './scripts/update-readme-coverage.sh'
3939

40+
update-readme-doc:
41+
extends:
42+
service: tools
43+
entrypoint: /bin/sh -c './scripts/update-readme-doc.sh'
44+
4045
lint-full:
4146
extends:
4247
service: tools

getenv.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// - []int32
1414
// - int64
1515
// - []int64
16+
// - uint8
1617
// - uint64
1718
// - []uint64
1819
// - uint

getenv_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,82 @@ func TestDurationOrDefault(t *testing.T) {
13581358
}
13591359
}
13601360

1361+
func TestUint8OrDefault(t *testing.T) {
1362+
type args struct {
1363+
key string
1364+
defaultVal uint8
1365+
}
1366+
1367+
type expected struct {
1368+
val uint8
1369+
}
1370+
1371+
var tests = []struct {
1372+
name string
1373+
precond precondition
1374+
args args
1375+
expected expected
1376+
}{
1377+
{
1378+
name: "env not set - default returned",
1379+
precond: precondition{
1380+
setenv: setenv{
1381+
isSet: false,
1382+
val: "12",
1383+
},
1384+
},
1385+
args: args{
1386+
key: testEnvKey,
1387+
defaultVal: 99,
1388+
},
1389+
expected: expected{
1390+
val: 99,
1391+
},
1392+
},
1393+
{
1394+
name: "env set - env value returned",
1395+
precond: precondition{
1396+
setenv: setenv{
1397+
isSet: true,
1398+
val: "12",
1399+
},
1400+
},
1401+
args: args{
1402+
key: testEnvKey,
1403+
defaultVal: 99,
1404+
},
1405+
expected: expected{
1406+
val: 12,
1407+
},
1408+
},
1409+
{
1410+
name: "empty env value set - default returned",
1411+
precond: precondition{
1412+
setenv: setenv{
1413+
isSet: true,
1414+
val: "",
1415+
},
1416+
},
1417+
args: args{
1418+
key: testEnvKey,
1419+
defaultVal: 99,
1420+
},
1421+
expected: expected{
1422+
val: 99,
1423+
},
1424+
},
1425+
}
1426+
1427+
for _, tt := range tests {
1428+
t.Run(tt.name, func(t *testing.T) {
1429+
tt.precond.maybeSetEnv(t, tt.args.key)
1430+
1431+
got := getenv.EnvOrDefault(tt.args.key, tt.args.defaultVal)
1432+
assert.Equal(t, tt.expected.val, got)
1433+
})
1434+
}
1435+
}
1436+
13611437
func TestUint64OrDefault(t *testing.T) {
13621438
type args struct {
13631439
key string

internal/constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type (
2222

2323
// Uint is a constraint for unsigned integer and slice of unsigned integers.
2424
Uint interface {
25-
uint64 | []uint64 | uint | []uint | []uint32 | uint32
25+
uint | []uint | uint8 | uint32 | []uint32 | uint64 | []uint64
2626
}
2727

2828
// Float is a constraint for floats and slice of floats.

internal/iface.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func NewEnvParser(v any) EnvParser {
1515
p = newStringParser(t)
1616
case int, []int, int8, []int8, int16, []int16, int32, []int32, int64, []int64:
1717
p = newIntParser(t)
18-
case uint64, []uint64, uint, []uint, uint32, []uint32:
18+
case uint, []uint, uint8, uint32, []uint32, uint64, []uint64:
1919
p = newUintParser(t)
2020
case bool:
2121
p = boolParser(t)
@@ -78,18 +78,20 @@ func newIntParser(v any) EnvParser {
7878

7979
func newUintParser(v any) EnvParser {
8080
switch t := v.(type) {
81-
case uint64:
82-
return uint64Parser(t)
83-
case []uint64:
84-
return uint64SliceParser(t)
81+
case uint8:
82+
return uint8Parser(t)
8583
case uint:
8684
return uintParser(t)
8785
case []uint:
8886
return uintSliceParser(t)
89-
case []uint32:
90-
return uint32SliceParser(t)
9187
case uint32:
9288
return uint32Parser(t)
89+
case []uint32:
90+
return uint32SliceParser(t)
91+
case uint64:
92+
return uint64Parser(t)
93+
case []uint64:
94+
return uint64SliceParser(t)
9395
default:
9496
return nil
9597
}
@@ -302,6 +304,14 @@ func (i uint64SliceParser) ParseEnv(key string, defaltVal any, options Parameter
302304
return val
303305
}
304306

307+
type uint8Parser uint
308+
309+
func (d uint8Parser) ParseEnv(key string, defaltVal any, _ Parameters) any {
310+
val := uint8OrDefault(key, defaltVal.(uint8))
311+
312+
return val
313+
}
314+
305315
type uintParser uint
306316

307317
func (d uintParser) ParseEnv(key string, defaltVal any, _ Parameters) any {

internal/iface_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10+
type notsupported struct {
11+
name string
12+
}
13+
1014
func TestNewEnvParser(t *testing.T) {
1115
type args struct {
1216
v any
@@ -83,7 +87,15 @@ func TestNewEnvParser(t *testing.T) {
8387
wantPanic: assert.NotPanics,
8488
},
8589
{
86-
name: "uint23",
90+
name: "uint8",
91+
args: args{
92+
v: uint8(1),
93+
},
94+
want: uint8Parser(1),
95+
wantPanic: assert.NotPanics,
96+
},
97+
{
98+
name: "uint32",
8799
args: args{
88100
v: uint32(1),
89101
},
@@ -221,7 +233,9 @@ func TestNewEnvParser(t *testing.T) {
221233
{
222234
name: "not supported - panics",
223235
args: args{
224-
v: byte(1),
236+
v: notsupported{
237+
name: "name",
238+
},
225239
},
226240
want: nil,
227241
wantPanic: assert.Panics,
@@ -359,6 +373,25 @@ func Test_ParseEnv(t *testing.T) {
359373
},
360374
want: []float64{-1.2, 0.2},
361375
},
376+
{
377+
name: "uint8Parser",
378+
s: uint8Parser(0),
379+
precond: precondition{
380+
setenv: setenv{
381+
isSet: true,
382+
val: "12",
383+
},
384+
},
385+
args: args{
386+
key: testEnvKey,
387+
defaltVal: uint8(99),
388+
in2: Parameters{
389+
Separator: ",",
390+
Layout: "",
391+
},
392+
},
393+
want: uint8(12),
394+
},
362395
{
363396
name: "uint64Parser",
364397
s: uint64Parser(0),

internal/parsers.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,29 @@ func uint64SliceOrDefault(key string, defaultVal []uint64, sep string) []uint64
470470
return val
471471
}
472472

473-
// uintOrDefault retrieves the unt64 value of the environment variable named
473+
// uint8OrDefault retrieves the unt8 value of the environment variable named
474+
// by the key.
475+
// If variable not set or value is empty - defaultVal will be returned.
476+
func uint8OrDefault(key string, defaultVal uint8) uint8 {
477+
env := stringOrDefault(key, "")
478+
if env == "" {
479+
return defaultVal
480+
}
481+
482+
const (
483+
base = 10
484+
bitsize = 8
485+
)
486+
487+
val, err := strconv.ParseUint(env, base, bitsize)
488+
if err != nil {
489+
return defaultVal
490+
}
491+
492+
return uint8(val)
493+
}
494+
495+
// uintOrDefault retrieves the unt value of the environment variable named
474496
// by the key.
475497
// If variable not set or value is empty - defaultVal will be returned.
476498
func uintOrDefault(key string, defaultVal uint) uint {

0 commit comments

Comments
 (0)