forked from benthosdev/benthos
/
config_drop_on.go
60 lines (52 loc) · 1.52 KB
/
config_drop_on.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
package output
import (
"encoding/json"
)
// DropOnConditions is a config struct representing the different circumstances
// under which messages should be dropped.
type DropOnConditions struct {
Error bool `json:"error" yaml:"error"`
BackPressure string `json:"back_pressure" yaml:"back_pressure"`
}
// DropOnConfig contains configuration values for the DropOn output type.
type DropOnConfig struct {
DropOnConditions `json:",inline" yaml:",inline"`
Output *Config `json:"output" yaml:"output"`
}
// NewDropOnConfig creates a new DropOnConfig with default values.
func NewDropOnConfig() DropOnConfig {
return DropOnConfig{
DropOnConditions: DropOnConditions{
Error: false,
BackPressure: "",
},
Output: nil,
}
}
//------------------------------------------------------------------------------
type dummyDropOnConfig struct {
DropOnConditions `json:",inline" yaml:",inline"`
Output any `json:"output" yaml:"output"`
}
// MarshalJSON prints an empty object instead of nil.
func (d DropOnConfig) MarshalJSON() ([]byte, error) {
dummy := dummyDropOnConfig{
Output: d.Output,
DropOnConditions: d.DropOnConditions,
}
if d.Output == nil {
dummy.Output = struct{}{}
}
return json.Marshal(dummy)
}
// MarshalYAML prints an empty object instead of nil.
func (d DropOnConfig) MarshalYAML() (any, error) {
dummy := dummyDropOnConfig{
Output: d.Output,
DropOnConditions: d.DropOnConditions,
}
if d.Output == nil {
dummy.Output = struct{}{}
}
return dummy, nil
}