-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
82 additions
and
38 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,46 @@ | ||
package xload | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cast" | ||
) | ||
|
||
// MapLoader loads values from a map. | ||
// | ||
// Can be used with xload.FlattenMap as an intermediate format | ||
// when loading from various sources. | ||
type MapLoader map[string]string | ||
|
||
// Load fetches the value from the map. | ||
func (m MapLoader) Load(ctx context.Context, key string) (string, error) { | ||
value, ok := m[key] | ||
if !ok { | ||
return "", nil | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// FlattenMap flattens a map[string]interface{} into a map[string]string. | ||
// Nested maps are flattened using given separator. | ||
func FlattenMap(m map[string]interface{}, sep string) map[string]string { | ||
return flatten(m, "", sep) | ||
} | ||
|
||
func flatten(m map[string]interface{}, prefix string, sep string) map[string]string { | ||
flattened := make(map[string]string) | ||
|
||
for key, value := range m { | ||
switch value := value.(type) { | ||
case map[string]interface{}: | ||
for k, v := range flatten(value, key+sep, sep) { | ||
flattened[prefix+k] = v | ||
} | ||
default: | ||
flattened[prefix+key] = cast.ToString(value) | ||
} | ||
} | ||
|
||
return flattened | ||
} |
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,32 @@ | ||
package xload | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFlattenMap(t *testing.T) { | ||
input := map[string]interface{}{ | ||
"NAME": "xload", | ||
"VERSION": 1.1, | ||
"AUTHOR": map[string]interface{}{ | ||
"NAME": "gojek", | ||
"EMAIL": "[email protected]", | ||
"ADDRESS": map[string]interface{}{ | ||
"CITY": "Bombay", | ||
}, | ||
}, | ||
} | ||
|
||
want := map[string]string{ | ||
"NAME": "xload", | ||
"VERSION": "1.1", | ||
"AUTHOR_NAME": "gojek", | ||
"AUTHOR_EMAIL": "[email protected]", | ||
"AUTHOR_ADDRESS_CITY": "Bombay", | ||
} | ||
|
||
got := FlattenMap(input, "_") | ||
assert.EqualValues(t, want, got) | ||
} |
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