Skip to content

Commit afa2739

Browse files
[mdatagen] Require at least two values for enum attributes
Update validateAttributes and validateResourceAttributes to return a validation error if an enum attribute has fewer than two values. Add unit tests for single-element and empty enum validation.
1 parent 2d31d94 commit afa2739

4 files changed

Lines changed: 52 additions & 10 deletions

File tree

cmd/mdatagen/internal/metadata.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ func (md *Metadata) validateResourceAttributes() error {
178178
if attr.EnabledPtr == nil {
179179
errs = errors.Join(errs, fmt.Errorf("enabled field is required for resource attribute: %v", name))
180180
}
181-
if attr.Enum != nil && len(attr.Enum) == 0 {
182-
errs = errors.Join(errs, fmt.Errorf("empty enum for resource attribute: %v", name))
181+
if attr.Enum != nil && len(attr.Enum) < 2 {
182+
errs = errors.Join(errs, fmt.Errorf("enum for resource attribute %v must have at least two values", name))
183183
}
184184
}
185185
return errs
@@ -319,8 +319,8 @@ func (md *Metadata) validateAttributes(usedAttrs map[AttributeName]bool) error {
319319
if attr.EnabledPtr != nil {
320320
errs = errors.Join(errs, fmt.Errorf("enabled field is not allowed for regular attribute: %v", attrName))
321321
}
322-
if attr.Enum != nil && len(attr.Enum) == 0 {
323-
errs = errors.Join(errs, fmt.Errorf("empty enum for attribute: %v", attrName))
322+
if attr.Enum != nil && len(attr.Enum) < 2 {
323+
errs = errors.Join(errs, fmt.Errorf("enum for attribute %v must have at least two values", attrName))
324324
}
325325
if !usedAttrs[attrName] {
326326
unusedAttrs = append(unusedAttrs, attrName)
@@ -733,9 +733,6 @@ func (a Attribute) TestValueTwo() string {
733733
if len(a.Enum) > 1 {
734734
return fmt.Sprintf(`%q`, a.Enum[1])
735735
}
736-
if len(a.Enum) == 1 {
737-
return fmt.Sprintf(`%q`, a.Enum[0])
738-
}
739736
switch a.Type.ValueType {
740737
case pcommon.ValueTypeEmpty:
741738
return ""

cmd/mdatagen/internal/metadata_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,19 @@ func TestValidate(t *testing.T) {
133133
},
134134
{
135135
name: "testdata/empty_enum_attr.yaml",
136-
wantErr: "empty enum for attribute: string_attr",
136+
wantErr: "enum for attribute string_attr must have at least two values",
137+
},
138+
{
139+
name: "testdata/single_enum_attr.yaml",
140+
wantErr: "enum for attribute string_attr must have at least two values",
137141
},
138142
{
139143
name: "testdata/empty_enum_rattr.yaml",
140-
wantErr: "empty enum for resource attribute: string.resource.attr",
144+
wantErr: "enum for resource attribute string.resource.attr must have at least two values",
145+
},
146+
{
147+
name: "testdata/single_enum_rattr.yaml",
148+
wantErr: "enum for resource attribute string.resource.attr must have at least two values",
141149
},
142150
{
143151
name: "testdata/entity_undefined_id_attribute.yaml",
@@ -358,7 +366,7 @@ func TestAttributeTestValueAndTestValueTwo(t *testing.T) {
358366
Enum: []string{"default"},
359367
},
360368
wantTestVal: `"default"`,
361-
wantTestVal2: `"default"`,
369+
wantTestVal2: `"mode-val-2"`,
362370
},
363371
{
364372
name: "empty enum slice",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
type: file
2+
3+
status:
4+
class: receiver
5+
stability:
6+
beta: [metrics]
7+
8+
attributes:
9+
string_attr:
10+
description: string attribute description
11+
type: string
12+
enum: ["default"]
13+
14+
metrics:
15+
default.metric:
16+
enabled: true
17+
description: Monotonic cumulative sum int metric enabled by default.
18+
stability: development
19+
unit: s
20+
sum:
21+
value_type: int
22+
monotonic: true
23+
aggregation_temporality: cumulative
24+
attributes: [string_attr]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type: file
2+
3+
status:
4+
class: receiver
5+
stability:
6+
beta: [metrics]
7+
8+
resource_attributes:
9+
string.resource.attr:
10+
description: string resource attribute
11+
type: string
12+
enabled: true
13+
enum: ["default"]

0 commit comments

Comments
 (0)