Skip to content

Commit

Permalink
if typeSpec elt is interface zeroValue should be nil (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luoxin authored Feb 4, 2021
1 parent 2a685ad commit a3a59d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ func getKeyAndElementType(pkg *ast.Package, name string, typeSpec *ast.TypeSpec)
if t, ok := typeSpec.Type.(*ast.ArrayType); ok {
explorer := NewTypeExplorer(pkg, getIdentName(t.Elt))

switch v := t.Elt.(type) {
case *ast.Ident:
if v == nil || v.Obj == nil {
break
}

if ts, ok := v.Obj.Decl.(*ast.TypeSpec); ok {
if _, ok := ts.Type.(*ast.InterfaceType); ok {
explorer.IsInterface = true
}
}

}

return pkgName, "", getIdentName(t.Elt), explorer
}

Expand Down Expand Up @@ -209,7 +223,7 @@ func main() {

// If its a pointer we need to replace '*' -> '&' when
// instantiating.
if elementType[0] == '*' {
if elementType[0] == '*' || explorer.IsInterface {
zeroValue = "nil"
}

Expand Down
5 changes: 3 additions & 2 deletions type_explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

type TypeExplorer struct {
TypeName string
Methods []string
TypeName string
Methods []string
IsInterface bool
}

func NewTypeExplorer(pkg *ast.Package, typeName string) *TypeExplorer {
Expand Down

0 comments on commit a3a59d9

Please sign in to comment.