-
Notifications
You must be signed in to change notification settings - Fork 0
/
fromYamlFile.go
103 lines (88 loc) · 2.09 KB
/
fromYamlFile.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
//Convert from YAML files
//go:build !gol10n_read_compiled_only
package translate
import (
"errors"
"gopkg.in/yaml.v2"
"strconv"
"unicode/utf8"
)
type yamlMapSlice yaml.MapSlice
type yamlItem yaml.MapItem
func (ms *yamlMapSlice) getValue(paramName string) (val tpItem, ok bool) {
for _, v := range *ms {
if v.Key == paramName {
return yamlItem(v), true
}
}
return nil, false
}
func (ms *yamlMapSlice) toMap() map[string]tpItem {
retVal := make(map[string]tpItem, len(*ms))
for _, v := range *ms {
name, _ := yamlValToStr(v.Key)
retVal[name] = yamlItem{Key: name, Value: v.Value}
}
return retVal
}
func (ms *yamlMapSlice) toOrdered() []tpItem {
ret := make([]tpItem, len(*ms))
for i, v := range *ms {
ret[i] = yamlItem(v)
}
return ret
}
func (ms *yamlMapSlice) getLength() uint {
return ulen(*ms)
}
func (i yamlItem) getName() string {
return twoToOne(yamlValToStr(i.Key))
}
func (i yamlItem) getObject() (val tpMap, ok bool) {
if _val, ok := i.Value.(yamlMapSlice); !ok {
return nil, false
} else {
return &_val, true
}
}
func (i yamlItem) getString() (val string, ok bool) {
val, ok = yamlValToStr(i.Value)
return
}
func yamlValToStr(i interface{}) (val string, ok bool) {
switch v := i.(type) {
case string:
//Note: YAML.v2 returns timestamps as strings
return v, true
case int:
return strconv.FormatInt(int64(v), 10), true
case int64:
return strconv.FormatInt(v, 10), true
case uint:
return strconv.FormatUint(uint64(v), 10), true
case uint64:
return strconv.FormatUint(v, 10), true
case float64:
return strconv.FormatFloat(v, 'g', 15, 64), true
case bool:
if v {
return "Yes", true
}
return "No", true
case nil:
return "", true
default:
return returnBlankStrOnErr, false
}
}
func fromYamlFile(textStr []byte) (yamlItem, error) {
//Check for valid utf8
if !utf8.Valid(textStr) {
return yamlItem{}, errors.New("File is not utf8 valid")
}
ms := yamlMapSlice{}
if err := yaml.Unmarshal(textStr, &ms); err != nil {
return yamlItem{}, errors.New("Error parsing YAML File: " + err.Error())
}
return yamlItem{Key: "TOP", Value: ms}, nil
}