[cmd/mdatagen] Generate required fields validation for go config structs - #14768
Conversation
Merging this PR will not alter performance
|
Codecov Report❌ Patch coverage is ❌ Your patch status has failed because the patch coverage (54.68%) 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 #14768 +/- ##
==========================================
- Coverage 91.38% 91.33% -0.05%
==========================================
Files 695 697 +2
Lines 44405 44456 +51
==========================================
+ Hits 40579 40604 +25
- Misses 2690 2714 +24
- Partials 1136 1138 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This is not needed. optional and required are contradictory |
Makes sense. I removed it. |
c985f1f
…cts (open-telemetry#14768) <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR adds automatic generation of Validate() methods for Go config structs produced by mdatagen, based on the JSON Schema config metadata. When a config field is marked required in the config schema, the generated Validate() method emits the appropriate nil/empty check, handling all field kinds: - IsPointer → nil check - IsOptional (configoptional) → .HasValue() check - string → empty string check - array → len() == 0 check - object (map) → nil or empty map check Validation is also propagated recursively — if a field is an embedded struct, map, or slice whose element type itself has required fields, the generated Validate() calls the child type's Validate() method. Internal $ref and allOf references are resolved through internal $defs and validated the same way. #### Example Given a metadata.yaml with: ```yaml config: type: object required: [targets] properties: targets: type: array items: type: object required: [url] properties: url: type: string ``` The generated code produces: ```go func (c *TargetsItem) Validate() error { var err error if c.URL == "" { err = errors.Join(err, errors.New("URL is required")) } return err } func (c *Config) Validate() error { var err error if len(c.Targets) == 0 { err = errors.Join(err, errors.New("Targets is required")) } for _, value := range c.Targets { err = errors.Join(err, value.Validate()) } return err } ``` <!-- Issue number if applicable --> #### Link to tracking issue open-telemetry#14563 <!--Describe what testing was performed and which tests were added.--> #### Testing 1. Unit tests 2. Added use cases to samplescraper and samplereceiver
Description
This PR adds automatic generation of Validate() methods for Go config structs produced by mdatagen, based on the JSON Schema config metadata.
When a config field is marked required in the config schema, the generated Validate() method emits the appropriate nil/empty check, handling all field kinds:
Validation is also propagated recursively — if a field is an embedded struct, map, or slice whose element type itself has required fields, the generated Validate() calls the child type's Validate() method. Internal $ref and allOf references are resolved through internal $defs and validated the same way.
Example
Given a metadata.yaml with:
The generated code produces:
Link to tracking issue
#14563
Testing