-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow nested koanf file merging (#240)
- Loading branch information
Showing
8 changed files
with
214 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package configx | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/ghodss/yaml" | ||
"github.com/pelletier/go-toml" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKoanfFile(t *testing.T) { | ||
setupFile := func(t *testing.T, fn, fc, subKey string) *KoanfFile { | ||
f, err := ioutil.TempFile("", fn) | ||
require.NoError(t, err) | ||
_, err = f.Write([]byte(fc)) | ||
require.NoError(t, err) | ||
|
||
kf, err := NewKoanfFileSubKey(context.Background(), f.Name(), subKey) | ||
require.NoError(t, err) | ||
return kf | ||
} | ||
|
||
t.Run("case=reads json root file", func(t *testing.T) { | ||
v := map[string]interface{}{ | ||
"foo": "bar", | ||
} | ||
encV, err := json.Marshal(v) | ||
require.NoError(t, err) | ||
|
||
kf := setupFile(t, "config*.json", string(encV), "") | ||
|
||
actual, err := kf.Read() | ||
require.NoError(t, err) | ||
assert.Equal(t, v, actual) | ||
}) | ||
|
||
t.Run("case=reads yaml root file", func(t *testing.T) { | ||
v := map[string]interface{}{ | ||
"foo": "yaml string", | ||
} | ||
encV, err := yaml.Marshal(v) | ||
require.NoError(t, err) | ||
|
||
kf := setupFile(t, "config*.yml", string(encV), "") | ||
|
||
actual, err := kf.Read() | ||
require.NoError(t, err) | ||
assert.Equal(t, v, actual) | ||
}) | ||
|
||
t.Run("case=reads toml root file", func(t *testing.T) { | ||
v := map[string]interface{}{ | ||
"foo": "toml string", | ||
} | ||
encV, err := toml.Marshal(v) | ||
require.NoError(t, err) | ||
|
||
kf := setupFile(t, "config*.toml", string(encV), "") | ||
|
||
actual, err := kf.Read() | ||
require.NoError(t, err) | ||
assert.Equal(t, v, actual) | ||
}) | ||
|
||
t.Run("case=reads json file as subkey", func(t *testing.T) { | ||
v := map[string]interface{}{ | ||
"bar": "asdf", | ||
} | ||
encV, err := json.Marshal(v) | ||
require.NoError(t, err) | ||
|
||
kf := setupFile(t, "config*.json", string(encV), "parent.of.config") | ||
|
||
actual, err := kf.Read() | ||
require.NoError(t, err) | ||
assert.Equal(t, map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"of": map[string]interface{}{ | ||
"config": v, | ||
}, | ||
}, | ||
}, actual) | ||
}) | ||
} |
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
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,11 @@ | ||
package stringslice | ||
|
||
func Reverse(s []string) []string { | ||
r := make([]string, len(s)) | ||
|
||
for i, j := 0, len(r)-1; i <= j; i, j = i+1, j-1 { | ||
r[i], r[j] = s[j], s[i] | ||
} | ||
|
||
return r | ||
} |
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,35 @@ | ||
package stringslice | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReverse(t *testing.T) { | ||
for i, tc := range []struct { | ||
i, e []string | ||
}{ | ||
{ | ||
i: []string{"a", "b", "c"}, | ||
e: []string{"c", "b", "a"}, | ||
}, | ||
{ | ||
i: []string{"foo"}, | ||
e: []string{"foo"}, | ||
}, | ||
{ | ||
i: []string{"foo", "bar"}, | ||
e: []string{"bar", "foo"}, | ||
}, | ||
{ | ||
i: []string{}, | ||
e: []string{}, | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("case=%d/input:%v expected:%v", i, tc.i, tc.e), func(t *testing.T) { | ||
assert.Equal(t, tc.e, Reverse(tc.i)) | ||
}) | ||
} | ||
} |