/
yaml_parser.go
200 lines (176 loc) 路 5.03 KB
/
yaml_parser.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/thcyron/graphs"
"gopkg.in/yaml.v2"
)
type Environments []string
type YamlData struct {
Environments Environments
Variables map[string]interface{}
}
type Config struct {
Environments Environments
Variables map[string]map[string]interface{}
}
func (config *Config) SetSameValues(variableName string, value interface{}) {
values := map[string]interface{}{}
for _, environmentName := range config.Environments {
values[environmentName] = value
}
config.Variables[variableName] = values
}
func (config *Config) SetValues(variableName string, values map[string]interface{}) {
config.Variables[variableName] = values
}
type Symbol struct {
Name string
}
type Value struct {
Name string
Value interface{}
DependsOn string
}
func NewValue(name string, value interface{}) *Value {
dependsOn := ""
if symbol, ok := value.(Symbol); ok {
value = nil
dependsOn = symbol.Name
}
return &Value{
Name: name,
Value: value,
DependsOn: dependsOn,
}
}
func (value *Value) HasDependency() bool {
return value.DependsOn != ""
}
func (value *Value) GetDependency() string {
return value.DependsOn
}
func makeSymbol(value interface{}) (symbol Symbol, err error) {
if symbolMap := value.(map[interface{}]interface{}); len(symbolMap) == 1 {
var symbolName string
var symbolValue interface{}
for k, v := range symbolMap {
symbolName = fmt.Sprintf("%v", k)
symbolValue = v
break
}
if symbolValue == nil {
return Symbol{symbolName}, nil
} else {
return symbol, errors.New(fmt.Sprintf("invalid environment symbol found"))
}
} else {
return symbol, errors.New(fmt.Sprintf("invalid environment symbol found"))
}
}
func makeEnvironmentValueMap(environments Environments, values []interface{}) (map[string]*Value, []error) {
newValues := map[string]*Value{}
errs := []error{}
if len(environments) != len(values) {
errs = append(errs, errors.New(fmt.Sprintf("array length must be %d, but %d", len(environments), len(values))))
return newValues, errs
}
for index, value := range values {
environmentName := environments[index]
if isScalar(value) {
newValues[environmentName] = NewValue(environmentName, value)
} else if isMap(value) {
symbol, err := makeSymbol(value)
if err != nil {
errs = append(errs, err)
} else {
if !environmentExists(environments, symbol.Name) {
errs = append(errs, errors.New(fmt.Sprintf("no such an environment: {%s}", symbol.Name)))
} else {
newValues[environmentName] = NewValue(environmentName, symbol)
}
}
} else {
data, _ := json.Marshal(value)
errs = append(errs, errors.New(fmt.Sprintf("variable value must be Bool, Number, String or Array: %s", string(data))))
}
}
return newValues, errs
}
func environmentExists(environments Environments, environmentName interface{}) bool {
for _, name := range environments {
if name == environmentName {
return true
}
}
return false
}
func resolveValues(values map[string]*Value) (map[string]interface{}, error) {
output := map[string]interface{}{}
// make graph
graph := graphs.NewDigraph()
for _, value := range values {
if value.HasDependency() {
graph.AddEdge(value.GetDependency(), value.Name, 0)
}
}
// sort
order, _, err := graphs.TopologicalSort(graph)
if err != nil {
return output, err
}
// create output
for _, value := range values {
if value.HasDependency() == false {
output[value.Name] = value.Value
}
}
for e := order.Front(); e != nil; e = e.Next() {
environmentName := e.Value.(string)
value := values[environmentName]
environmentValue := value.Value
if value.HasDependency() {
environmentValue = output[value.GetDependency()]
}
output[environmentName] = environmentValue
}
return output, nil
}
func makeConfig(yamlData YamlData) (*Config, []error) {
errs := []error{}
config := &Config{
Environments: yamlData.Environments,
Variables: map[string]map[string]interface{}{},
}
for variableName, variableValue := range yamlData.Variables {
if isScalar(variableValue) {
config.SetSameValues(variableName, variableValue)
} else if isSlice(variableValue) {
environmentValueMap, mapErrors := makeEnvironmentValueMap(config.Environments, variableValue.([]interface{}))
if len(mapErrors) > 0 {
for _, err := range mapErrors {
errs = append(errs, errors.New(fmt.Sprintf("%s: %s", variableName, err)))
}
} else {
resolvedValues, err := resolveValues(environmentValueMap)
if err != nil {
errs = append(errs, errors.New(fmt.Sprintf("%s: Cyclic environment symobls are detected", variableName)))
} else {
config.SetValues(variableName, resolvedValues)
}
}
} else {
errs = append(errs, errors.New(fmt.Sprintf("%s value must be type of Number, String, Boolean, null or Array", variableName)))
}
}
return config, errs
}
func parse(data string) (config *Config, errs []error) {
yamlData := YamlData{}
err := yaml.Unmarshal([]byte(data), &yamlData)
if err != nil {
return config, []error{err}
}
return makeConfig(yamlData)
}