-
Notifications
You must be signed in to change notification settings - Fork 329
/
afunc.go
235 lines (191 loc) · 4.77 KB
/
afunc.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package codegen
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
"text/template"
. "github.com/cortezaproject/corteza/server/pkg/y7s"
"gopkg.in/yaml.v3"
)
type (
// definitions are in one file
aFuncDefs struct {
Package string
Name string
Source string
Prefix string
outputDir string
// List of imports
// Used only by generated file and not pre-generated-user-file
Imports []string
Functions aFunctionSet
}
aFunctionSet []*aFuncDef
aFuncDef struct {
Name string
Kind string
Labels map[string]string
Meta *aFuncMetaDef
Params aFuncParamSet
Results aFuncResultSet
}
aFuncParamSet []*aFuncParamDef
aFuncResultSet []*aFuncResultDef
aFuncParamDef struct {
Name string
Required bool
IsArray bool `yaml:"isArray"`
Types []*aFuncParamTypeVarDef
Meta *aFuncParamMetaDef
}
aFuncParamTypeVarDef struct {
WorkflowType string `yaml:"wf"`
GoType string `yaml:"go"`
Suffix string
}
aFuncResultDef struct {
Name string
IsArray bool `yaml:"isArray"`
WorkflowType string `yaml:"wf"`
GoType string `yaml:"go"`
Meta *aFuncParamMetaDef
}
aFuncMetaDef struct {
Short string
Description string
Visual map[string]interface{}
}
aFuncParamMetaDef struct {
Label string
Description string
Visual map[string]interface{}
}
)
func procAutomationFunctions(mm ...string) (dd []*aFuncDefs, err error) {
for _, m := range mm {
f, err := os.Open(m)
if err != nil {
return nil, fmt.Errorf("%s read failed: %w", m, err)
}
defer f.Close()
var (
d = &aFuncDefs{
Package: "automation",
Source: m,
Name: path.Base(m),
outputDir: path.Dir(m),
}
)
d.Name = d.Name[:len(d.Name)-13]
if err := yaml.NewDecoder(f).Decode(d); err != nil {
return nil, fmt.Errorf("could not decode %s: %w", m, err)
}
dd = append(dd, d)
}
return
}
func (set *aFunctionSet) UnmarshalYAML(n *yaml.Node) error {
return Each(n, func(k *yaml.Node, v *yaml.Node) (err error) {
def := &aFuncDef{Name: k.Value}
if err = v.Decode(&def); err != nil {
return err
}
if def.Kind == "" {
def.Kind = "function"
}
*set = append(*set, def)
return nil
})
}
func (set *aFuncParamSet) UnmarshalYAML(n *yaml.Node) error {
return Each(n, func(k *yaml.Node, v *yaml.Node) (err error) {
def := aFuncParamDef{}
if k != nil {
def.Name = k.Value
}
*set = append(*set, &def)
return v.Decode(&def)
})
}
func (set *aFuncResultSet) UnmarshalYAML(n *yaml.Node) error {
return Each(n, func(k *yaml.Node, v *yaml.Node) (err error) {
def := aFuncResultDef{}
if k != nil {
def.Name = k.Value
}
*set = append(*set, &def)
return v.Decode(&def)
})
}
func expandAutomationFunctionTypes(ff []*aFuncDefs, tt []*exprTypesDef) {
// index of all known types
ti := make(map[string]*exprTypeDef)
for _, t := range tt {
for typ, d := range t.Types {
ti[typ] = d
}
}
for _, f := range ff {
for _, fn := range f.Functions {
for _, p := range fn.Params {
for _, t := range p.Types {
if ti[t.WorkflowType] == nil {
fmt.Printf("%s/%s(): unknown type %q used for param %q\n", f.Prefix, fn.Name, t.WorkflowType, p.Name)
continue
}
if t.GoType == "" {
t.GoType = ti[t.WorkflowType].As
if "[]TypedValue" == t.GoType {
t.GoType = "[]expr.TypedValue"
}
}
if t.Suffix == "" && len(p.Types) > 1 {
t.Suffix = t.WorkflowType
}
}
}
for _, r := range fn.Results {
if ti[r.WorkflowType] == nil {
fmt.Printf("%s/%s(): unknown type %q used for result %q\n", f.Prefix, fn.Name, r.WorkflowType, r.Name)
continue
}
if r.GoType == "" {
r.GoType = ti[r.WorkflowType].As
if "[]TypedValue" == r.GoType {
r.GoType = "[]expr.TypedValue"
}
}
}
}
}
}
func genAutomationFunctions(tpl *template.Template, dd ...*aFuncDefs) (err error) {
var (
// Will only be generated if file does not exist previously
tplAFuncGen = tpl.Lookup("afunc.gen.go.tpl")
dst string
)
for _, d := range dd {
// Generic code, actions for every resource goes to a separated file
dst = path.Join(d.outputDir, path.Base(d.Source)[:strings.LastIndex(path.Base(d.Source), ".")]+".gen.go")
json.NewEncoder(os.Stdout).SetIndent("", " ")
err = goTemplate(dst, tplAFuncGen, d)
if err != nil {
return
}
}
return nil
}
// genAutomationFunctionDocs look for afunc.gen.adoc.tpl and generates afunc.gen.adoc from it
func genAutomationFunctionDocs(tpl *template.Template, docsPath string, dd ...*aFuncDefs) (err error) {
var (
typeGenAdoc = tpl.Lookup("afunc.gen.adoc.tpl")
dst string
)
dst = path.Join(docsPath, "afunc.gen.adoc")
return plainTemplate(dst, typeGenAdoc, map[string]interface{}{
"Definitions": dd,
})
}