forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
77 lines (66 loc) · 1.61 KB
/
component.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
package cuegen
import (
"fmt"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
"github.com/benthosdev/benthos/v4/internal/docs"
)
type componentOptions struct {
collectionIdent *ast.Ident
disjunctionIdent *ast.Ident
canLabel bool
canPreProcess bool
}
func doComponents(specs []docs.ComponentSpec, opts *componentOptions) ([]ast.Decl, error) {
fields := make([]any, 0, len(specs))
for _, v := range specs {
field, err := doComponentSpec(v)
if err != nil {
return nil, fmt.Errorf("failed to generate cue type for component: %s: %w", v.Name, err)
}
fields = append(fields, field)
}
decls := []ast.Decl{
&ast.Field{
Label: opts.collectionIdent,
Value: ast.NewStruct(fields...),
},
&ast.Field{
Label: opts.disjunctionIdent,
Value: ast.NewCall(ast.NewIdent("or"), ast.NewList(&ast.Comprehension{
Clauses: []ast.Clause{
&ast.ForClause{
Key: ast.NewIdent("name"),
Value: ast.NewIdent("config"),
Source: opts.collectionIdent,
},
},
Value: ast.NewStruct(&ast.Field{
Label: interpolateIdent(ast.NewIdent("name")),
Value: ast.NewIdent("config"),
}),
})),
},
}
if opts.canLabel {
decls = append(decls, &ast.Field{
Label: opts.disjunctionIdent,
Value: ast.NewStruct(
ast.NewIdent("label"),
token.OPTION,
ast.NewIdent("string"),
),
})
}
if opts.canPreProcess {
decls = append(decls, &ast.Field{
Label: opts.disjunctionIdent,
Value: ast.NewStruct(
ast.NewIdent("processors"),
token.OPTION,
ast.NewList(&ast.Ellipsis{Type: ast.NewIdent("#Processor")}),
),
})
}
return decls, nil
}