forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloblang.go
75 lines (68 loc) · 1.83 KB
/
bloblang.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
package msgpack
import (
"github.com/vmihailenco/msgpack/v5"
"github.com/benthosdev/benthos/v4/internal/bloblang/query"
"github.com/benthosdev/benthos/v4/public/bloblang"
)
func init() {
// Note: The examples are run and tested from within
// ./internal/bloblang/query/parsed_test.go
msgpackParseSpec := bloblang.NewPluginSpec().
Category("Parsing").
Description("Parses a [MessagePack](https://msgpack.org/) message into a structured document.").
Example("",
`root = content().decode("hex").parse_msgpack()`,
[2]string{
`81a3666f6fa3626172`,
`{"foo":"bar"}`,
}).
Example("",
`root = this.encoded.decode("base64").parse_msgpack()`,
[2]string{
`{"encoded":"gaNmb2+jYmFy"}`,
`{"foo":"bar"}`,
})
if err := bloblang.RegisterMethodV2(
"parse_msgpack", msgpackParseSpec,
func(args *bloblang.ParsedParams) (bloblang.Method, error) {
return func(v any) (any, error) {
b, err := query.IGetBytes(v)
if err != nil {
return nil, err
}
var jObj any
if err := msgpack.Unmarshal(b, &jObj); err != nil {
return nil, err
}
return jObj, nil
}, nil
},
); err != nil {
panic(err)
}
msgpackFormatSpec := bloblang.NewPluginSpec().
Category("Parsing").
Description("Formats data as a [MessagePack](https://msgpack.org/) message in bytes format.").
Example("",
`root = this.format_msgpack().encode("hex")`,
[2]string{
`{"foo":"bar"}`,
`81a3666f6fa3626172`,
}).
Example("",
`root.encoded = this.format_msgpack().encode("base64")`,
[2]string{
`{"foo":"bar"}`,
`{"encoded":"gaNmb2+jYmFy"}`,
})
if err := bloblang.RegisterMethodV2(
"format_msgpack", msgpackFormatSpec,
func(args *bloblang.ParsedParams) (bloblang.Method, error) {
return func(v any) (any, error) {
return msgpack.Marshal(v)
}, nil
},
); err != nil {
panic(err)
}
}