forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cue.go
177 lines (155 loc) · 4.27 KB
/
cue.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
package cuegen
import (
"fmt"
"strings"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
"github.com/benthosdev/benthos/v4/internal/docs"
)
func doComponentSpec(cs docs.ComponentSpec) (*ast.Field, error) {
f, err := doFieldSpec(cs.Config)
if err != nil {
return nil, fmt.Errorf("%s: failed to generate CUE: %w", cs.Name, err)
}
field := &ast.Field{
Label: ast.NewIdent(cs.Name),
Value: f.Value,
}
if cs.Summary != "" {
ast.AddComment(field, doComment(cs.Summary))
}
return field, nil
}
func doComment(comment string) *ast.CommentGroup {
comments := []*ast.Comment{}
for _, v := range strings.Split(strings.TrimSpace(comment), "\n") {
comments = append(comments, &ast.Comment{
Slash: token.NoPos,
Text: "// " + v,
})
}
return &ast.CommentGroup{
Position: 0,
List: comments,
}
}
func doFieldSpecs(s docs.FieldSpecs) ([]any, error) {
var fields []any
for _, fieldSpec := range s {
field, err := doFieldSpec(fieldSpec)
if err != nil {
return nil, err
}
if fieldSpec.Description != "" {
ast.AddComment(field.Label, doComment(fieldSpec.Description))
}
if fieldSpec.CheckRequired() {
fields = append(fields, field)
} else {
fields = append(fields, field.Label, token.OPTION, field.Value)
}
}
return fields, nil
}
func doFieldSpec(spec docs.FieldSpec) (*ast.Field, error) {
switch spec.Kind {
case "":
return doScalarField(spec)
case docs.KindScalar:
return doScalarField(spec)
case docs.KindArray:
f, err := doScalarField(spec)
if err != nil {
return nil, err
}
f.Value = ast.NewList(&ast.Ellipsis{Type: f.Value})
return f, nil
case docs.Kind2DArray:
f, err := doScalarField(spec)
if err != nil {
return nil, err
}
f.Value = ast.NewList(&ast.Ellipsis{Type: ast.NewList(&ast.Ellipsis{Type: f.Value})})
return f, nil
case docs.KindMap:
f, err := doScalarField(spec)
if err != nil {
return nil, err
}
f.Value = ast.NewStruct(&ast.Field{
Label: ast.NewList(ast.NewIdent("string")),
Value: f.Value,
})
return f, nil
default:
return nil, fmt.Errorf("unrecognised field kind: %s", spec.Kind)
}
}
func doScalarField(spec docs.FieldSpec) (*ast.Field, error) {
label := ast.NewIdent(spec.Name)
optionalMark := token.Blank.Pos()
var optional token.Pos
var val ast.Expr
switch spec.Type {
case docs.FieldTypeString:
val = ast.NewIdent("string")
case docs.FieldTypeInt:
val = ast.NewIdent("int")
case docs.FieldTypeFloat:
val = ast.NewIdent("float")
case docs.FieldTypeBool:
val = ast.NewIdent("bool")
case docs.FieldTypeObject:
fields := make([]any, 0, len(spec.Children))
for _, child := range spec.Children {
field, err := doFieldSpec(child)
if err != nil {
return nil, fmt.Errorf("failed to generate type for object field: %w", err)
}
if child.Description != "" {
ast.AddComment(field.Label, doComment(child.Description))
}
if child.CheckRequired() {
fields = append(fields, field)
} else {
fields = append(fields, field.Label, token.OPTION, field.Value)
}
}
val = ast.NewStruct(fields...)
case docs.FieldTypeUnknown:
val = ast.NewIdent("_")
case docs.FieldTypeInput:
// The following set of cases have unresolvable structure cycles.
// We need to mark them as optional to break the cycle...
// https://cuelang.org/docs/references/spec/#structural-cycles
val, optional = identInputDisjunction, optionalMark
case docs.FieldTypeBuffer:
val, optional = identBufferDisjunction, optionalMark
case docs.FieldTypeCache:
val, optional = identCacheDisjunction, optionalMark
case docs.FieldTypeProcessor:
val, optional = identProcessorDisjunction, optionalMark
case docs.FieldTypeRateLimit:
val, optional = identRateLimitDisjunction, optionalMark
case docs.FieldTypeOutput:
val, optional = identOutputDisjunction, optionalMark
case docs.FieldTypeMetrics:
val, optional = identMetricDisjunction, optionalMark
case docs.FieldTypeTracer:
val, optional = identTracerDisjunction, optionalMark
default:
return nil, fmt.Errorf("unrecognised field type: %s", spec.Type)
}
return &ast.Field{
Label: label,
Value: val,
Optional: optional,
}, nil
}
func interpolateIdent(ident *ast.Ident) ast.Label {
return &ast.Interpolation{Elts: []ast.Expr{
ast.NewLit(token.STRING, "("),
ident,
ast.NewLit(token.STRING, ")"),
}}
}