Skip to content
Open
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
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ func (c *ConfReader) flagsBinding(conf interface{}) error {
return nil
}

func convertToEnvName(fullPath string) string {
return strings.ToUpper(strings.ReplaceAll(fullPath, ".", "_"))
}

type flagInfo struct {
Name string
Type reflect.Type
Expand Down
43 changes: 43 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,46 @@ func Test_InputStructErrors(t *testing.T) {
}
})
}

func Test_Map(t *testing.T) {
type MapConf struct {
MapWithAttribute map[string]string `flag:"testmap" envvar:"ENV_TEST_MAP"`
Testmap map[string]string
}

resetFlags()
expectedMap := map[string]string{"a": "1", "b": "2"}

t.Run("envVar", func(t *testing.T) {
cfg := MapConf{}
os.Setenv("TESTMAP_A", "1")
os.Setenv("TESTMAP_C", "3")
defer os.Unsetenv("TESTMAP_A")
defer os.Unsetenv("TESTMAP_C")

reader := NewConfReader("map-vars")
err := reader.Read(&cfg)
if assert.NoError(t, err) {
assert.Equal(t, expectedMap, cfg.Testmap)
}
})

t.Run("file", func(t *testing.T) {
cfg := MapConf{}
reader := NewConfReader("map").WithSearchDirs("testdata")
err := reader.Read(&cfg)
if assert.NoError(t, err) {
assert.Equal(t, expectedMap, cfg.MapWithAttribute)
}
})

t.Run("cmdArgs", func(t *testing.T) {
cfg := MapConf{}
os.Args = []string{"myapp", "--testmap", "a:1", "--testmap", "b:2", "--testmap", "c:3"}
reader := NewConfReader("map-args")
err := reader.Read(&cfg)
if assert.NoError(t, err) {
assert.Equal(t, expectedMap, cfg.TestMap)
}
})
}
41 changes: 21 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@ go 1.19

require (
github.com/go-playground/validator/v10 v10.10.1
github.com/iamolegga/enviper v1.4.0
github.com/iamolegga/enviper v1.4.2
github.com/pkg/errors v0.9.1
github.com/spf13/viper v1.11.0
github.com/stretchr/testify v1.7.1
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/creasty/defaults v1.6.0
github.com/fsnotify/fsnotify v1.7.0
github.com/spf13/pflag v1.0.5
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading