-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extend the shared libraries (#94)
* feat: add new packages * chore: update docs
- Loading branch information
1 parent
c1e986c
commit fdb6b31
Showing
6 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package env | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// GetBool converts the given value to the bool type and returns that value, or returns the specified fallback value if the value is empty. | ||
func GetBool(value string, fallback bool) bool { | ||
if strVal, ok := nonEmptyValue(value); ok { | ||
if val, err := strconv.ParseBool(strVal); err == nil { | ||
return val | ||
} | ||
} | ||
|
||
return fallback | ||
} | ||
|
||
// GetNegativeBool converts the given value to the bool type and returns the inverted value, or returns the specified fallback value if the value is empty. | ||
func GetNegativeBool(value string, fallback bool) bool { | ||
if strVal, ok := nonEmptyValue(value); ok { | ||
if val, err := strconv.ParseBool(strVal); err == nil { | ||
return !val | ||
} | ||
} | ||
|
||
return fallback | ||
} | ||
|
||
// GetInt converts the given value to the integer type and returns that value, or returns the specified fallback value if the value is empty. | ||
func GetInt(value string, fallback int) int { | ||
if strVal, ok := nonEmptyValue(value); ok { | ||
if val, err := strconv.Atoi(strVal); err == nil { | ||
return val | ||
} | ||
} | ||
|
||
return fallback | ||
} | ||
|
||
// GetString returns the same string value, or returns the given fallback value if the value is empty. | ||
func GetString(value string, fallback string) string { | ||
if val, ok := nonEmptyValue(value); ok { | ||
return val | ||
} | ||
|
||
return fallback | ||
} | ||
|
||
// nonEmptyValue trims spaces in the value and returns this trimmed value and true if the value is not empty, otherwise false. | ||
func nonEmptyValue(value string) (string, bool) { | ||
value = strings.TrimSpace(value) | ||
isPresent := value != "" | ||
|
||
return value, isPresent | ||
} | ||
|
||
func Parse(envs []string) map[string]string { | ||
envMap := make(map[string]string) | ||
|
||
for _, env := range envs { | ||
parts := strings.SplitN(env, "=", 2) | ||
|
||
if len(parts) == 2 { | ||
envMap[strings.TrimSpace(parts[0])] = parts[1] | ||
} | ||
} | ||
|
||
return envMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package env | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetBool(t *testing.T) { | ||
t.Parallel() | ||
|
||
var testCases = []struct { | ||
envVarValue string | ||
fallback bool | ||
expected bool | ||
}{ | ||
// false | ||
{"", false, false}, | ||
{"false", false, false}, | ||
{" false ", false, false}, | ||
{"False", false, false}, | ||
{"FALSE", false, false}, | ||
{"0", false, false}, | ||
// true | ||
{"true", false, true}, | ||
{" true ", false, true}, | ||
{"True", false, true}, | ||
{"TRUE", false, true}, | ||
{"", true, true}, | ||
{"", true, true}, | ||
{"1", true, true}, | ||
{"foo", false, false}, | ||
} | ||
|
||
for i, testCase := range testCases { | ||
// to make sure testCase's values don't get updated due to concurrency within the scope of t.Run(..) below | ||
testCase := testCase | ||
|
||
envVarName := fmt.Sprintf("TestGetBool-testCase-%d", i) | ||
t.Run(envVarName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := GetBool(testCase.envVarValue, testCase.fallback) | ||
assert.Equal(t, testCase.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetNegativeBool(t *testing.T) { | ||
t.Parallel() | ||
|
||
var testCases = []struct { | ||
envVarValue string | ||
fallback bool | ||
expected bool | ||
}{ | ||
// true | ||
{"", true, true}, | ||
{"false", false, true}, | ||
{" false ", false, true}, | ||
{"False", false, true}, | ||
{"FALSE", false, true}, | ||
{"0", false, true}, | ||
// false | ||
{"", false, false}, | ||
{"true", false, false}, | ||
{" true ", false, false}, | ||
{"True", false, false}, | ||
{"TRUE", false, false}, | ||
|
||
{"1", true, false}, | ||
{"foo", false, false}, | ||
} | ||
|
||
for i, testCase := range testCases { | ||
// to make sure testCase's values don't get updated due to concurrency within the scope of t.Run(..) below | ||
testCase := testCase | ||
|
||
envVarName := fmt.Sprintf("TestGetNegativeBool-testCase-%d", i) | ||
t.Run(envVarName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := GetNegativeBool(testCase.envVarValue, testCase.fallback) | ||
assert.Equal(t, testCase.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetInt(t *testing.T) { | ||
t.Parallel() | ||
|
||
var testCases = []struct { | ||
envVarValue string | ||
fallback int | ||
expected int | ||
}{ | ||
{"10", 20, 10}, | ||
{"0", 30, 0}, | ||
{"", 5, 5}, | ||
{"foo", 15, 15}, | ||
} | ||
|
||
for i, testCase := range testCases { | ||
// to make sure testCase's values don't get updated due to concurrency within the scope of t.Run(..) below | ||
testCase := testCase | ||
|
||
envVarName := fmt.Sprintf("TestGetInt-testCase-%d", i) | ||
t.Run(envVarName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := GetInt(testCase.envVarValue, testCase.fallback) | ||
assert.Equal(t, testCase.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetString(t *testing.T) { | ||
t.Parallel() | ||
|
||
var testCases = []struct { | ||
envVarValue string | ||
fallback string | ||
expected string | ||
}{ | ||
{"first", "second", "first"}, | ||
{"", "second", "second"}, | ||
} | ||
|
||
for i, testCase := range testCases { | ||
// to make sure testCase's values don't get updated due to concurrency within the scope of t.Run(..) below | ||
testCase := testCase | ||
|
||
envVarName := fmt.Sprintf("test-%d-val-%s-expected-%s", i, testCase.envVarValue, testCase.expected) | ||
t.Run(envVarName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := GetString(testCase.envVarValue, testCase.fallback) | ||
assert.Equal(t, testCase.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestParseironmentVariables(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
environmentVariables []string | ||
expectedVariables map[string]string | ||
}{ | ||
{ | ||
[]string{}, | ||
map[string]string{}, | ||
}, | ||
{ | ||
[]string{"foobar"}, | ||
map[string]string{}, | ||
}, | ||
{ | ||
[]string{"foo=bar"}, | ||
map[string]string{"foo": "bar"}, | ||
}, | ||
{ | ||
[]string{"foo=bar", "goo=gar"}, | ||
map[string]string{"foo": "bar", "goo": "gar"}, | ||
}, | ||
{ | ||
[]string{"foo=bar "}, | ||
map[string]string{"foo": "bar "}, | ||
}, | ||
{ | ||
[]string{"foo =bar "}, | ||
map[string]string{"foo": "bar "}, | ||
}, | ||
{ | ||
[]string{"foo=composite=bar"}, | ||
map[string]string{"foo": "composite=bar"}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
actualVariables := Parse(testCase.environmentVariables) | ||
assert.Equal(t, testCase.expectedVariables, actualVariables) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters