forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloblang_markdown.go
350 lines (288 loc) · 8.28 KB
/
bloblang_markdown.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package docs
import (
"bytes"
"strings"
"text/template"
"github.com/dafanshu/benthos/v4/internal/bloblang/query"
)
type functionCategory struct {
Name string
Specs []query.FunctionSpec
}
type functionsContext struct {
Categories []functionCategory
}
var bloblangParamsTemplate = `{{define "parameters" -}}
{{if gt (len .Definitions) 0}}
#### Parameters
{{range $i, $param := .Definitions -}}
` + "**`{{$param.Name}}`**" + ` <{{if $param.IsOptional}}(optional) {{end}}{{$param.ValueType}}{{if $param.DefaultValue}}, default ` + "`{{$param.PrettyDefault}}`" + `{{end}}> {{$param.Description}}
{{end -}}
{{end -}}
{{end -}}
`
var bloblangFunctionsTemplate = bloblangParamsTemplate + `{{define "function_example" -}}
{{if gt (len .Summary) 0 -}}
{{.Summary}}
{{end -}}
` + "```coffee" + `
{{.Mapping}}
{{range $i, $result := .Results}}
# In: {{index $result 0}}
# Out: {{index $result 1}}
{{end -}}
` + "```" + `
{{end -}}
{{define "function_spec" -}}
### ` + "`{{.Name}}`" + `
{{if eq .Status "beta" -}}
:::caution BETA
This function is mostly stable but breaking changes could still be made outside of major version releases if a fundamental problem with it is found.
:::
{{end -}}
{{if eq .Status "experimental" -}}
:::caution EXPERIMENTAL
This function is experimental and therefore breaking changes could be made to it outside of major version releases.
:::
{{end -}}
{{.Description}}{{if gt (len .Version) 0}}
Introduced in version {{.Version}}.
{{end}}
{{template "parameters" .Params -}}
{{if gt (len .Examples) 0}}
#### Examples
{{range $i, $example := .Examples}}
{{template "function_example" $example -}}
{{end -}}
{{end -}}
{{end -}}
---
title: Bloblang Functions
sidebar_label: Functions
description: A list of Bloblang functions
---
<!--
THIS FILE IS AUTOGENERATED!
To make changes please edit the contents of:
internal/bloblang/query/functions.go
internal/docs/bloblang.go
-->
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Functions can be placed anywhere and allow you to extract information from your environment, generate values, or access data from the underlying message being mapped:
` + "```coffee" + `
root.doc.id = uuid_v4()
root.doc.received_at = now()
root.doc.host = hostname()
` + "```" + `
Functions support both named and nameless style arguments:
` + "```coffee" + `
root.values_one = range(start: 0, stop: this.max, step: 2)
root.values_two = range(0, this.max, 2)
` + "```" + `
{{range $i, $cat := .Categories -}}
## {{$cat.Name}}
{{range $i, $spec := $cat.Specs -}}
{{template "function_spec" $spec}}
{{end -}}
{{end -}}
[error_handling]: /docs/configuration/error_handling
[field_paths]: /docs/configuration/field_paths
[meta_proc]: /docs/components/processors/metadata
[methods.encode]: /docs/guides/bloblang/methods#encode
[methods.string]: /docs/guides/bloblang/methods#string
`
func prefixExamples(s []query.ExampleSpec) {
for _, spec := range s {
for i := range spec.Results {
spec.Results[i][0] = strings.ReplaceAll(
strings.TrimSuffix(spec.Results[i][0], "\n"),
"\n", "\n# ",
)
spec.Results[i][1] = strings.ReplaceAll(
strings.TrimSuffix(spec.Results[i][1], "\n"),
"\n", "\n# ",
)
}
}
}
// BloblangFunctionsMarkdown returns a markdown document for all Bloblang
// functions.
func BloblangFunctionsMarkdown() ([]byte, error) {
ctx := functionsContext{}
specs := query.FunctionDocs()
for _, s := range specs {
prefixExamples(s.Examples)
}
for _, cat := range []string{
query.FunctionCategoryGeneral,
query.FunctionCategoryMessage,
query.FunctionCategoryEnvironment,
query.FunctionCategoryFakeData,
query.FunctionCategoryDeprecated,
} {
functions := functionCategory{
Name: cat,
}
for _, spec := range specs {
if spec.Category == cat {
functions.Specs = append(functions.Specs, spec)
}
}
if len(functions.Specs) > 0 {
ctx.Categories = append(ctx.Categories, functions)
}
}
var buf bytes.Buffer
err := template.Must(template.New("functions").Parse(bloblangFunctionsTemplate)).Execute(&buf, ctx)
return buf.Bytes(), err
}
//------------------------------------------------------------------------------
type methodCategory struct {
Name string
Specs []query.MethodSpec
}
type methodsContext struct {
Categories []methodCategory
General []query.MethodSpec
}
var bloblangMethodsTemplate = bloblangParamsTemplate + `{{define "method_example" -}}
{{if gt (len .Summary) 0 -}}
{{.Summary}}
{{end -}}
` + "```coffee" + `
{{.Mapping}}
{{range $i, $result := .Results}}
# In: {{index $result 0}}
# Out: {{index $result 1}}
{{end -}}
` + "```" + `
{{end -}}
{{define "method_spec" -}}
### ` + "`{{.Name}}`" + `
{{if eq .Status "beta" -}}
:::caution BETA
This method is mostly stable but breaking changes could still be made outside of major version releases if a fundamental problem with it is found.
:::
{{end -}}
{{if eq .Status "experimental" -}}
:::caution EXPERIMENTAL
This method is experimental and therefore breaking changes could be made to it outside of major version releases.
:::
{{end -}}
{{.Description}}{{if gt (len .Version) 0}}
Introduced in version {{.Version}}.
{{end}}
{{template "parameters" .Params -}}
{{if gt (len .Examples) 0}}
#### Examples
{{range $i, $example := .Examples}}
{{template "method_example" $example -}}
{{end -}}
{{end -}}
{{end -}}
---
title: Bloblang Methods
sidebar_label: Methods
description: A list of Bloblang methods
---
<!--
THIS FILE IS AUTOGENERATED!
To make changes please edit the contents of:
internal/bloblang/query/methods.go
internal/bloblang/query/methods_strings.go
internal/docs/bloblang.go
-->
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Methods provide most of the power in Bloblang as they allow you to augment values and can be added to any expression (including other methods):
` + "```coffee" + `
root.doc.id = this.thing.id.string().catch(uuid_v4())
root.doc.reduced_nums = this.thing.nums.map_each(num -> if num < 10 {
deleted()
} else {
num - 10
})
root.has_good_taste = ["pikachu","mewtwo","magmar"].contains(this.user.fav_pokemon)
` + "```" + `
Methods support both named and nameless style arguments:
` + "```coffee" + `
root.foo_one = this.(bar | baz).trim().replace_all(old: "dog", new: "cat")
root.foo_two = this.(bar | baz).trim().replace_all("dog", "cat")
` + "```" + `
{{if gt (len .General) 0 -}}
## General
{{range $i, $spec := .General -}}
{{template "method_spec" $spec}}
{{end -}}
{{end -}}
{{range $i, $cat := .Categories -}}
## {{$cat.Name}}
{{range $i, $spec := $cat.Specs -}}
{{template "method_spec" $spec}}
{{end -}}
{{end -}}
[field_paths]: /docs/configuration/field_paths
[methods.encode]: #encode
[methods.string]: #string
`
func methodForCat(s query.MethodSpec, cat string) (query.MethodSpec, bool) {
for _, c := range s.Categories {
if c.Category == cat {
spec := s
if len(c.Description) > 0 {
spec.Description = strings.TrimSpace(c.Description)
}
if len(c.Examples) > 0 {
spec.Examples = c.Examples
}
return spec, true
}
}
return s, false
}
// BloblangMethodsMarkdown returns a markdown document for all Bloblang methods.
func BloblangMethodsMarkdown() ([]byte, error) {
ctx := methodsContext{}
specs := query.MethodDocs()
for _, s := range specs {
prefixExamples(s.Examples)
for _, cat := range s.Categories {
prefixExamples(cat.Examples)
}
}
for _, cat := range []string{
query.MethodCategoryStrings,
query.MethodCategoryRegexp,
query.MethodCategoryNumbers,
query.MethodCategoryTime,
query.MethodCategoryCoercion,
query.MethodCategoryObjectAndArray,
query.MethodCategoryParsing,
query.MethodCategoryEncoding,
query.MethodCategoryGeoIP,
query.MethodCategoryDeprecated,
} {
methods := methodCategory{
Name: cat,
}
for _, spec := range specs {
var ok bool
if spec, ok = methodForCat(spec, cat); ok {
methods.Specs = append(methods.Specs, spec)
}
}
if len(methods.Specs) > 0 {
ctx.Categories = append(ctx.Categories, methods)
}
}
for _, spec := range specs {
if len(spec.Categories) == 0 && spec.Status != query.StatusHidden {
spec.Description = strings.TrimSpace(spec.Description)
ctx.General = append(ctx.General, spec)
}
}
var buf bytes.Buffer
err := template.Must(template.New("methods").Parse(bloblangMethodsTemplate)).Execute(&buf, ctx)
return buf.Bytes(), err
}