Skip to content

Commit

Permalink
Merge pull request #2 from john-peterson-g17/add-comma-list
Browse files Browse the repository at this point in the history
Add functionality for parsing comma list value into a slice of strings
  • Loading branch information
syntaqx authored Jul 15, 2024
2 parents 97fb6d3 + b6e3d42 commit 6a9ce90
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func main() {

port := env.GetWithFallback("PORT", "8080")
fmt.Printf("Port: %s\n", port)

// Assuming the value of HOSTS is a comma separated list of strings
// Example: some-host:8000,another-host:8000
hosts := env.GetSliceWithFallback("HOSTS", []string{"fallback-host-1:8000", "fallback-host-2:8000"})
fmt.Printf("Hosts: %v\n", hosts)
}
```

Expand Down
10 changes: 10 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"os"
"strings"

"github.com/joho/godotenv"
)
Expand Down Expand Up @@ -31,6 +32,15 @@ func GetWithFallback(key string, fallback string) string {
return fallback
}

// GetSliceWithFallback returns the value of a comma separated environment variable as a slice
// of strings or a fallback value if the environment variable is not set.
func GetSliceWithFallback(key string, fallback []string) []string {
if value, ok := Lookup(key); ok {
return strings.Split(value, ",")
}
return fallback
}

// GetBool returns the value of an environment variable as a boolean.
func GetBool(key string) bool {
return Get(key) == "true"
Expand Down
65 changes: 65 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package env
import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetWithFallback(t *testing.T) {
Expand Down Expand Up @@ -103,3 +105,66 @@ func TestGetBoolWithFallback(t *testing.T) {
})
}
}
func TestGetSliceWithFallback(t *testing.T) {
tests := []struct {
setValue bool
key string
fallback []string
envValue string
wantValue []string
}{
{
setValue: true,
key: "EXISTING_KEY_SLICE_ONE_VALUE",
fallback: []string{"fallback_value_1"},
envValue: "existing_value",
wantValue: []string{"existing_value"},
},
{
setValue: false,
key: "NON_EXISTING_KEY_SLICE",
fallback: []string{"fallback_value"},
envValue: "",
wantValue: []string{"fallback_value"},
},
{
setValue: true,
key: "EMPTY_VALUE_KEY_SLICE",
fallback: []string{"fallback_value"},
envValue: "",
wantValue: []string{""},
},
{
setValue: true,
key: "EXISTING_KEY_SLICE_MULTI_VALUE",
fallback: []string{"fallback_value_1", "fallback_value_2"},
envValue: "existing_value_1,existing_value_2",
wantValue: []string{"existing_value_1", "existing_value_2"},
},
{
setValue: false,
key: "NON_EXISTING_KEY_SLICE_MULTI_VALUE",
fallback: []string{"fallback_value_1", "fallback_value_2"},
envValue: "",
wantValue: []string{"fallback_value_1", "fallback_value_2"},
},
}

for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
// Set the environment variable
if tt.setValue {
os.Setenv(tt.key, tt.envValue)
}

// Call the function under test
got := GetSliceWithFallback(tt.key, tt.fallback)

// Check if the returned value matches the expected value
assert.ElementsMatch(t, tt.wantValue, got, "GetSliceWithFallback(%q, %q) should return expected slice elements, order ignored", tt.key, tt.fallback)

// Unset the environment variable
os.Unsetenv(tt.key)
})
}
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ module github.com/syntaqx/env
go 1.22.3

require github.com/joho/godotenv v1.5.1

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 6a9ce90

Please sign in to comment.