Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paramgen support for maps and wildcards #37

Merged
merged 1 commit into from
Apr 16, 2024
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
41 changes: 41 additions & 0 deletions paramgen/internal/paramgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ func (p *parameterParser) parseTypeSpec(ts *ast.TypeSpec, f *ast.Field) (params
return p.parseSelectorExpr(v, f)
case *ast.Ident:
return p.parseIdent(v, f)
case *ast.MapType:
return p.parseMapType(v, f)
default:
return nil, fmt.Errorf("unexpected type: %T", ts.Type)
}
Expand Down Expand Up @@ -296,6 +298,8 @@ func (p *parameterParser) parseField(f *ast.Field) (params map[string]config.Par
return p.parseStructType(v, f)
case *ast.SelectorExpr:
return p.parseSelectorExpr(v, f)
case *ast.MapType:
return p.parseMapType(v, f)
case *ast.ArrayType:
strType := fmt.Sprintf("%s", v.Elt)
if !p.isBuiltinType(strType) && !strings.Contains(strType, "time Duration") {
Expand All @@ -312,6 +316,43 @@ func (p *parameterParser) parseField(f *ast.Field) (params map[string]config.Par
}
}

func (p *parameterParser) parseMapType(mt *ast.MapType, f *ast.Field) (params map[string]config.Parameter, err error) {
if fmt.Sprintf("%s", mt.Key) != "string" {
return nil, fmt.Errorf("unsupported map key type: %s", mt.Key)
}

// parse map value as if it was a field
var tmpParams map[string]config.Parameter
switch val := mt.Value.(type) {
case *ast.Ident:
// identifier (builtin type or type in same package)
tmpParams, err = p.parseIdent(val, f)
case *ast.StructType:
// nested type
tmpParams, err = p.parseStructType(val, f)
case *ast.SelectorExpr:
tmpParams, err = p.parseSelectorExpr(val, f)
}
if err != nil {
return nil, err
}

// inject wildcard
params = make(map[string]config.Parameter, len(tmpParams))
for k, p := range tmpParams {
index := strings.Index(k, ".")
if index == -1 {
index = len(k)
}
name := k[:index] + ".*"
if index < len(k) {
name += k[index:]
}
params[name] = p
}
return params, nil
}

func (p *parameterParser) parseSelectorExpr(se *ast.SelectorExpr, f *ast.Field) (params map[string]config.Parameter, err error) {
defer func() {
if err != nil {
Expand Down
51 changes: 34 additions & 17 deletions paramgen/internal/paramgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,26 @@ func TestParseSpecificationSuccess(t *testing.T) {
},
},
},
"myUint": {Type: config.ParameterTypeInt},
"myInt8": {Type: config.ParameterTypeInt},
"myUint8": {Type: config.ParameterTypeInt},
"myInt16": {Type: config.ParameterTypeInt},
"myUint16": {Type: config.ParameterTypeInt},
"myInt32": {Type: config.ParameterTypeInt},
"myUint32": {Type: config.ParameterTypeInt},
"myInt64": {Type: config.ParameterTypeInt},
"myUint64": {Type: config.ParameterTypeInt},
"myByte": {Type: config.ParameterTypeString},
"myRune": {Type: config.ParameterTypeInt},
"myFloat32": {Type: config.ParameterTypeFloat},
"myFloat64": {Type: config.ParameterTypeFloat},
"myDuration": {Type: config.ParameterTypeDuration},
"myIntSlice": {Type: config.ParameterTypeString},
"myFloatSlice": {Type: config.ParameterTypeString},
"myDurSlice": {Type: config.ParameterTypeString},
"myUint": {Type: config.ParameterTypeInt},
"myInt8": {Type: config.ParameterTypeInt},
"myUint8": {Type: config.ParameterTypeInt},
"myInt16": {Type: config.ParameterTypeInt},
"myUint16": {Type: config.ParameterTypeInt},
"myInt32": {Type: config.ParameterTypeInt},
"myUint32": {Type: config.ParameterTypeInt},
"myInt64": {Type: config.ParameterTypeInt},
"myUint64": {Type: config.ParameterTypeInt},
"myByte": {Type: config.ParameterTypeString},
"myRune": {Type: config.ParameterTypeInt},
"myFloat32": {Type: config.ParameterTypeFloat},
"myFloat64": {Type: config.ParameterTypeFloat},
"myDuration": {Type: config.ParameterTypeDuration},
"myIntSlice": {Type: config.ParameterTypeString},
"myFloatSlice": {Type: config.ParameterTypeString},
"myDurSlice": {Type: config.ParameterTypeString},
"myStringMap.*": {Type: config.ParameterTypeString},
"myStructMap.*.myInt": {Type: config.ParameterTypeInt},
"myStructMap.*.myString": {Type: config.ParameterTypeString},
},
},
{
Expand All @@ -88,6 +91,20 @@ func TestParseSpecificationSuccess(t *testing.T) {
Description: "Duration does not have a name so the type name is used.",
Type: config.ParameterTypeDuration,
},
"global.wildcardStrings.*": {
Default: "foo",
Type: config.ParameterTypeString,
Validations: []config.Validation{
config.ValidationRequired{},
},
},
"global.renamed.*": {
Default: "1s",
Type: config.ParameterTypeDuration,
},
"global.wildcardStructs.*.name": {
Type: config.ParameterTypeString,
},
"nestMeHere.anotherNested": {
Type: config.ParameterTypeInt,
Description: "AnotherNested is also nested under nestMeHere.\nThis is a block comment.",
Expand Down
8 changes: 8 additions & 0 deletions paramgen/internal/testdata/basic/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ type SourceConfig struct {
MyFloatSlice []float32
MyDurSlice []time.Duration

MyStringMap map[string]string
MyStructMap map[string]structMapVal

// this field is ignored because it is not exported
ignoreThis http.Client
}

type structMapVal struct {
MyString string
MyInt int
}
8 changes: 8 additions & 0 deletions paramgen/internal/testdata/complex/internal/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ import "time"
type GlobalConfig struct {
// Duration does not have a name so the type name is used.
time.Duration `default:"1s"` // line comments on fields with doc comments are ignored

WildcardStrings map[string]string `default:"foo" validate:"required"`
WildcardInts map[string]time.Duration `json:"renamed" default:"1s"`
WildcardStructs WildcardStruct
}

type WildcardStruct map[string]struct {
Name string
}
Loading