forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
267 lines (237 loc) · 6.05 KB
/
field.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package entry // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
import (
"encoding/json"
"fmt"
"strings"
)
const (
AttributesPrefix = "attributes"
ResourcePrefix = "resource"
BodyPrefix = "body"
)
// Field represents a potential field on an entry.
// It is used to get, set, and delete values at this field.
// It is deserialized from JSON dot notation.
type Field struct {
FieldInterface
}
// RootableField is a Field that may refer directly to "attributes" or "resource"
type RootableField struct {
Field
}
// FieldInterface is a field on an entry.
type FieldInterface interface {
Get(*Entry) (interface{}, bool)
Set(entry *Entry, value interface{}) error
Delete(entry *Entry) (interface{}, bool)
String() string
}
// UnmarshalJSON will unmarshal a field from JSON
func (f *Field) UnmarshalJSON(raw []byte) error {
var s string
err := json.Unmarshal(raw, &s)
if err != nil {
return err
}
*f, err = NewField(s)
return err
}
// UnmarshalJSON will unmarshal a field from JSON
func (r *RootableField) UnmarshalJSON(raw []byte) error {
var s string
err := json.Unmarshal(raw, &s)
if err != nil {
return err
}
field, err := newField(s, true)
*r = RootableField{Field: field}
return err
}
// UnmarshalYAML will unmarshal a field from YAML
func (f *Field) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
*f, err = NewField(s)
return err
}
// UnmarshalYAML will unmarshal a field from YAML
func (r *RootableField) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
field, err := newField(s, true)
*r = RootableField{Field: field}
return err
}
// UnmarshalText will unmarshal a field from text
func (f *Field) UnmarshalText(text []byte) error {
field, err := NewField(string(text))
*f = field
return err
}
// UnmarshalText will unmarshal a field from text
func (r *RootableField) UnmarshalText(text []byte) error {
field, err := newField(string(text), true)
*r = RootableField{Field: field}
return err
}
func NewField(s string) (Field, error) {
return newField(s, false)
}
func newField(s string, rootable bool) (Field, error) {
keys, err := fromJSONDot(s)
if err != nil {
return Field{}, fmt.Errorf("splitting field: %w", err)
}
switch keys[0] {
case AttributesPrefix:
if !rootable && len(keys) == 1 {
return Field{}, fmt.Errorf("attributes cannot be referenced without subfield")
}
return NewAttributeField(keys[1:]...), nil
case ResourcePrefix:
if !rootable && len(keys) == 1 {
return Field{}, fmt.Errorf("resource cannot be referenced without subfield")
}
return NewResourceField(keys[1:]...), nil
case BodyPrefix:
return NewBodyField(keys[1:]...), nil
default:
return Field{}, fmt.Errorf("unrecognized prefix")
}
}
type splitState uint
const (
// Begin is the beginning state of a field split
Begin splitState = iota
// InBracket is the state of a field split inside a bracket
InBracket
// InQuote is the state of a field split inside a quote
InQuote
// OutQuote is the state of a field split outside a quote
OutQuote
// OutBracket is the state of a field split outside a bracket
OutBracket
// InUnbracketedToken is the state field split on any token outside brackets
InUnbracketedToken
)
func fromJSONDot(s string) ([]string, error) {
fields := make([]string, 0, 1)
state := Begin
var quoteChar rune
var tokenStart int
for i, c := range s {
switch state {
case Begin:
if c == '[' {
state = InBracket
continue
}
tokenStart = i
state = InUnbracketedToken
case InBracket:
if !(c == '\'' || c == '"') {
return nil, fmt.Errorf("strings in brackets must be surrounded by quotes")
}
state = InQuote
quoteChar = c
tokenStart = i + 1
case InQuote:
if c == quoteChar {
fields = append(fields, s[tokenStart:i])
state = OutQuote
}
case OutQuote:
if c != ']' {
return nil, fmt.Errorf("found characters between closed quote and closing bracket")
}
state = OutBracket
case OutBracket:
switch c {
case '.':
state = InUnbracketedToken
tokenStart = i + 1
case '[':
state = InBracket
default:
return nil, fmt.Errorf("bracketed access must be followed by a dot or another bracketed access")
}
case InUnbracketedToken:
if c == '.' {
fields = append(fields, s[tokenStart:i])
tokenStart = i + 1
} else if c == '[' {
fields = append(fields, s[tokenStart:i])
state = InBracket
}
}
}
switch state {
case InBracket, OutQuote:
return nil, fmt.Errorf("found unclosed left bracket")
case InQuote:
if quoteChar == '"' {
return nil, fmt.Errorf("found unclosed double quote")
}
return nil, fmt.Errorf("found unclosed single quote")
case InUnbracketedToken:
fields = append(fields, s[tokenStart:])
case Begin, OutBracket:
// shouldn't be possible
}
if len(fields) == 0 {
return nil, fmt.Errorf("fields size is 0")
}
return fields, nil
}
// toJSONDot returns the JSON dot notation for a field.
func toJSONDot(prefix string, keys []string) string {
if len(keys) == 0 {
return prefix
}
containsDots := false
for _, key := range keys {
if strings.Contains(key, ".") {
containsDots = true
}
}
var b strings.Builder
b.WriteString(prefix)
if containsDots {
for _, key := range keys {
b.WriteString(`['`)
b.WriteString(key)
b.WriteString(`']`)
}
} else {
b.WriteString(".")
for i, key := range keys {
if i != 0 {
b.WriteString(".")
}
b.WriteString(key)
}
}
return b.String()
}
// getNestedMap will get a nested map assigned to a key.
// If the map does not exist, it will create and return it.
func getNestedMap(currentMap map[string]interface{}, key string) map[string]interface{} {
currentValue, ok := currentMap[key]
if !ok {
currentMap[key] = map[string]interface{}{}
}
nextMap, ok := currentValue.(map[string]interface{})
if !ok {
nextMap = map[string]interface{}{}
currentMap[key] = nextMap
}
return nextMap
}