-
Notifications
You must be signed in to change notification settings - Fork 784
/
component.go
336 lines (279 loc) · 7.95 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package docs
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"text/template"
"github.com/Jeffail/benthos/v3/lib/util/config"
"gopkg.in/yaml.v3"
)
// AnnotatedExample is an isolated example for a component.
type AnnotatedExample struct {
// A title for the example.
Title string `json:"title"`
// Summary of the example.
Summary string `json:"summary"`
// A config snippet to show.
Config string `json:"config"`
}
// Status of a component.
type Status string
// Component statuses.
var (
StatusStable Status = "stable"
StatusBeta Status = "beta"
StatusExperimental Status = "experimental"
StatusDeprecated Status = "deprecated"
)
// Type of a component.
type Type string
// Component types.
var (
TypeBuffer Type = "buffer"
TypeCache Type = "cache"
TypeInput Type = "input"
TypeMetrics Type = "metrics"
TypeOutput Type = "output"
TypeProcessor Type = "processor"
TypeRateLimit Type = "rate_limit"
TypeTracer Type = "tracer"
)
// Types returns a slice containing all component types.
func Types() []Type {
return []Type{
TypeBuffer,
TypeCache,
TypeInput,
TypeMetrics,
TypeOutput,
TypeProcessor,
TypeRateLimit,
TypeTracer,
}
}
// ComponentSpec describes a Benthos component.
type ComponentSpec struct {
// Name of the component
Name string `json:"name"`
// Type of the component (input, output, etc)
Type Type `json:"type"`
// The status of the component.
Status Status `json:"status"`
// Plugin is true for all plugin components.
Plugin bool `json:"plugin"`
// Summary of the component (in markdown, must be short).
Summary string `json:"summary"`
// Description of the component (in markdown).
Description string `json:"description"`
// Categories that describe the purpose of the component.
Categories []string `json:"categories"`
// Footnotes of the component (in markdown).
Footnotes string `json:"footnotes"`
// Examples demonstrating use cases for the component.
Examples []AnnotatedExample `json:"examples,omitempty"`
// A summary of each field in the component configuration.
Config FieldSpec `json:"config"`
// Version is the Benthos version this component was introduced.
Version string `json:"version,omitempty"`
}
type componentContext struct {
Name string
Type string
FrontMatterSummary string
Summary string
Description string
Categories string
Examples []AnnotatedExample
Fields []FieldSpecCtx
Footnotes string
CommonConfig string
AdvancedConfig string
Status string
Version string
}
var componentTemplate = FieldsTemplate(true) + `---
title: {{.Name}}
type: {{.Type}}
status: {{.Status}}
{{if gt (len .FrontMatterSummary) 0 -}}
description: "{{.FrontMatterSummary}}"
{{end -}}
{{if gt (len .Categories) 0 -}}
categories: {{.Categories}}
{{end -}}
---
<!--
THIS FILE IS AUTOGENERATED!
To make changes please edit the contents of:
lib/{{.Type}}/{{.Name}}.go
-->
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
{{if eq .Status "beta" -}}
:::caution BETA
This component is mostly stable but breaking changes could still be made outside of major version releases if a fundamental problem with the component is found.
:::
{{end -}}
{{if eq .Status "experimental" -}}
:::caution EXPERIMENTAL
This component is experimental and therefore subject to change or removal outside of major version releases.
:::
{{end -}}
{{if eq .Status "deprecated" -}}
:::warning DEPRECATED
This component is deprecated and will be removed in the next major version release. Please consider moving onto [alternative components](#alternatives).
:::
{{end -}}
{{if gt (len .Summary) 0 -}}
{{.Summary}}
{{end -}}{{if gt (len .Version) 0}}
Introduced in version {{.Version}}.
{{end}}
{{if eq .CommonConfig .AdvancedConfig -}}
` + "```yaml" + `
# Config fields, showing default values
{{.CommonConfig -}}
` + "```" + `
{{else}}
<Tabs defaultValue="common" values={{"{"}}[
{ label: 'Common', value: 'common', },
{ label: 'Advanced', value: 'advanced', },
]{{"}"}}>
<TabItem value="common">
` + "```yaml" + `
# Common config fields, showing default values
{{.CommonConfig -}}
` + "```" + `
</TabItem>
<TabItem value="advanced">
` + "```yaml" + `
# All config fields, showing default values
{{.AdvancedConfig -}}
` + "```" + `
</TabItem>
</Tabs>
{{end -}}
{{if gt (len .Description) 0}}
{{.Description}}
{{end}}
{{if and (le (len .Fields) 4) (gt (len .Fields) 0) -}}
## Fields
{{template "field_docs" . -}}
{{end -}}
{{if gt (len .Examples) 0 -}}
## Examples
<Tabs defaultValue="{{ (index .Examples 0).Title }}" values={{"{"}}[
{{range $i, $example := .Examples -}}
{ label: '{{$example.Title}}', value: '{{$example.Title}}', },
{{end -}}
]{{"}"}}>
{{range $i, $example := .Examples -}}
<TabItem value="{{$example.Title}}">
{{if gt (len $example.Summary) 0 -}}
{{$example.Summary}}
{{end}}
{{if gt (len $example.Config) 0 -}}
` + "```yaml" + `{{$example.Config}}` + "```" + `
{{end}}
</TabItem>
{{end -}}
</Tabs>
{{end -}}
{{if gt (len .Fields) 4 -}}
## Fields
{{template "field_docs" . -}}
{{end -}}
{{if gt (len .Footnotes) 0 -}}
{{.Footnotes}}
{{end}}
`
func createOrderedConfig(t Type, rawExample interface{}, filter FieldFilter) (*yaml.Node, error) {
var newNode yaml.Node
if err := newNode.Encode(rawExample); err != nil {
return nil, err
}
if err := SanitiseYAML(t, &newNode, SanitiseConfig{
RemoveTypeField: true,
Filter: filter,
ForExample: true,
}); err != nil {
return nil, err
}
return &newNode, nil
}
func genExampleConfigs(t Type, nest bool, fullConfigExample interface{}) (commonConfigStr, advConfigStr string, err error) {
var advConfig, commonConfig interface{}
if advConfig, err = createOrderedConfig(t, fullConfigExample, func(f FieldSpec) bool {
return !f.IsDeprecated
}); err != nil {
panic(err)
}
if commonConfig, err = createOrderedConfig(t, fullConfigExample, func(f FieldSpec) bool {
return !f.IsAdvanced && !f.IsDeprecated
}); err != nil {
panic(err)
}
if nest {
advConfig = map[string]interface{}{string(t): advConfig}
commonConfig = map[string]interface{}{string(t): commonConfig}
}
advancedConfigBytes, err := config.MarshalYAML(advConfig)
if err != nil {
panic(err)
}
commonConfigBytes, err := config.MarshalYAML(commonConfig)
if err != nil {
panic(err)
}
return string(commonConfigBytes), string(advancedConfigBytes), nil
}
// AsMarkdown renders the spec of a component, along with a full configuration
// example, into a markdown document.
func (c *ComponentSpec) AsMarkdown(nest bool, fullConfigExample interface{}) ([]byte, error) {
if strings.Contains(c.Summary, "\n\n") {
return nil, fmt.Errorf("%v component '%v' has a summary containing empty lines", c.Type, c.Name)
}
ctx := componentContext{
Name: c.Name,
Type: string(c.Type),
Summary: c.Summary,
Description: c.Description,
Examples: c.Examples,
Footnotes: c.Footnotes,
Status: string(c.Status),
Version: c.Version,
}
if ctx.Status == "" {
ctx.Status = string(StatusStable)
}
if len(c.Categories) > 0 {
cats, _ := json.Marshal(c.Categories)
ctx.Categories = string(cats)
}
var err error
if ctx.CommonConfig, ctx.AdvancedConfig, err = genExampleConfigs(c.Type, nest, fullConfigExample); err != nil {
return nil, err
}
if len(c.Description) > 0 && c.Description[0] == '\n' {
ctx.Description = c.Description[1:]
}
if len(c.Footnotes) > 0 && c.Footnotes[0] == '\n' {
ctx.Footnotes = c.Footnotes[1:]
}
flattenedFields := c.Config.FlattenChildrenForDocs()
for _, v := range flattenedFields {
if v.Spec.Kind == KindMap {
v.Spec.Type = "object"
} else if v.Spec.Kind == KindArray {
v.Spec.Type = "array"
} else if v.Spec.Kind == Kind2DArray {
v.Spec.Type = "two-dimensional array"
}
v.Spec.Kind = KindScalar
ctx.Fields = append(ctx.Fields, v)
}
var buf bytes.Buffer
err = template.Must(template.New("component").Parse(componentTemplate)).Execute(&buf, ctx)
return buf.Bytes(), err
}