Skip to content

Commit

Permalink
* deprecated current ini section-key generation: flatten -> map
Browse files Browse the repository at this point in the history
  • Loading branch information
bogcon committed Sep 30, 2022
1 parent 982b929 commit 0fede3d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
2 changes: 1 addition & 1 deletion loader_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,6 @@ func ExampleFileLoader() {
// 4
// 4
// 4
// 4
// 6
// 4
}
34 changes: 24 additions & 10 deletions loader_ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ type IniFileLoader struct {
filePath string
// loadOpts are the original package parse options.
loadOpts ini.LoadOptions
// keyFunc is a function that returns the final configuration key name
// keyFunc is a function that returns a flatten key name
// based on a section and a key under it.
// Deprecated: to be removed in a future release.
// [FlatternLoader] can be used to achieve what this function does.
keyFunc func(section, key string) string
}

Expand Down Expand Up @@ -50,9 +52,21 @@ func (loader IniFileLoader) Load() (map[string]interface{}, error) {
configMap := make(map[string]interface{})
sections := cfg.Sections()
for _, section := range sections {
for _, key := range section.Keys() {
sectionKeys := section.Keys()
if section.Name() != ini.DefaultSection {
configMap[section.Name()] = make(map[string]interface{}, len(sectionKeys))
}
for _, key := range sectionKeys {
// deprecation, to be removed - start
keyName := loader.keyFunc(section.Name(), key.Name())
configMap[keyName] = key.Value()
// deprecation, to be removed - stop

if section.Name() == ini.DefaultSection {
configMap[key.Name()] = key.Value()
} else {
configMap[section.Name()].(map[string]interface{})[key.Name()] = key.Value()
}
}
}

Expand All @@ -74,14 +88,12 @@ func IniFileLoaderWithLoadOptions(iniLoadOpts ini.LoadOptions) IniFileLoaderOpti
// IniFileLoaderWithSectionKeyFunc sets given configuration key name provider based
// on a key and the section it belongs to.
//
// By default a function that returns the same key for default section, and <section/key>
// for a different section from default is used.
//
// You may want for example to provide a custom function that ignores the section:
//
// xconf.IniFileLoaderWithSectionKeyFunc(func(_, key string) string {
// return key
// })
// Deprecated: do not use it anymore, it will be removed in a future release!
// You can use [FlattenLoader] wrapper to get flatten keys when they belong to a section
// different from 'default' section, if needed.
// Neither current usage of accessing keys should be avoided.
// Example: for a key "bar" in section "[foo]"" deprecated implementation gives access to a key "foo/bar".
// This kind of key generation will be removed. You should use new implementation output in the form of map ({"foo": {"bar": ...}}).
func IniFileLoaderWithSectionKeyFunc(keyFunc func(section, key string) string) IniFileLoaderOption {
return func(loader *IniFileLoader) {
loader.keyFunc = keyFunc
Expand All @@ -97,6 +109,8 @@ func IniFileLoaderWithSectionKeyFunc(keyFunc func(section, key string) string) I
// year=2022
//
// it will produce "foo" and "time/year" for the 2 above keys.
//
// Deprecated: to be removed along with IniFileLoaderWithSectionKeyFunc logic.
var defaultIniKeyFunc = func(section, key string) string {
if section == ini.DefaultSection {
return key
Expand Down
44 changes: 35 additions & 9 deletions loader_ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ import (
)

var iniConfigMap = map[string]interface{}{
"ini_foo": "bar",
"ini_foo": "bar",
// deprecation, to be removed - start
"time/ini_year": "2022",
"temperature/ini_celsius": "37.5",
"temperature/ini_fahrenheit": "99.5",
// deprecation, to be removed - stop
"time": map[string]interface{}{
"ini_year": "2022",
},
"temperature": map[string]interface{}{
"ini_celsius": "37.5",
"ini_fahrenheit": "99.5",
},
}

const iniFilePath = "testdata/config.ini"
Expand Down Expand Up @@ -104,6 +113,7 @@ func testIniFileLoaderWithCustomIniLoadOptions(t *testing.T) {
assertEqual(t, 0, len(config))
}

// Deprecated: to be removed.
func testIniFileLoaderWithCustomKeyFuncOption(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -131,6 +141,14 @@ func testIniFileLoaderWithCustomKeyFuncOption(t *testing.T) {
"INI_YEAR": "2022",
"INI_CELSIUS": "37.5",
"INI_FAHRENHEIT": "99.5",
"ini_foo": "bar",
"time": map[string]interface{}{
"ini_year": "2022",
},
"temperature": map[string]interface{}{
"ini_celsius": "37.5",
"ini_fahrenheit": "99.5",
},
},
config,
)
Expand Down Expand Up @@ -163,10 +181,19 @@ func testIniFileLoaderReturnsSafeMutableConfigMap(t *testing.T) {
assertEqual(
t,
map[string]interface{}{
"ini_foo": "bar",
"ini_foo": "bar",
// deprecation, to be removed - start
"time/ini_year": "2022",
"temperature/ini_celsius": "37.5",
"temperature/ini_fahrenheit": "99.5",
// deprecation, to be removed - stop
"time": map[string]interface{}{
"ini_year": "2022",
},
"temperature": map[string]interface{}{
"ini_celsius": "37.5",
"ini_fahrenheit": "99.5",
},
},
iniConfigMap,
)
Expand All @@ -193,13 +220,12 @@ func ExampleIniFileLoader() {
if err != nil {
panic(err)
}
for key, value := range configMap {
fmt.Println(key+":", value)
}
fmt.Println(configMap["ini_foo"])
fmt.Println(configMap["temperature"].(map[string]interface{})["ini_celsius"])
fmt.Println(configMap["temperature"].(map[string]interface{})["ini_fahrenheit"])

// Unordered output:
// ini_foo: bar
// time/ini_year: 2022
// temperature/ini_celsius: 37.5
// temperature/ini_fahrenheit: 99.5
// bar
// 37.5
// 99.5
}

0 comments on commit 0fede3d

Please sign in to comment.