Skip to content

Commit

Permalink
refactor: move extractTypeFromExpression to utils_ast.go
Browse files Browse the repository at this point in the history
  • Loading branch information
khatibomar committed May 11, 2024
1 parent 644d035 commit a21a979
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
33 changes: 0 additions & 33 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"go/ast"
"path/filepath"
"strings"
)
Expand All @@ -14,38 +13,6 @@ func joinLinuxPath(elem ...string) string {
return joinedPath
}

func extractTypeFromExpression(expr ast.Expr) string {
switch expr := expr.(type) {
case *ast.Ident:
return expr.Name
case *ast.StarExpr:
return "*" + extractTypeFromExpression(expr.X)
case *ast.ArrayType:
return "[]" + extractTypeFromExpression(expr.Elt)
case *ast.MapType:
return "map[" + extractTypeFromExpression(expr.Key) + "]" + extractTypeFromExpression(expr.Value)
case *ast.StructType:
return "struct{}"
case *ast.InterfaceType:
return "interface{}"
case *ast.ChanType:
var dir string
switch expr.Dir {
case ast.SEND:
dir = "chan<- "
case ast.RECV:
dir = "<-chan "
default:
dir = "chan "
}
return dir + extractTypeFromExpression(expr.Value)
case *ast.SelectorExpr:
return extractTypeFromExpression(expr.X) + "." + expr.Sel.Name
default:
return "unknown"
}
}

func in(target string, list []string) bool {
for _, item := range list {
if item == target {
Expand Down
32 changes: 32 additions & 0 deletions utils_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,35 @@ func getFields(node *ast.File, typeName string) ([]Field, error) {

return fields, err
}

func extractTypeFromExpression(expr ast.Expr) string {
switch expr := expr.(type) {
case *ast.Ident:
return expr.Name
case *ast.StarExpr:
return "*" + extractTypeFromExpression(expr.X)
case *ast.ArrayType:
return "[]" + extractTypeFromExpression(expr.Elt)
case *ast.MapType:
return "map[" + extractTypeFromExpression(expr.Key) + "]" + extractTypeFromExpression(expr.Value)
case *ast.StructType:
return "struct{}"
case *ast.InterfaceType:
return "interface{}"
case *ast.ChanType:
var dir string
switch expr.Dir {
case ast.SEND:
dir = "chan<- "
case ast.RECV:
dir = "<-chan "
default:
dir = "chan "
}
return dir + extractTypeFromExpression(expr.Value)
case *ast.SelectorExpr:
return extractTypeFromExpression(expr.X) + "." + expr.Sel.Name
default:
return "unknown"
}
}

0 comments on commit a21a979

Please sign in to comment.