-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringer.go
103 lines (100 loc) · 2.16 KB
/
stringer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package godeep
import (
"fmt"
"go/ast"
"reflect"
"strings"
)
/*
Creation Time: 2020 - Jan - 27
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2018
*/
func astExpr(x ast.Expr) string {
switch xx := x.(type) {
case *ast.Ident:
return astIdent(xx)
case *ast.Ellipsis:
return astEllipsis(xx)
case *ast.ArrayType:
return astArray(xx)
case *ast.SelectorExpr:
return astSelectorExpr(xx)
case *ast.StarExpr:
return astStarExpr(xx)
case *ast.MapType:
return astMapType(xx)
case *ast.InterfaceType:
return astInterfaceType(xx)
case *ast.FuncType:
return fmt.Sprintf("func %s", astFuncType(xx))
default:
return reflect.TypeOf(xx).String()
}
}
func astStarExpr(x *ast.StarExpr) string {
sb := strings.Builder{}
sb.WriteRune('*')
sb.WriteString(astExpr(x.X))
return sb.String()
}
func astMapType(x *ast.MapType) string {
sb := strings.Builder{}
sb.WriteString("map[")
sb.WriteString(astExpr(x.Key))
sb.WriteString("]")
sb.WriteString(astExpr(x.Value))
return sb.String()
}
func astInterfaceType(x *ast.InterfaceType) string {
return "interface{}"
}
func astFuncType(x *ast.FuncType) string {
sb := strings.Builder{}
sb.WriteRune('(')
for idx, p := range x.Params.List {
for idx, n := range p.Names {
sb.WriteString(n.Name)
if idx < len(p.Names)-1 {
sb.WriteString(", ")
}
}
sb.WriteRune(' ')
sb.WriteString(astExpr(p.Type))
if idx < len(x.Params.List)-1 {
sb.WriteString(", ")
}
}
sb.WriteRune(')')
return sb.String()
}
func astSelectorExpr(x *ast.SelectorExpr) string {
sb := strings.Builder{}
sb.WriteString(astExpr(x.X))
sb.WriteRune('.')
sb.WriteString(astIdent(x.Sel))
return sb.String()
}
func astIdent(x *ast.Ident) string {
return x.Name
}
func astEllipsis(x *ast.Ellipsis) string {
sb := strings.Builder{}
sb.WriteString("...")
sb.WriteString(astExpr(x.Elt))
return sb.String()
}
func astArray(x *ast.ArrayType) string {
sb := strings.Builder{}
sb.WriteString("[]")
switch xx := x.Elt.(type) {
case *ast.Ident:
sb.WriteString(astIdent(xx))
default:
sb.WriteString(reflect.TypeOf(x.Elt).String())
}
return sb.String()
}