260
|
1 |
package dotenv |
|
2 |
|
|
3 |
import ( |
|
4 |
"strings" |
|
5 |
|
|
6 |
"github.com/spf13/cast" |
|
7 |
) |
|
8 |
|
|
9 |
// flattenAndMergeMap recursively flattens the given map into a new map |
|
10 |
// Code is based on the function with the same name in tha main package. |
|
11 |
// TODO: move it to a common place |
|
12 |
func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{}, prefix string, delimiter string) map[string]interface{} { |
|
13 |
if shadow != nil && prefix != "" && shadow[prefix] != nil { |
|
14 |
// prefix is shadowed => nothing more to flatten |
|
15 |
return shadow |
|
16 |
} |
|
17 |
if shadow == nil { |
|
18 |
shadow = make(map[string]interface{}) |
|
19 |
} |
|
20 |
|
|
21 |
var m2 map[string]interface{} |
|
22 |
if prefix != "" { |
|
23 |
prefix += delimiter |
|
24 |
} |
|
25 |
for k, val := range m { |
|
26 |
fullKey := prefix + k |
|
27 |
switch val.(type) { |
|
28 |
case map[string]interface{}: |
|
29 |
m2 = val.(map[string]interface{}) |
|
30 |
case map[interface{}]interface{}: |
|
31 |
m2 = cast.ToStringMap(val) |
|
32 |
default: |
|
33 |
// immediate value |
|
34 |
shadow[strings.ToLower(fullKey)] = val |
|
35 |
continue |
|
36 |
} |
|
37 |
// recursively merge to shadow map |
|
38 |
shadow = flattenAndMergeMap(shadow, m2, fullKey, delimiter) |
|
39 |
} |
|
40 |
return shadow |
|
41 |
} |