-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
112 lines (89 loc) · 2.1 KB
/
plugin.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
package fluentbitconfig
import (
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"github.com/calyptia/go-fluentbit-config/v2/property"
)
type Plugins []Plugin
// IDs not namespaced.
// For example: tail.0
func (plugins Plugins) IDs() []string {
var ids []string
for _, plugin := range plugins {
ids = append(ids, plugin.ID)
}
return ids
}
// FindByID were the id should not be namespaced.
// For example: tail.0
func (plugins Plugins) FindByID(id string) (Plugin, bool) {
for _, plugin := range plugins {
if plugin.ID == id {
return plugin, true
}
}
return Plugin{}, false
}
func (plugins *Plugins) UnmarshalJSON(data []byte) error {
var dest []Plugin
err := json.Unmarshal(data, &dest)
if err != nil {
return err
}
*plugins = dest
for i, plugin := range *plugins {
plugin.ID = fmt.Sprintf("%s.%d", plugin.Name, i)
(*plugins)[i] = plugin
}
return nil
}
func (plugins *Plugins) UnmarshalYAML(node *yaml.Node) error {
var dest []Plugin
err := node.Decode(&dest)
if err != nil {
return err
}
*plugins = dest
for i, plugin := range *plugins {
plugin.ID = fmt.Sprintf("%s.%d", plugin.Name, i)
(*plugins)[i] = plugin
}
return nil
}
type Plugin struct {
ID string `json:"-" yaml:"-"`
Name string `json:"-" yaml:"-"`
Properties property.Properties `json:",inline" yaml:",inline"`
}
func (p Plugin) MarshalJSON() ([]byte, error) {
return p.Properties.MarshalJSON()
}
func (p Plugin) MarshalYAML() (any, error) {
return p.Properties.MarshalYAML()
}
func (p *Plugin) UnmarshalJSON(data []byte) error {
err := p.Properties.UnmarshalJSON(data)
if err != nil {
return err
}
p.Name = Name(p.Properties)
return nil
}
func (p *Plugin) UnmarshalYAML(node *yaml.Node) error {
err := p.Properties.UnmarshalYAML(node)
if err != nil {
return err
}
p.Name = Name(p.Properties)
return nil
}
func (p Plugin) Equal(target Plugin) bool {
return p.Properties.Equal(target.Properties)
}
func (plugins Plugins) Equal(target Plugins) bool {
return slices.EqualFunc(plugins, target, func(a, b Plugin) bool {
return a.Equal(b)
})
}