Skip to content

Commit f75427b

Browse files
authored
[chore][pkg/stanza] exclude unkeyed literal initialization (#43786)
Exclude unkeyed literal initialization with the pkg/stanza API.
1 parent 9adb940 commit f75427b

File tree

9 files changed

+71
-64
lines changed

9 files changed

+71
-64
lines changed

pkg/stanza/entry/attribute_field.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
// AttributeField is the path to an entry attribute
1414
type AttributeField struct {
1515
Keys []string
16+
// prevent unkeyed literal initialization
17+
_ struct{}
1618
}
1719

1820
// NewAttributeField will creat a new attribute field from a key
@@ -33,15 +35,15 @@ func (f AttributeField) Parent() AttributeField {
3335
}
3436

3537
keys := f.Keys[:len(f.Keys)-1]
36-
return AttributeField{keys}
38+
return AttributeField{Keys: keys}
3739
}
3840

3941
// Child returns a child of the current field using the given key.
4042
func (f AttributeField) Child(key string) AttributeField {
4143
child := make([]string, len(f.Keys), len(f.Keys)+1)
4244
copy(child, f.Keys)
4345
child = append(child, key)
44-
return AttributeField{child}
46+
return AttributeField{Keys: child}
4547
}
4648

4749
// IsRoot returns a boolean indicating if this is a root level field.
@@ -178,7 +180,7 @@ func (f *AttributeField) UnmarshalJSON(raw []byte) error {
178180
return fmt.Errorf("must start with 'attributes': %s", value)
179181
}
180182

181-
*f = AttributeField{keys[1:]}
183+
*f = AttributeField{Keys: keys[1:]}
182184
return nil
183185
}
184186

@@ -198,7 +200,7 @@ func (f *AttributeField) UnmarshalYAML(unmarshal func(any) error) error {
198200
return fmt.Errorf("must start with 'attributes': %s", value)
199201
}
200202

201-
*f = AttributeField{keys[1:]}
203+
*f = AttributeField{Keys: keys[1:]}
202204
return nil
203205
}
204206

@@ -213,6 +215,6 @@ func (f *AttributeField) UnmarshalText(text []byte) error {
213215
return fmt.Errorf("must start with 'attributes': %s", text)
214216
}
215217

216-
*f = AttributeField{keys[1:]}
218+
*f = AttributeField{Keys: keys[1:]}
217219
return nil
218220
}

pkg/stanza/entry/attribute_field_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,25 +361,25 @@ func TestAttributeFieldSet(t *testing.T) {
361361

362362
func TestAttributeFieldParent(t *testing.T) {
363363
t.Run("Simple", func(t *testing.T) {
364-
field := AttributeField{[]string{"child"}}
365-
require.Equal(t, AttributeField{[]string{}}, field.Parent())
364+
field := AttributeField{Keys: []string{"child"}}
365+
require.Equal(t, AttributeField{Keys: []string{}}, field.Parent())
366366
})
367367

368368
t.Run("Root", func(t *testing.T) {
369-
field := AttributeField{[]string{}}
370-
require.Equal(t, AttributeField{[]string{}}, field.Parent())
369+
field := AttributeField{Keys: []string{}}
370+
require.Equal(t, AttributeField{Keys: []string{}}, field.Parent())
371371
})
372372
}
373373

374374
func TestAttributeFieldChild(t *testing.T) {
375-
field := AttributeField{[]string{"parent"}}
376-
require.Equal(t, AttributeField{[]string{"parent", "child"}}, field.Child("child"))
375+
field := AttributeField{Keys: []string{"parent"}}
376+
require.Equal(t, AttributeField{Keys: []string{"parent", "child"}}, field.Child("child"))
377377
}
378378

379379
func TestAttributeFieldMerge(t *testing.T) {
380380
entry := &Entry{}
381381
entry.Attributes = map[string]any{"old": "values"}
382-
field := AttributeField{[]string{"embedded"}}
382+
field := AttributeField{Keys: []string{"embedded"}}
383383
values := map[string]any{"new": "values"}
384384
field.Merge(entry, values)
385385
expected := map[string]any{"embedded": values, "old": "values"}

pkg/stanza/entry/body_field.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
// BodyField is a field found on an entry body.
1313
type BodyField struct {
1414
Keys []string
15+
// prevent unkeyed literal initialization
16+
_ struct{}
1517
}
1618

1719
// NewBodyField creates a new field from an ordered array of keys.
@@ -32,15 +34,15 @@ func (f BodyField) Parent() BodyField {
3234
}
3335

3436
keys := f.Keys[:len(f.Keys)-1]
35-
return BodyField{keys}
37+
return BodyField{Keys: keys}
3638
}
3739

3840
// Child returns a child of the current field using the given key.
3941
func (f BodyField) Child(key string) BodyField {
4042
child := make([]string, len(f.Keys), len(f.Keys)+1)
4143
copy(child, f.Keys)
4244
child = append(child, key)
43-
return BodyField{child}
45+
return BodyField{Keys: child}
4446
}
4547

4648
// IsRoot returns a boolean indicating if this is a root level field.
@@ -169,7 +171,7 @@ func (f *BodyField) UnmarshalJSON(raw []byte) error {
169171
return fmt.Errorf("must start with 'body': %s", value)
170172
}
171173

172-
*f = BodyField{keys[1:]}
174+
*f = BodyField{Keys: keys[1:]}
173175
return nil
174176
}
175177

@@ -189,7 +191,7 @@ func (f *BodyField) UnmarshalYAML(unmarshal func(any) error) error {
189191
return fmt.Errorf("must start with 'body': %s", value)
190192
}
191193

192-
*f = BodyField{keys[1:]}
194+
*f = BodyField{Keys: keys[1:]}
193195
return nil
194196
}
195197

@@ -204,6 +206,6 @@ func (f *BodyField) UnmarshalText(text []byte) error {
204206
return fmt.Errorf("must start with 'body': %s", text)
205207
}
206208

207-
*f = BodyField{keys[1:]}
209+
*f = BodyField{Keys: keys[1:]}
208210
return nil
209211
}

pkg/stanza/entry/body_field_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,25 @@ func TestBodyFieldSet(t *testing.T) {
287287

288288
func TestBodyFieldParent(t *testing.T) {
289289
t.Run("Simple", func(t *testing.T) {
290-
field := BodyField{[]string{"child"}}
291-
require.Equal(t, BodyField{[]string{}}, field.Parent())
290+
field := BodyField{Keys: []string{"child"}}
291+
require.Equal(t, BodyField{Keys: []string{}}, field.Parent())
292292
})
293293

294294
t.Run("Root", func(t *testing.T) {
295-
field := BodyField{[]string{}}
296-
require.Equal(t, BodyField{[]string{}}, field.Parent())
295+
field := BodyField{Keys: []string{}}
296+
require.Equal(t, BodyField{Keys: []string{}}, field.Parent())
297297
})
298298
}
299299

300300
func TestBodyFieldChild(t *testing.T) {
301-
field := BodyField{[]string{"parent"}}
302-
require.Equal(t, BodyField{[]string{"parent", "child"}}, field.Child("child"))
301+
field := BodyField{Keys: []string{"parent"}}
302+
require.Equal(t, BodyField{Keys: []string{"parent", "child"}}, field.Child("child"))
303303
}
304304

305305
func TestBodyFieldMerge(t *testing.T) {
306306
entry := &Entry{}
307307
entry.Body = "raw_value"
308-
field := BodyField{[]string{"embedded"}}
308+
field := BodyField{Keys: []string{"embedded"}}
309309
values := map[string]any{"new": "values"}
310310
field.Merge(entry, values)
311311
expected := map[string]any{"embedded": values}

pkg/stanza/entry/entry_test.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -200,46 +200,39 @@ func TestFieldFromString(t *testing.T) {
200200
expectedError bool
201201
}{
202202
{
203-
"Body",
204-
"body",
205-
Field{BodyField{[]string{}}},
206-
false,
203+
name: "Body",
204+
input: "body",
205+
output: Field{FieldInterface: BodyField{Keys: []string{}}},
207206
},
208207
{
209-
"PrefixedBody",
210-
"body.test",
211-
Field{BodyField{[]string{"test"}}},
212-
false,
208+
name: "PrefixedBody",
209+
input: "body.test",
210+
output: Field{FieldInterface: BodyField{Keys: []string{"test"}}},
213211
},
214212
{
215-
"NestedBody",
216-
"body.test.foo.bar",
217-
Field{BodyField{[]string{"test", "foo", "bar"}}},
218-
false,
213+
name: "NestedBody",
214+
input: "body.test.foo.bar",
215+
output: Field{FieldInterface: BodyField{Keys: []string{"test", "foo", "bar"}}},
219216
},
220217
{
221-
"SimpleAttribute",
222-
"attributes.test",
223-
Field{AttributeField{[]string{"test"}}},
224-
false,
218+
name: "SimpleAttribute",
219+
input: "attributes.test",
220+
output: Field{FieldInterface: AttributeField{Keys: []string{"test"}}},
225221
},
226222
{
227-
"NestedAttribute",
228-
"attributes.test.foo.bar",
229-
Field{AttributeField{[]string{"test", "foo", "bar"}}},
230-
false,
223+
name: "NestedAttribute",
224+
input: "attributes.test.foo.bar",
225+
output: Field{FieldInterface: AttributeField{Keys: []string{"test", "foo", "bar"}}},
231226
},
232227
{
233-
"SimpleResource",
234-
"resource.test",
235-
Field{ResourceField{[]string{"test"}}},
236-
false,
228+
name: "SimpleResource",
229+
input: "resource.test",
230+
output: Field{ResourceField{Keys: []string{"test"}}},
237231
},
238232
{
239-
"NestedResource",
240-
"resource.test.foo.bar",
241-
Field{ResourceField{[]string{"test", "foo", "bar"}}},
242-
false,
233+
name: "NestedResource",
234+
input: "resource.test.foo.bar",
235+
output: Field{FieldInterface: ResourceField{Keys: []string{"test", "foo", "bar"}}},
243236
},
244237
}
245238

pkg/stanza/entry/field.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type Field struct {
2626
// RootableField is a Field that may refer directly to "attributes" or "resource"
2727
type RootableField struct {
2828
Field
29+
// prevent unkeyed literal initialization
30+
_ struct{}
2931
}
3032

3133
// FieldInterface is a field on an entry.

pkg/stanza/entry/resource_field.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
// ResourceField is the path to an entry resource
1414
type ResourceField struct {
1515
Keys []string
16+
// prevent unkeyed literal initialization
17+
_ struct{}
1618
}
1719

1820
// NewResourceField will creat a new resource field from a key
@@ -33,15 +35,19 @@ func (f ResourceField) Parent() ResourceField {
3335
}
3436

3537
keys := f.Keys[:len(f.Keys)-1]
36-
return ResourceField{keys}
38+
return ResourceField{
39+
Keys: keys,
40+
}
3741
}
3842

3943
// Child returns a child of the current field using the given key.
4044
func (f ResourceField) Child(key string) ResourceField {
4145
child := make([]string, len(f.Keys), len(f.Keys)+1)
4246
copy(child, f.Keys)
4347
child = append(child, key)
44-
return ResourceField{child}
48+
return ResourceField{
49+
Keys: child,
50+
}
4551
}
4652

4753
// IsRoot returns a boolean indicating if this is a root level field.
@@ -178,7 +184,7 @@ func (f *ResourceField) UnmarshalJSON(raw []byte) error {
178184
return fmt.Errorf("must start with 'resource': %s", value)
179185
}
180186

181-
*f = ResourceField{keys[1:]}
187+
*f = ResourceField{Keys: keys[1:]}
182188
return nil
183189
}
184190

@@ -198,7 +204,7 @@ func (f *ResourceField) UnmarshalYAML(unmarshal func(any) error) error {
198204
return fmt.Errorf("must start with 'resource': %s", value)
199205
}
200206

201-
*f = ResourceField{keys[1:]}
207+
*f = ResourceField{Keys: keys[1:]}
202208
return nil
203209
}
204210

@@ -213,6 +219,6 @@ func (f *ResourceField) UnmarshalText(text []byte) error {
213219
return fmt.Errorf("must start with 'resource': %s", text)
214220
}
215221

216-
*f = ResourceField{keys[1:]}
222+
*f = ResourceField{Keys: keys[1:]}
217223
return nil
218224
}

pkg/stanza/entry/resource_field_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,25 +361,25 @@ func TestResourceFieldSet(t *testing.T) {
361361

362362
func TestResourceFieldParent(t *testing.T) {
363363
t.Run("Simple", func(t *testing.T) {
364-
field := ResourceField{[]string{"child"}}
365-
require.Equal(t, ResourceField{[]string{}}, field.Parent())
364+
field := ResourceField{Keys: []string{"child"}}
365+
require.Equal(t, ResourceField{Keys: []string{}}, field.Parent())
366366
})
367367

368368
t.Run("Root", func(t *testing.T) {
369-
field := ResourceField{[]string{}}
370-
require.Equal(t, ResourceField{[]string{}}, field.Parent())
369+
field := ResourceField{Keys: []string{}}
370+
require.Equal(t, ResourceField{Keys: []string{}}, field.Parent())
371371
})
372372
}
373373

374374
func TestResourceFieldChild(t *testing.T) {
375-
field := ResourceField{[]string{"parent"}}
376-
require.Equal(t, ResourceField{[]string{"parent", "child"}}, field.Child("child"))
375+
field := ResourceField{Keys: []string{"parent"}}
376+
require.Equal(t, ResourceField{Keys: []string{"parent", "child"}}, field.Child("child"))
377377
}
378378

379379
func TestResourceFieldMerge(t *testing.T) {
380380
entry := &Entry{}
381381
entry.Resource = map[string]any{"old": "values"}
382-
field := ResourceField{[]string{"embedded"}}
382+
field := ResourceField{Keys: []string{"embedded"}}
383383
values := map[string]any{"new": "values"}
384384
field.Merge(entry, values)
385385
expected := map[string]any{"embedded": values, "old": "values"}

pkg/stanza/operator/helper/scope_name.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
// ScopeNameParser is a helper that parses severity onto an entry.
1212
type ScopeNameParser struct {
1313
ParseFrom entry.Field `mapstructure:"parse_from,omitempty"`
14+
// prevent unkeyed literal initialization
15+
_ struct{}
1416
}
1517

1618
// NewScopeNameParser creates a new scope parser with default values

0 commit comments

Comments
 (0)