forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
190 lines (161 loc) · 4.85 KB
/
xml.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
package processor
import (
"fmt"
"time"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/tracing"
"github.com/dafanshu/benthos/v3/internal/xml"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/types"
)
func init() {
Constructors[TypeXML] = TypeSpec{
constructor: NewXML,
Status: docs.StatusBeta,
Categories: []Category{
CategoryParsing,
},
Summary: `
Parses messages as an XML document, performs a mutation on the data, and then
overwrites the previous contents with the new value.`,
Description: `
## Operators
### ` + "`to_json`" + `
Converts an XML document into a JSON structure, where elements appear as keys of
an object according to the following rules:
- If an element contains attributes they are parsed by prefixing a hyphen,
` + "`-`" + `, to the attribute label.
- If the element is a simple element and has attributes, the element value
is given the key ` + "`#text`" + `.
- XML comments, directives, and process instructions are ignored.
- When elements are repeated the resulting JSON value is an array.
For example, given the following XML:
` + "```xml" + `
<root>
<title>This is a title</title>
<description tone="boring">This is a description</description>
<elements id="1">foo1</elements>
<elements id="2">foo2</elements>
<elements>foo3</elements>
</root>
` + "```" + `
The resulting JSON structure would look like this:
` + "```json" + `
{
"root":{
"title":"This is a title",
"description":{
"#text":"This is a description",
"-tone":"boring"
},
"elements":[
{"#text":"foo1","-id":"1"},
{"#text":"foo2","-id":"2"},
"foo3"
]
}
}
` + "```" + `
With cast set to true, the resulting JSON structure would look like this:
` + "```json" + `
{
"root":{
"title":"This is a title",
"description":{
"#text":"This is a description",
"-tone":"boring"
},
"elements":[
{"#text":"foo1","-id":1},
{"#text":"foo2","-id":2},
"foo3"
]
}
}
` + "```" + ``,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("operator", "An XML [operation](#operators) to apply to messages.").HasOptions("to_json"),
docs.FieldCommon("cast", "Whether to try to cast values that are numbers and booleans to the right type. Default: all values are strings."),
PartsFieldSpec,
},
}
}
//------------------------------------------------------------------------------
// XMLConfig contains configuration fields for the XML processor.
type XMLConfig struct {
Parts []int `json:"parts" yaml:"parts"`
Operator string `json:"operator" yaml:"operator"`
Cast bool `json:"cast" yaml:"cast"`
}
// NewXMLConfig returns a XMLConfig with default values.
func NewXMLConfig() XMLConfig {
return XMLConfig{
Parts: []int{},
Operator: "to_json",
Cast: false,
}
}
//------------------------------------------------------------------------------
// XML is a processor that performs an operation on a XML payload.
type XML struct {
parts []int
conf Config
log log.Modular
stats metrics.Type
mCount metrics.StatCounter
mErr metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
}
// NewXML returns a XML processor.
func NewXML(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
if conf.XML.Operator != "to_json" {
return nil, fmt.Errorf("operator not recognised: %v", conf.XML.Operator)
}
j := &XML{
parts: conf.XML.Parts,
conf: conf,
log: log,
stats: stats,
mCount: stats.GetCounter("count"),
mErr: stats.GetCounter("error"),
mSent: stats.GetCounter("sent"),
mBatchSent: stats.GetCounter("batch.sent"),
}
return j, nil
}
//------------------------------------------------------------------------------
// ProcessMessage applies the processor to a message, either creating >0
// resulting messages or a response to be sent back to the message source.
func (p *XML) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
p.mCount.Incr(1)
newMsg := msg.Copy()
proc := func(index int, span *tracing.Span, part types.Part) error {
root, err := xml.ToMap(part.Get(), p.conf.XML.Cast)
if err != nil {
p.mErr.Incr(1)
p.log.Debugf("Failed to parse part as XML: %v\n", err)
return err
}
if err = part.SetJSON(root); err != nil {
p.mErr.Incr(1)
p.log.Debugf("Failed to marshal XML as JSON: %v\n", err)
return err
}
return nil
}
IteratePartsWithSpanV2(TypeXML, p.parts, newMsg, proc)
p.mBatchSent.Incr(1)
p.mSent.Incr(int64(newMsg.Len()))
return []types.Message{newMsg}, nil
}
// CloseAsync shuts down the processor and stops processing requests.
func (p *XML) CloseAsync() {
}
// WaitForClose blocks until the processor has closed down.
func (p *XML) WaitForClose(timeout time.Duration) error {
return nil
}