From 7aed4501c7278e68e0ece9e5f69ad366c65c7d03 Mon Sep 17 00:00:00 2001 From: Christian Sueiras Date: Sat, 27 Feb 2021 11:11:37 -0500 Subject: [PATCH] FIX Issue #37: Map support --- internal/generator/method/method.go | 10 ++++++++++ internal/generator/method/method_test.go | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/internal/generator/method/method.go b/internal/generator/method/method.go index f776237..e94528a 100644 --- a/internal/generator/method/method.go +++ b/internal/generator/method/method.go @@ -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) } diff --git a/internal/generator/method/method_test.go b/internal/generator/method/method_test.go index cf10a92..99db205 100644 --- a/internal/generator/method/method_test.go +++ b/internal/generator/method/method_test.go @@ -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 {