-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
160 lines (139 loc) · 3.03 KB
/
utils.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package goml
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/smallfish/simpleyaml"
"gopkg.in/yaml.v2"
)
func ExtractType(value *simpleyaml.Yaml) (interface{}, error) {
if v, err := value.String(); err == nil {
return v, nil
}
if v, err := value.Bool(); err == nil {
return strconv.FormatBool(v), nil
}
if v, err := value.Int(); err == nil {
return strconv.Itoa(v), nil
}
if v, err := value.Float(); err == nil {
return fmt.Sprint(v), nil
}
if v, err := value.Array(); err == nil {
strSl := []string{}
for _, val := range v {
tmp := extractArrayType(val)
strSl = append(strSl, tmp)
}
str := strings.Join(strSl, ",")
return str, nil
}
if v, err := value.Map(); err == nil {
return v, nil
}
return nil, errors.New("property not found")
}
func extractArrayType(value interface{}) string {
switch t := value.(type) {
default:
fmt.Fprintf(os.Stderr, "unexpected type %T\n", t)
case string:
return value.(string)
case bool:
return strconv.FormatBool(value.(bool))
case int:
return strconv.Itoa(value.(int))
case float64:
return fmt.Sprint(value.(float64))
}
return ""
}
func convertValueType(val interface{}) interface{} {
switch val.(type) {
default:
return val
case string:
str := val.(string)
if value, err := strconv.Atoi(str); err == nil {
return value
}
if value, err := strconv.ParseBool(str); err == nil {
return value
}
}
return val
}
func WriteYaml(yml *simpleyaml.Yaml, file string) error {
var (
err error
goml interface{}
)
switch {
case yml.IsMap():
goml, err = yml.Map()
case yml.IsArray():
goml, err = yml.Array()
default:
err = errors.New("Unexpected yml structure after manipulation")
}
if err != nil {
return err
}
gomlSave, err := yaml.Marshal(goml)
if err != nil {
return err
}
return ioutil.WriteFile(file, gomlSave, 0644)
}
func ReadYaml(yaml []byte) (*simpleyaml.Yaml, error) {
yml, err := simpleyaml.NewYaml(yaml)
if err != nil {
return nil, err
}
return yml, nil
}
func ReadYamlFromFile(filename string) (*simpleyaml.Yaml, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
if len(file) == 0 {
file = append(file, []byte(`{}`)...)
}
return simpleyaml.NewYaml(file)
}
func returnIndexForProp(propName string, array []interface{}) (int, error) {
keyVal := strings.Split(propName, ":")
if len(keyVal) < 2 {
keyVal = strings.Split(propName, "|")
}
key, val := keyVal[0], keyVal[1]
for i, _ := range array {
if key == "" {
check := array[i]
if check == val {
return i, nil
}
} else {
check := array[i].(map[interface{}]interface{})
if check[key] == val {
return i, nil
}
}
}
return 0, errors.New("property not found")
}
func createArrayEntry(propName string, array *[]interface{}) int {
keyVal := strings.Split(propName, ":")
if len(keyVal) < 2 {
keyVal = strings.Split(propName, "|")
}
key, val := keyVal[0], keyVal[1]
m := make(map[interface{}]interface{})
m[key] = val
*array = append(*array, m)
return len(*array) - 1
}