[mdatagen] Fix panic in TestValue() and TestValueTwo() for single-element or empty enum attributes#15646
Conversation
…ment or empty enum attributes Fix index-out-of-range panic in Attribute.TestValue() and Attribute.TestValueTwo() when handling attributes with single-element or empty enum slices. Also add validation in validateAttributes() and validateResourceAttributes() for empty Enum slices.
|
This is very much an edge case, I would argue an enum with less than 2 enumerations is not an enum. Given that if an enum is presented with less than 2 I would expect it to error, something like: if len(attr.Enum) == 1 {
errs = errors.Join(errs, fmt.Errorf("enum for attribute %v must have at least two values", attrName))
}If you want to use an enum with one value I would agrue that it should be a constant. |
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (57.14%) is below the target coverage (95.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #15646 +/- ##
==========================================
- Coverage 91.04% 91.04% -0.01%
==========================================
Files 728 729 +1
Lines 48467 48497 +30
==========================================
+ Hits 44129 44154 +25
- Misses 3011 3018 +7
+ Partials 1327 1325 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
|
Thanks for the feedback @osullivandonal! That makes complete sense — an enum with fewer than 2 values should be rejected as invalid metadata. I've updated validateAttributes() and validateResourceAttributes() to enforce that enum attributes must have at least two values, and added corresponding unit tests. |
Description
Fix index-out-of-range panic in
cmd/mdatagenwhen handling single-element enum attributes or empty enum slices. Also add validation invalidateAttributes()andvalidateResourceAttributes()to reject empty enum attributes.Link to tracking issue
Fixes #15644
Testing
Added unit tests in
cmd/mdatagen/internal/metadata_test.gocovering single-element, multi-element, empty enum, and non-enum attributes, as well as metadata validation tests. Verified all tests pass incmd/mdatagen.Documentation
None
Authorship