-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
152 lines (140 loc) · 3.96 KB
/
generator.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
package opsfile
import (
"fmt"
"github.com/cjnosal/manifer/pkg/file"
"github.com/cjnosal/manifer/pkg/yaml"
"github.com/cppforlife/go-patch/patch"
y "gopkg.in/yaml.v3"
"strings"
)
type opFileGenerator struct {
yaml yaml.YamlAccess
}
func (i *opFileGenerator) generateSnippets(schema *yaml.SchemaNode) ([]*file.TaggedBytes, error) {
ops := []*file.TaggedBytes{}
if schema.Kind == y.MappingNode {
for k, v := range schema.Contents {
if v.Kind == y.ScalarNode {
opPath := fmt.Sprintf("/%s?", k)
file := fmt.Sprintf("./set_%s.yml", k)
op, err := i.generate(opPath, file, "", k, v)
if err != nil {
return nil, err
}
ops = append(ops, op)
} else {
p := fmt.Sprintf("/%s", k)
d := "."
o, err := i.visit(p, d, "", k, v)
if err != nil {
return nil, err
}
ops = append(ops, o...)
}
}
} else { // sequence
element := schema.Contents["!!element"]
appendpath := "/-"
elementName := "root"
file := fmt.Sprintf("./add_%s.yml", elementName)
appendop, err := i.generate(appendpath, file, "", elementName, element)
if err != nil {
return nil, err
}
ops = append(ops, appendop)
if element != nil {
indexvar := fmt.Sprintf("((%s_index))", elementName)
editpath := fmt.Sprintf("/%s", indexvar)
editops, err := i.visit(editpath, ".", elementName, elementName, element)
if err != nil {
return nil, err
}
ops = append(ops, editops...)
}
}
return ops, nil
}
func (i *opFileGenerator) visit(opPath string, dir string, parent string, name string, node *yaml.SchemaNode) ([]*file.TaggedBytes, error) {
ops := []*file.TaggedBytes{}
if node.Kind == y.SequenceNode {
element := node.Contents["!!element"]
appendpath := fmt.Sprintf("%s?/%s", opPath, "-")
elementName := naiveSingular(name)
file := fmt.Sprintf("%s/add_%s.yml", dir, elementName)
appendop, err := i.generate(appendpath, file, parent, elementName, element)
if err != nil {
return nil, err
}
ops = append(ops, appendop)
if element != nil {
indexvar := fmt.Sprintf("((%s_index))", elementName)
editpath := fmt.Sprintf("%s/%s", opPath, indexvar)
editops, err := i.visit(editpath, dir, elementName, elementName, element)
if err != nil {
return nil, err
}
ops = append(ops, editops...)
}
} else if node.Kind == y.MappingNode {
optionalPath := opPath
if !strings.HasSuffix(opPath, "))") {
optionalPath = fmt.Sprintf("%s?", opPath)
}
file := fmt.Sprintf("%s/set_%s.yml", dir, name)
op, err := i.generate(optionalPath, file, parent, name, node)
if err != nil {
return nil, err
}
ops = append(ops, op)
// recurse sequence/mapping nodes
for k, v := range node.Contents {
p := fmt.Sprintf("%s/%s", opPath, k)
d := fmt.Sprintf("%s/%s", dir, name)
o, err := i.visit(p, d, name, k, v)
if err != nil {
return nil, err
}
ops = append(ops, o...)
}
}
return ops, nil
}
func (i *opFileGenerator) generate(opPath string, filePath string, parent string, name string, node *yaml.SchemaNode) (*file.TaggedBytes, error) {
var value interface{}
// generate opsfile that inserts all scalars into a mapping
// opsfile to insert key=value for empty mapping node
if node == nil || node.Kind == y.ScalarNode {
var variable string
if parent != "" {
variable = fmt.Sprintf("((%s_%s))", parent, name)
} else {
variable = fmt.Sprintf("((%s))", name)
}
value = &variable
} else {
values := &map[string]string{}
for k, v := range node.Contents {
if v.Kind == y.ScalarNode {
variable := fmt.Sprintf("((%s_%s))", name, k)
(*values)[k] = variable
}
}
value = values
}
opdef := patch.OpDefinition{
Path: &opPath,
Type: "replace",
Value: &value,
}
bytes, err := i.yaml.Marshal([]patch.OpDefinition{opdef})
if err != nil {
return nil, fmt.Errorf("%w\n marshaling %s", err, opPath)
}
return &file.TaggedBytes{Bytes: bytes, Tag: filePath}, nil
}
func naiveSingular(name string) string {
if strings.HasSuffix(name, "s") {
return name[:len(name)-1]
}
return name
}