-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
yamltools.go
52 lines (47 loc) · 1.23 KB
/
yamltools.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
package util
import (
"fmt"
"gopkg.in/yaml.v3"
"os"
)
// YamlFileToMap() reads the named file into a map[string]interface{}
func YamlFileToMap(fname string) (map[string]interface{}, error) {
file, err2 := os.ReadFile(fname)
contents, err := file, err2
if err != nil {
return nil, fmt.Errorf("unable to read file %s (%v)", fname, err)
}
itemMap := make(map[string]interface{})
err = yaml.Unmarshal(contents, &itemMap)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal %s: %v", contents, err)
}
return itemMap, nil
}
// YamlToDict turns random yaml-based interface into a map[string]interface{}
func YamlToDict(topm interface{}) (map[string]interface{}, error) {
res := make(map[string]interface{})
var err error
switch topm.(type) {
case map[string]interface{}:
for yk, v := range topm.(map[string]interface{}) {
switch v.(type) {
case string:
res[yk] = v
case map[string]interface{}:
res[yk], err = YamlToDict(v)
case interface{}:
res[yk] = v
default:
Warning("Configuration has invalid type '%T' for '%s'", v, yk)
continue
}
if err != nil {
return nil, err
}
}
default:
return nil, fmt.Errorf("yamlToDict: type %T not handled (%v)", topm, topm)
}
return res, nil
}