-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
159 lines (137 loc) · 3.27 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
package action
import (
"fmt"
"reflect"
"strings"
"sync"
"gopkg.in/yaml.v3"
"github.com/launchrctl/launchr/pkg/jsonschema"
)
func yamlTypeError(s string) *yaml.TypeError {
return &yaml.TypeError{Errors: []string{s}}
}
func yamlTypeErrorLine(s string, l int, c int) *yaml.TypeError {
return yamlTypeError(fmt.Sprintf("%s, line %d, col %d", s, l, c))
}
func yamlMergeErrors(errs ...*yaml.TypeError) *yaml.TypeError {
strs := make([]string, 0, len(errs))
for _, err := range errs {
if err != nil {
strs = append(strs, err.Errors...)
}
}
return &yaml.TypeError{Errors: strs}
}
func yamlFindNodeByKey(n *yaml.Node, k string) *yaml.Node {
if n.Kind != yaml.MappingNode {
return nil
}
for i := 0; i < len(n.Content); i += 2 {
if n.Content[i].Value == k {
return n.Content[i+1]
}
}
return nil
}
func yamlNodeLineCol(n *yaml.Node, k string) (int, int) {
if o := yamlFindNodeByKey(n, k); o != nil {
return o.Line, o.Column
}
return n.Line, n.Column
}
type dupSet map[string]struct{}
var replDashes = strings.NewReplacer("-", "_")
func (d dupSet) isUnique(s string) bool {
_, ok := d[s]
_, okDashed := d[replDashes.Replace(s)]
if ok || okDashed {
return false
}
d[s] = struct{}{}
return true
}
type yamlParseDefNodes struct {
nodes map[*yaml.Node]struct{}
dups dupSet
}
type yamlGlobalParseMeta struct {
tree map[*Definition]yamlParseDefNodes
mx sync.RWMutex
}
func newGlobalYamlParseMeta() *yamlGlobalParseMeta {
return &yamlGlobalParseMeta{
tree: make(map[*Definition]yamlParseDefNodes),
}
}
func (m *yamlGlobalParseMeta) addDef(d *Definition, n *yaml.Node) {
m.mx.Lock()
defer m.mx.Unlock()
if _, ok := m.tree[d]; ok {
return
}
nodes := collectAllNodes(n)
mdef := yamlParseDefNodes{
nodes: make(map[*yaml.Node]struct{}),
dups: make(dupSet),
}
for _, child := range nodes {
mdef.nodes[child] = struct{}{}
}
m.tree[d] = mdef
}
func (m *yamlGlobalParseMeta) removeDef(d *Definition) {
m.mx.Lock()
defer m.mx.Unlock()
delete(m.tree, d)
}
func (m *yamlGlobalParseMeta) dupsByNode(n *yaml.Node) dupSet {
m.mx.RLock()
defer m.mx.RUnlock()
for _, md := range m.tree {
if _, ok := md.nodes[n]; ok {
return md.dups
}
}
return nil
}
func collectAllNodes(n *yaml.Node) []*yaml.Node {
res := make([]*yaml.Node, 0, len(n.Content)+1)
res = append(res, n)
for i := 0; i < len(n.Content); i++ {
res = append(res, collectAllNodes(n.Content[i])...)
}
return res
}
func reflectValRef(v any, n string) any {
return reflect.ValueOf(v).Elem().FieldByName(n).Addr().Interface()
}
func getDefaultByType(o *Option) interface{} {
// @todo rethink default if it's not actually defined, do not set anything.
switch o.Type {
case jsonschema.String:
return defaultVal(o.Default, "")
case jsonschema.Integer:
return defaultVal(o.Default, 0)
case jsonschema.Number:
return defaultVal(o.Default, .0)
case jsonschema.Boolean:
return defaultVal(o.Default, false)
case jsonschema.Array:
return defaultVal(o.Default, []string{})
default:
return fmt.Errorf("value for json schema type %q is not implemented", o.Type)
}
}
func defaultVal[T any](val interface{}, d T) T {
if val != nil {
return val.(T)
}
return d
}
func copyMap[K comparable, V any](m map[K]V) map[K]V {
r := make(map[K]V, len(m))
for k, v := range m {
r[k] = v
}
return r
}