-
Notifications
You must be signed in to change notification settings - Fork 4
/
get.go
84 lines (68 loc) · 1.71 KB
/
get.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package goml
import (
"errors"
"strconv"
"strings"
"github.com/smallfish/simpleyaml"
)
func GetFromFile(file string, path string) (interface{}, error) {
yaml, err := ReadYamlFromFile(file)
if err != nil {
return nil, err
}
return Get(yaml, path)
}
func GetInMemory(file []byte, path string) (interface{}, error) {
yml, err := simpleyaml.NewYaml(file)
if err != nil {
return nil, err
}
return Get(yml, path)
}
func GetFromFileAsSimpleYaml(file string, path string) (*simpleyaml.Yaml, error) {
yaml, err := ReadYamlFromFile(file)
if err != nil {
return nil, err
}
return GetAsSimpleYaml(yaml, path)
}
func Get(yml *simpleyaml.Yaml, path string) (interface{}, error) {
val, ok := get(yml, path)
if ok == nil {
return nil, errors.New("property not found")
}
result, err := ExtractType(val)
return result, err
}
func GetAsSimpleYaml(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, error) {
val, ok := get(yml, path)
if ok == nil {
return nil, errors.New("property not found")
}
return val, nil
}
func get(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, []string) {
solvedPath := []string{}
props := strings.Split(path, ".")
for _, p := range props {
if index, err := strconv.Atoi(p); err == nil {
yml = yml.GetIndex(index)
solvedPath = append(solvedPath, strconv.Itoa(index))
continue
}
if strings.Contains(p, ":") || strings.Contains(p, "|") {
if prop, err := yml.Array(); err == nil {
index, err := returnIndexForProp(p, prop)
if err != nil {
return yml, nil
}
yml = yml.GetIndex(index)
solvedPath = append(solvedPath, strconv.Itoa(index))
continue
}
}
solvedPath = append(solvedPath, p)
yml = yml.Get(p)
}
return yml, solvedPath
}