Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/go_validators_mdatagen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
component: cmd/mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added ability to generate required fields validation to config go structs generated by mdatagen

# One or more tracking issues or pull requests related to the change
issues: [14563]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
96 changes: 79 additions & 17 deletions cmd/mdatagen/internal/cfggen/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func NewCfgFns(rootPackage, componentPackage string) map[string]any {
}
return ExtractDefs(cfg)
},
"extractValidators": func(cfg *ConfigMetadata) []Validator {
if cfg == nil {
return nil
}
return ExtractValidators(cfg)
},
"mapGoType": func(cfg *ConfigMetadata, propName string) string {
if cfg == nil {
return "any"
Expand Down Expand Up @@ -256,24 +262,80 @@ func collectDefs(md *ConfigMetadata, defs map[string]*ConfigMetadata) {
}

for propName, prop := range md.Properties {
// if is embedded object
if prop.Type == "object" {
if len(prop.Properties) > 0 {
defs[propName] = prop
collectDefs(prop, defs)
}
ap := md.AdditionalProperties
if ap != nil && ap.Type == "object" && len(ap.Properties) > 0 {
defs[propName] = ap
collectDefs(ap, defs)
}
collectDefsForSchema(propName, prop, defs)
}

for _, schema := range md.AllOf {
collectDefs(schema, defs)
}
}

func collectDefsForSchema(propName string, md *ConfigMetadata, defs map[string]*ConfigMetadata) {
if md == nil || md.Ref != "" || md.GoType != "" {
return
}

switch md.Type {
case "object":
if len(md.Properties) > 0 {
defs[propName] = md
collectDefs(md, defs)
} else if md.AdditionalProperties != nil {
// map[string]V — the value type V inherits the same propName
collectDefsForSchema(propName, md.AdditionalProperties, defs)
}
if prop.Type == "array" {
if prop.Items != nil && prop.Items.Type == "object" && len(prop.Items.Properties) > 0 {
defName := propName + "_item"
defs[defName] = prop.Items
collectDefs(prop.Items, defs)
}
case "array":
if md.Items != nil {
// []T — item type uses propName+"_item", matching MapGoType
collectDefsForSchema(propName+"_item", md.Items, defs)
}
}
}

// ExtractValidators recursively scans the ConfigMetadata and collects validators for required fields and nested schemas.
func ExtractValidators(md *ConfigMetadata) []Validator {
validators := make([]Validator, 0)

if md == nil {
return validators
}
collectValidators(md, &validators)

return validators
}

type Validator struct {
FieldName string
FieldType string
IsRequired bool
IsPointer bool
IsOptional bool
}

func collectValidators(md *ConfigMetadata, validators *[]Validator) {
for propName, prop := range md.Properties {
isRequired := slices.Contains(md.Required, propName)
if isRequired {
*validators = append(*validators, Validator{
FieldName: propName,
FieldType: resolveType(prop),
IsRequired: isRequired,
IsPointer: prop.IsPointer,
IsOptional: prop.IsOptional,
})
}
}
}

func resolveType(md *ConfigMetadata) string {
switch {
case md.Ref != "":
return "ref"
case md.Type == "string" && md.Format == "date-time":
return "datetime"
case md.Type == "string" && md.Format == "duration":
return "duration"
default:
return md.Type
}
}
183 changes: 165 additions & 18 deletions cmd/mdatagen/internal/cfggen/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,24 +793,6 @@ func TestExtractDefs_EmptyInput(t *testing.T) {
require.Empty(t, result)
}

func TestExtractDefs_AdditionalPropertiesObject(t *testing.T) {
md := &ConfigMetadata{
Type: "object",
Properties: map[string]*ConfigMetadata{
// A property of type "object" with no sub-properties triggers the ap check
"extra": {Type: "object"},
},
AdditionalProperties: &ConfigMetadata{
Type: "object",
Properties: map[string]*ConfigMetadata{
"value": {Type: "integer"},
},
},
}
result := ExtractDefs(md)
require.Contains(t, result, "extra")
}

func TestNewCfgFns_ExtractImports(t *testing.T) {
fns := NewCfgFns("go.opentelemetry.io/collector", "go.opentelemetry.io/collector/comp")

Expand Down Expand Up @@ -982,3 +964,168 @@ func TestExtractImports_ContentSchema(t *testing.T) {
require.NoError(t, err)
require.Contains(t, result, "time")
}

func TestExtractValidators(t *testing.T) {
tests := []struct {
name string
metadata *ConfigMetadata
expected []Validator
}{
{
name: "no validators",
metadata: &ConfigMetadata{
Type: "string",
},
expected: []Validator{},
},
{
name: "nil config",
metadata: nil,
expected: []Validator{},
},
{
name: "required string field",
metadata: &ConfigMetadata{
Type: "object",
Required: []string{"name"},
Properties: map[string]*ConfigMetadata{
"name": {Type: "string"},
},
},
expected: []Validator{
{
FieldName: "name",
FieldType: "string",
IsRequired: true,
IsOptional: false,
IsPointer: false,
},
},
},
{
name: "required optional field",
metadata: &ConfigMetadata{
Type: "object",
Required: []string{"name"},
Properties: map[string]*ConfigMetadata{
"name": {Type: "string", IsOptional: true},
},
},
expected: []Validator{
{
FieldName: "name",
FieldType: "string",
IsRequired: true,
IsOptional: true,
IsPointer: false,
},
},
},
{
name: "required pointer field",
metadata: &ConfigMetadata{
Type: "object",
Required: []string{"name"},
Properties: map[string]*ConfigMetadata{
"name": {Type: "string", IsPointer: true},
},
},
expected: []Validator{
{
FieldName: "name",
FieldType: "string",
IsRequired: true,
IsOptional: false,
IsPointer: true,
},
},
},
{
name: "object with additionalProperties but no required children emits nothing",
metadata: &ConfigMetadata{
Type: "object",
Properties: map[string]*ConfigMetadata{
"labels": {
Type: "object",
AdditionalProperties: &ConfigMetadata{
Type: "string",
},
},
},
},
expected: []Validator{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := ExtractValidators(test.metadata)
require.Equal(t, test.expected, result)
})
}
}

func TestExtractValidators_InternalRefFromDefs_NoValidators(t *testing.T) {
md := &ConfigMetadata{
Ref: "plain_config",
}

result := ExtractValidators(md)
require.Empty(t, result)
}

func TestExtractValidators_InternalRefFromDefs_RefNotFound(t *testing.T) {
md := &ConfigMetadata{
Ref: "missing_def",
}

result := ExtractValidators(md)
require.Empty(t, result)
}

func TestExtractValidators_AllOf_NoRef(t *testing.T) {
md := &ConfigMetadata{
Type: "object",
AllOf: []*ConfigMetadata{
{
Type: "object",
Properties: map[string]*ConfigMetadata{
"host": {Type: "string"},
},
},
},
}

result := ExtractValidators(md)
require.Empty(t, result)
}

func TestExtractValidators_AllOf_RefWithNoValidators(t *testing.T) {
md := &ConfigMetadata{
Type: "object",
AllOf: []*ConfigMetadata{
{Ref: "empty_base"},
},
}

result := ExtractValidators(md)
require.Empty(t, result)
}

func TestNewCfgFns_ExtractValidators(t *testing.T) {
fns := NewCfgFns("", "")
extractValidators := fns["extractValidators"].(func(*ConfigMetadata) []Validator)

require.Nil(t, extractValidators(nil))

md := &ConfigMetadata{
Type: "object",
Required: []string{"name"},
Properties: map[string]*ConfigMetadata{
"name": {Type: "string"},
},
}
result := extractValidators(md)
require.Len(t, result, 1)
require.Equal(t, "name", result[0].FieldName)
require.True(t, result[0].IsRequired)
}
2 changes: 1 addition & 1 deletion cmd/mdatagen/internal/cfggen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ConfigMetadata struct {
Title string `mapstructure:"title,omitempty" json:"title,omitempty" yaml:"title,omitempty"`
Description string `mapstructure:"description,omitempty" json:"description,omitempty" yaml:"description,omitempty"`
Comment string `mapstructure:"$comment,omitempty" json:"$comment,omitempty" yaml:"$comment,omitempty"`
Type any `mapstructure:"type,omitempty" json:"type,omitempty" yaml:"type,omitempty"`
Type string `mapstructure:"type,omitempty" json:"type,omitempty" yaml:"type,omitempty"`
Ref string `mapstructure:"$ref,omitempty" json:"-" yaml:"$ref,omitempty"`
Default any `mapstructure:"default,omitempty" json:"default,omitempty" yaml:"default,omitempty"`
Examples []any `mapstructure:"examples,omitempty" json:"examples,omitempty" yaml:"examples,omitempty"`
Expand Down
7 changes: 1 addition & 6 deletions cmd/mdatagen/internal/cfggen/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestConfigMetadata_Validate_TypeAsInterface(t *testing.T) {
// might produce different types
tests := []struct {
name string
typeVal any
typeVal string
wantErr bool
}{
{
Expand All @@ -233,11 +233,6 @@ func TestConfigMetadata_Validate_TypeAsInterface(t *testing.T) {
typeVal: "string",
wantErr: true,
},
{
name: "type as array of strings (union type) - not supported",
typeVal: []any{"object", "null"},
wantErr: true, // Current implementation doesn't handle union types, treats as invalid
},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdatagen/internal/cfggen/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestResolver_ResolveSchema_UnknownInternalReference(t *testing.T) {
// Should use "any" type because the internal reference doesn't exist
result, err := resolver.ResolveSchema(src)
require.NoError(t, err)
require.Nil(t, result.Properties["config"].Type)
require.Empty(t, result.Properties["config"].Type)
}

func TestResolver_ResolveSchema_NestedStructures(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions cmd/mdatagen/internal/samplereceiver/generated_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading