Skip to content

Commit

Permalink
feat: Add support of []net.IP (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Mar 27, 2023
1 parent 8bcac8d commit 35bea46
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 5 deletions.
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-97.58%25-brightgreen?longCache=true&style=flat)
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-98.93%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 @@ -44,6 +44,7 @@ Types supported:
- url.URL
- []url.URL
- net.IP
- []net.IP

## Examples

Expand Down
1 change: 1 addition & 0 deletions getenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// - url.URL
// - []url.URL
// - net.IP
// - []net.IP
package getenv

import (
Expand Down
85 changes: 85 additions & 0 deletions getenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3050,6 +3050,91 @@ func TestIPOrDefault(t *testing.T) {
}
}

func TestIPSliceOrDefault(t *testing.T) {
const rawDefault = "0.0.0.0"

type args struct {
key string
defaultVal []net.IP
separator string
}

type expected struct {
val []net.IP
}

var tests = []struct {
name string
precond precondition
args args
expected expected
}{
{
name: "env not set - default returned",
precond: precondition{
setenv: setenv{
isSet: false,
val: "192.168.8.0,2001:cb8::17",
},
},
args: args{
key: testEnvKey,
defaultVal: []net.IP{getIP(t, rawDefault)},
separator: ",",
},
expected: expected{
val: []net.IP{getIP(t, rawDefault)},
},
},
{
name: "env set - env value returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "192.168.8.0,2001:cb8::17",
},
},
args: args{
key: testEnvKey,
defaultVal: []net.IP{getIP(t, rawDefault)},
separator: ",",
},
expected: expected{
val: []net.IP{
getIP(t, "192.168.8.0"),
getIP(t, "2001:cb8::17"),
},
},
},
{
name: "empty env value set - default returned",
precond: precondition{
setenv: setenv{
isSet: true,
val: "",
},
},
args: args{
key: testEnvKey,
defaultVal: []net.IP{getIP(t, rawDefault)},
separator: ",",
},
expected: expected{
val: []net.IP{getIP(t, rawDefault)},
},
},
}

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.separator))
assert.Equal(t, tt.expected.val, got)
})
}
}

func TestURLSliceOrDefault(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 @@ -9,7 +9,7 @@ import (
type (
// EnvParsable is a constraint for supported environment variable types parsers.
EnvParsable interface {
String | Int | Uint | Float | Time | bool | url.URL | []url.URL | net.IP
String | Int | Uint | Float | Time | bool | url.URL | []url.URL | net.IP | []net.IP
}

// String is a constraint for strings and slice of strings.
Expand Down
12 changes: 12 additions & 0 deletions internal/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func NewEnvParser(v any) EnvParser {
p = urlSliceParser(t)
case net.IP:
p = ipParser(t)
case []net.IP:
p = ipSliceParser(t)
default:
p = nil
}
Expand Down Expand Up @@ -448,3 +450,13 @@ func (t ipParser) ParseEnv(key string, defaltVal any, _ Parameters) any {

return val
}

type ipSliceParser []net.IP

func (t ipSliceParser) ParseEnv(key string, defaltVal any, opts Parameters) any {
separator := opts.Separator

val := ipSliceOrDefault(key, defaltVal.([]net.IP), separator)

return val
}
32 changes: 29 additions & 3 deletions internal/iface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ func TestNewEnvParser(t *testing.T) {
want: ipParser(net.IP{}),
wantPanic: assert.NotPanics,
},
{
name: "[]net.IP",
args: args{
v: []net.IP{},
},
want: ipSliceParser([]net.IP{}),
wantPanic: assert.NotPanics,
},
{
name: "not supported - panics",
args: args{
Expand Down Expand Up @@ -972,12 +980,30 @@ func Test_ParseEnv(t *testing.T) {
args: args{
key: testEnvKey,
defaltVal: net.IP{},
in2: Parameters{},
},
want: getIP(t, "2001:cb8::17"),
},
{
name: "ipSliceParser",
s: ipSliceParser([]net.IP{}),
precond: precondition{
setenv: setenv{
isSet: true,
val: "2001:cb8::17,192.168.0.1",
},
},
args: args{
key: testEnvKey,
defaltVal: []net.IP{},
in2: Parameters{
Separator: "",
Layout: time.DateOnly,
Separator: ",",
},
},
want: getIP(t, "2001:cb8::17"),
want: []net.IP{
getIP(t, "2001:cb8::17"),
getIP(t, "192.168.0.1"),
},
},
}

Expand Down
23 changes: 23 additions & 0 deletions internal/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,26 @@ func ipOrDefault(key string, defaultVal net.IP) net.IP {

return val
}

// ipSliceOrDefault retrieves the net.IP 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 ipSliceOrDefault(key string, defaultVal []net.IP, sep string) []net.IP {
valraw := stringSliceOrDefault(key, nil, sep)
if valraw == nil {
return defaultVal
}

val := make([]net.IP, 0, len(valraw))

for _, s := range valraw {
v := net.ParseIP(s)
if v == nil {
return defaultVal
}

val = append(val, v)
}

return val
}

0 comments on commit 35bea46

Please sign in to comment.