-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
dotnet.go
58 lines (52 loc) · 1.46 KB
/
dotnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package util
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
func MapAppsettingsToEnv(in *string) (map[string]string, error) {
if in == nil {
return map[string]string{}, errors.New("Nil input")
}
if in != nil && *in == "" {
return map[string]string{}, errors.New("Empty input")
}
envList := map[string]string{}
root := map[string]interface{}{}
// invalid Jason, bad Jason, go home, you are drunk!
if err := json.Unmarshal([]byte(*in), &root); err != nil {
return map[string]string{}, err
}
parseWithPrefix(envList, "", root)
return envList, nil
}
func parseWithPrefix(envList map[string]string, prefix string, leaf interface{}) {
switch leaf := leaf.(type) {
case map[string]interface{}:
prefixToAdd := ""
if prefix != "" {
prefixToAdd = prefix + "__"
}
for key, value := range leaf {
switch value := value.(type) {
case []interface{}:
for index, v := range value {
parseWithPrefix(envList, fmt.Sprintf("%s%s__%v", prefixToAdd, key, index), v)
}
case map[string]interface{}:
parseWithPrefix(envList, prefixToAdd+key, value)
case string, int:
envList[prefixToAdd+key] = value.(string)
case bool:
envList[prefixToAdd+key] = fmt.Sprintf("%v", value)
case float32, float64:
envList[prefixToAdd+key] = fmt.Sprintf("%v", value.(float64))
default:
log.Info().Interface("value", value).Msg("Unexpected case")
}
}
default:
envList[prefix] = leaf.(string)
}
}