/
tag_dict.go
197 lines (162 loc) · 4.37 KB
/
tag_dict.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
package starlark
import (
"errors"
"fmt"
"strings"
"github.com/influxdata/telegraf"
"go.starlark.net/starlark"
)
// TagDict is a starlark.Value for the metric tags. It is heavily based on the
// starlark.Dict.
type TagDict struct {
*Metric
}
func (d TagDict) String() string {
buf := new(strings.Builder)
buf.WriteString("{")
sep := ""
for _, item := range d.Items() {
k, v := item[0], item[1]
buf.WriteString(sep)
buf.WriteString(k.String())
buf.WriteString(": ")
buf.WriteString(v.String())
sep = ", "
}
buf.WriteString("}")
return buf.String()
}
func (d TagDict) Type() string {
return "Tags"
}
func (d TagDict) Freeze() {
d.frozen = true
}
func (d TagDict) Truth() starlark.Bool {
return len(d.metric.TagList()) != 0
}
func (d TagDict) Hash() (uint32, error) {
return 0, errors.New("not hashable")
}
// AttrNames implements the starlark.HasAttrs interface.
func (d TagDict) AttrNames() []string {
return builtinAttrNames(TagDictMethods)
}
// Attr implements the starlark.HasAttrs interface.
func (d TagDict) Attr(name string) (starlark.Value, error) {
return builtinAttr(d, name, TagDictMethods)
}
var TagDictMethods = map[string]builtinMethod{
"clear": dictClear,
"get": dictGet,
"items": dictItems,
"keys": dictKeys,
"pop": dictPop,
"popitem": dictPopitem,
"setdefault": dictSetdefault,
"update": dictUpdate,
"values": dictValues,
}
// Get implements the starlark.Mapping interface.
func (d TagDict) Get(key starlark.Value) (v starlark.Value, found bool, err error) {
if k, ok := key.(starlark.String); ok {
gv, found := d.metric.GetTag(k.GoString())
if !found {
return starlark.None, false, nil
}
return starlark.String(gv), true, err
}
return starlark.None, false, errors.New("key must be of type 'str'")
}
// SetKey implements the starlark.HasSetKey interface to support map update
// using x[k]=v syntax, like a dictionary.
func (d TagDict) SetKey(k, v starlark.Value) error {
if d.tagIterCount > 0 {
return fmt.Errorf("cannot insert during iteration")
}
key, ok := k.(starlark.String)
if !ok {
return errors.New("tag key must be of type 'str'")
}
value, ok := v.(starlark.String)
if !ok {
return errors.New("tag value must be of type 'str'")
}
d.metric.AddTag(key.GoString(), value.GoString())
return nil
}
// Items implements the starlark.IterableMapping interface.
func (d TagDict) Items() []starlark.Tuple {
items := make([]starlark.Tuple, 0, len(d.metric.TagList()))
for _, tag := range d.metric.TagList() {
key := starlark.String(tag.Key)
value := starlark.String(tag.Value)
pair := starlark.Tuple{key, value}
items = append(items, pair)
}
return items
}
func (d TagDict) Clear() error {
if d.tagIterCount > 0 {
return fmt.Errorf("cannot delete during iteration")
}
keys := make([]string, 0, len(d.metric.TagList()))
for _, tag := range d.metric.TagList() {
keys = append(keys, tag.Key)
}
for _, key := range keys {
d.metric.RemoveTag(key)
}
return nil
}
func (d TagDict) PopItem() (v starlark.Value, err error) {
if d.tagIterCount > 0 {
return nil, fmt.Errorf("cannot delete during iteration")
}
for _, tag := range d.metric.TagList() {
k := tag.Key
v := tag.Value
d.metric.RemoveTag(k)
sk := starlark.String(k)
sv := starlark.String(v)
return starlark.Tuple{sk, sv}, nil
}
return nil, errors.New("popitem(): tag dictionary is empty")
}
func (d TagDict) Delete(k starlark.Value) (v starlark.Value, found bool, err error) {
if d.tagIterCount > 0 {
return nil, false, fmt.Errorf("cannot delete during iteration")
}
if key, ok := k.(starlark.String); ok {
value, ok := d.metric.GetTag(key.GoString())
if ok {
d.metric.RemoveTag(key.GoString())
v := starlark.String(value)
return v, ok, err
}
}
return starlark.None, false, errors.New("key must be of type 'str'")
}
// Items implements the starlark.Mapping interface.
func (d TagDict) Iterate() starlark.Iterator {
d.tagIterCount++
return &TagIterator{Metric: d.Metric, tags: d.metric.TagList()}
}
type TagIterator struct {
*Metric
tags []*telegraf.Tag
}
// Next implements the starlark.Iterator interface.
func (i *TagIterator) Next(p *starlark.Value) bool {
if len(i.tags) == 0 {
return false
}
tag := i.tags[0]
i.tags = i.tags[1:]
*p = starlark.String(tag.Key)
return true
}
// Done implements the starlark.Iterator interface.
func (i *TagIterator) Done() {
i.tagIterCount--
}