forked from gonvenience/ytbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restructure.go
194 lines (163 loc) · 5.67 KB
/
restructure.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
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package ytbx
import (
"sort"
yamlv3 "gopkg.in/yaml.v3"
)
// DisableRemainingKeySort disables that during restructuring of map keys, all
// unknown keys are also sorted in such a way that it improves the readability.
var DisableRemainingKeySort = false
var knownKeyOrders = [][]string{
{"name", "director_uuid", "releases", "instance_groups", "networks", "resource_pools", "compilation"},
{"name", "url", "version", "sha1"},
// Concourse (https://concourse-ci.org/pipelines.html, https://concourse-ci.org/steps.html, https://concourse-ci.org/resources.html)
{"jobs", "resources", "resource_types"},
{"name", "type", "source"},
{"get"},
{"put"},
{"task"},
// SUSE SCF role manifest (https://github.com/SUSE/scf/blob/develop/container-host-files/etc/scf/config/role-manifest.yml)
{"releases", "instance_groups", "configuration", "variables"},
{"auth", "templates"},
// Universal default #1 ... name should always be first
{"name"},
// Universal default #2 ... key should always be first
{"key"},
// Universal default #3 ... id should always be first
{"id"},
}
func lookupMap(list []string) map[string]int {
result := make(map[string]int, len(list))
for idx, entry := range list {
result[entry] = idx
}
return result
}
func lookupMapOfContentList(list []*yamlv3.Node) map[string]int {
lookup := make(map[string]int, len(list))
for i := 0; i < len(list); i += 2 {
lookup[list[i].Value] = i
}
return lookup
}
func maxDepth(node *yamlv3.Node) (max int) {
rootPath, _ := ParseGoPatchStylePathString("/")
traverseTree(
rootPath,
nil,
node,
func(p Path, _ *yamlv3.Node, _ *yamlv3.Node) {
if depth := len(p.PathElements); depth > max {
max = depth
}
},
)
return max
}
func countCommonKeys(keys []string, list []string) (counter int) {
lookup := lookupMap(keys)
for _, key := range list {
if _, ok := lookup[key]; ok {
counter++
}
}
return
}
func commonKeys(setA []string, setB []string) []string {
result, lookup := []string{}, lookupMap(setB)
for _, entry := range setA {
if _, ok := lookup[entry]; ok {
result = append(result, entry)
}
}
return result
}
func reorderKeyValuePairsInMappingNodeContent(mappingNode *yamlv3.Node, keys []string) {
// Create list with all keys, that are not part of the provided list of keys
remainingKeys, keysLookup := []string{}, lookupMap(keys)
for i := 0; i < len(mappingNode.Content); i += 2 {
key := mappingNode.Content[i].Value
if _, ok := keysLookup[key]; !ok {
remainingKeys = append(remainingKeys, key)
}
}
// Sort remaining keys by sorting long and possibly hard to read structure
// to the end of the mapping
if !DisableRemainingKeySort {
sort.Slice(remainingKeys, func(i, j int) bool {
valI, _ := getValueByKey(mappingNode, remainingKeys[i])
valJ, _ := getValueByKey(mappingNode, remainingKeys[j])
return maxDepth(valI) < maxDepth(valJ)
})
}
// Rebuild a new YAML Node list (content) key by key by using first the keys
// from the reorder list and then all remaining keys
content, contentLookup := []*yamlv3.Node{}, lookupMapOfContentList(mappingNode.Content)
for _, key := range append(keys, remainingKeys...) {
idx := contentLookup[key]
content = append(content,
mappingNode.Content[idx],
mappingNode.Content[idx+1],
)
}
mappingNode.Content = content
}
func getSuitableReorderFunction(keys []string) func(*yamlv3.Node) {
topCandidateIdx, topCandidateHits := -1, -1
for idx, candidate := range knownKeyOrders {
if count := countCommonKeys(keys, candidate); count > 0 && count > topCandidateHits {
topCandidateIdx = idx
topCandidateHits = count
}
}
if topCandidateIdx >= 0 {
return func(input *yamlv3.Node) {
reorderKeyValuePairsInMappingNodeContent(
input,
commonKeys(knownKeyOrders[topCandidateIdx], keys),
)
}
}
return nil
}
// RestructureObject takes an object and traverses down any sub elements such as
// list entries or map values to recursively call restructure itself. On YAML
// MappingNodes, it will use a look-up mechanism to decide if the order of key
// in that map need to be rearranged to meet some known established human order.
func RestructureObject(node *yamlv3.Node) {
switch node.Kind {
case yamlv3.DocumentNode:
RestructureObject(node.Content[0])
case yamlv3.MappingNode:
keys := listKeys(node)
if fn := getSuitableReorderFunction(keys); fn != nil {
fn(node)
}
// Restructure the values of the respective keys of this YAML MapSlice
for i := 0; i < len(node.Content); i += 2 {
RestructureObject(node.Content[i+1])
}
case yamlv3.SequenceNode:
for i := range node.Content {
RestructureObject(node.Content[i])
}
}
}