forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
44 lines (39 loc) · 1.02 KB
/
config.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
// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package config
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/tsuru/config"
)
func ConvertEntries(initial interface{}) interface{} {
switch initialType := initial.(type) {
case []interface{}:
for i := range initialType {
initialType[i] = ConvertEntries(initialType[i])
}
return initialType
case map[interface{}]interface{}:
output := make(map[string]interface{}, len(initialType))
for k, v := range initialType {
output[fmt.Sprintf("%v", k)] = ConvertEntries(v)
}
return output
default:
return initialType
}
}
func UnmarshalConfig(key string, result interface{}) error {
data, err := config.Get(key)
if err != nil {
return errors.WithStack(err)
}
data = ConvertEntries(data)
jsonData, err := json.Marshal(data)
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(json.Unmarshal(jsonData, result))
}