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 ParserEnvWithValue #284

Merged
merged 1 commit into from
Apr 3, 2024
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
10 changes: 10 additions & 0 deletions parsers/dotenv/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ func ParserEnv(prefix, delim string, cb func(s string) string) *DotEnv {
}
}

// ParserEnvWithValue allows to make the DOTENV Parser behave like the env.ProviderWithValue.
func ParserEnvWithValue(prefix, delim string, cb func(key string, value string) (string, interface{})) *DotEnv {
return &DotEnv{
delim: delim,
prefix: prefix,
cb: cb,
reverseCB: make(map[string]string),
}
}

// Unmarshal parses the given DOTENV bytes.
func (p *DotEnv) Unmarshal(b []byte) (map[string]interface{}, error) {
// Unmarshal DOTENV from []byte
Expand Down
85 changes: 85 additions & 0 deletions parsers/dotenv/dotenv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dotenv

import (
"encoding/json"
"strings"
"testing"

Expand Down Expand Up @@ -219,3 +220,87 @@ func TestCompareToEnvProvider(t *testing.T) {
})
}
}

func TestParserEnvWithValue(t *testing.T) {
testCases := []struct {
name string
prefix string
delim string
key string
value string
expKey string
expValue interface{}
cb func(key, value string) (string, interface{})
}{
{
name: "Nil cb",
prefix: "TESTVAR_",
delim: ".",
},
{
name: "Simple cb",
prefix: "TESTVAR_",
delim: ".",
key: "TestKey",
value: "TestVal",
expKey: "testkey",
expValue: "TestVal",
cb: func(key, value string) (string, interface{}) {
return strings.ToLower(key), value
},
},
{
name: "Empty string nil cb",
prefix: "",
delim: ".",
},
{
name: "Cb is given",
prefix: "",
delim: ".",
key: "test_key",
value: "test_val",
expKey: "TEST.KEY",
expValue: "test_val",
cb: func(key, value string) (string, interface{}) {
return strings.Replace(strings.ToUpper(key), "_", ".", -1), value
},
},
{
name: "Cb is given and changes value",
prefix: "",
delim: ".",
key: "test_key",
value: `{"foo": "bar"}`,
expKey: "TEST.KEY",
expValue: map[string]interface{}{
"foo": "bar",
},
cb: func(key, value string) (string, interface{}) {
key = strings.Replace(strings.ToUpper(key), "_", ".", -1)

var v map[string]interface{}
err := json.Unmarshal([]byte(value), &v)
if err == nil {
return key, v
}
return key, value
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotProvider := ParserEnvWithValue(tc.prefix, tc.delim, tc.cb)
if tc.cb == nil {
assert.Equal(t, tc.prefix, gotProvider.prefix)
assert.Equal(t, tc.delim, gotProvider.delim)
}
if tc.cb != nil {
k, v := gotProvider.cb(tc.key, tc.value)
assert.Equal(t, tc.expKey, k)
assert.Equal(t, tc.expValue, v)
}
})
}
}
Loading