Skip to content

Commit

Permalink
Merge pull request #39 from csueiras/fix-37-map-support
Browse files Browse the repository at this point in the history
FIX Issue #37: Map support
  • Loading branch information
csueiras authored Feb 27, 2021
2 parents cee11ee + 7aed450 commit 8b129a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/generator/method/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ func toType(t types.Type, variadic bool) (jen.Code, error) {
return jen.Index().Add(elemType), nil
case named:
return jen.Id(v.Name()), nil
case *types.Map:
keyType, err := toType(v.Key(), false)
if err != nil {
return nil, err
}
elemType, err := toType(v.Elem(), false)
if err != nil {
return nil, err
}
return jen.Map(keyType).Add(elemType), nil
default:
return nil, fmt.Errorf("type not handled: %T", v)
}
Expand Down
17 changes: 17 additions & 0 deletions internal/generator/method/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ func TestNewMethod(t *testing.T) {
ReturnTypes: []jen.Code{jen.Id("interface{}")},
},
},
{
name: "Fn(arg map[string]interface{}) map[string]int",
args: args{
name: "Fn",
signature: types.NewSignature(nil,
types.NewTuple(types.NewVar(token.NoPos, nil, "arg", types.NewMap(types.Typ[types.String], types.NewInterfaceType(nil, nil)))),
types.NewTuple(types.NewVar(token.NoPos, nil, "", types.NewMap(types.Typ[types.String], types.Typ[types.Int]))),
false),
},
want: &method.Method{
Name: "Fn",
HasContext: false,
ParameterNames: []string{"arg0"},
ParametersNameAndType: []jen.Code{jen.Id("arg0").Add(jen.Map(jen.Id("string")).Add(jen.Id("interface{}")))},
ReturnTypes: []jen.Code{jen.Map(jen.Id("string")).Add(jen.Id("int"))},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 8b129a8

Please sign in to comment.