forked from jfeliu007/goplantuml
/
field.go
196 lines (169 loc) · 5.02 KB
/
field.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package parser
import (
"fmt"
"strings"
"go/ast"
)
const packageConstant = "{packageName}"
//Field can hold the name and type of any field
type Field struct {
Name string
Type string
FullType string
}
//Returns a string representation of the given expression if it was recognized.
//Refer to the implementation to see the different string representations.
func getFieldType(exp ast.Expr, aliases map[string]string) (string, []string) {
switch v := exp.(type) {
case *ast.Ident:
return getIdent(v, aliases)
case *ast.ArrayType:
return getArrayType(v, aliases)
case *ast.SelectorExpr:
return getSelectorExp(v, aliases)
case *ast.MapType:
return getMapType(v, aliases)
case *ast.StarExpr:
return getStarExp(v, aliases)
case *ast.ChanType:
return getChanType(v, aliases)
case *ast.StructType:
return getStructType(v, aliases)
case *ast.InterfaceType:
return getInterfaceType(v, aliases)
case *ast.FuncType:
return getFuncType(v, aliases)
case *ast.Ellipsis:
return getEllipsis(v, aliases)
}
return "", []string{}
}
func getIdent(v *ast.Ident, aliases map[string]string) (string, []string) {
if isPrimitive(v) {
return v.Name, []string{}
}
t := fmt.Sprintf("%s%s", packageConstant, v.Name)
return t, []string{t}
}
func getArrayType(v *ast.ArrayType, aliases map[string]string) (string, []string) {
t, fundamentalTypes := getFieldType(v.Elt, aliases)
return fmt.Sprintf("[]%s", t), fundamentalTypes
}
func getSelectorExp(v *ast.SelectorExpr, aliases map[string]string) (string, []string) {
packageName := v.X.(*ast.Ident).Name
if realPackageName, ok := aliases[packageName]; ok {
packageName = realPackageName
}
t := fmt.Sprintf("%s.%s", packageName, v.Sel.Name)
return t, []string{t}
}
func getMapType(v *ast.MapType, aliases map[string]string) (string, []string) {
t1, f1 := getFieldType(v.Key, aliases)
t2, f2 := getFieldType(v.Value, aliases)
return fmt.Sprintf("<font color=blue>map</font>[%s]%s", t1, t2), append(f1, f2...)
}
func getStarExp(v *ast.StarExpr, aliases map[string]string) (string, []string) {
t, f := getFieldType(v.X, aliases)
return fmt.Sprintf("*%s", t), f
}
func getChanType(v *ast.ChanType, aliases map[string]string) (string, []string) {
t, f := getFieldType(v.Value, aliases)
return fmt.Sprintf("<font color=blue>chan</font> %s", t), f
}
func getStructType(v *ast.StructType, aliases map[string]string) (string, []string) {
fieldList := make([]string, 0)
for _, field := range v.Fields.List {
t, _ := getFieldType(field.Type, aliases)
fieldList = append(fieldList, t)
}
return fmt.Sprintf("<font color=blue>struct</font>{%s}", strings.Join(fieldList, ", ")), []string{}
}
func getInterfaceType(v *ast.InterfaceType, aliases map[string]string) (string, []string) {
methods := make([]string, 0)
for _, field := range v.Methods.List {
methodName := ""
if field.Names != nil {
methodName = field.Names[0].Name
}
t, _ := getFieldType(field.Type, aliases)
methods = append(methods, methodName+" "+t)
}
return fmt.Sprintf("<font color=blue>interface</font>{%s}", strings.Join(methods, "; ")), []string{}
}
func getFuncType(v *ast.FuncType, aliases map[string]string) (string, []string) {
function := getFunction(v, "", aliases, "")
params := make([]string, 0)
for _, pa := range function.Parameters {
params = append(params, pa.Type)
}
returns := ""
returnList := make([]string, 0)
for _, re := range function.ReturnValues {
returnList = append(returnList, re)
}
if len(returnList) > 1 {
returns = fmt.Sprintf("(%s)", strings.Join(returnList, ", "))
} else {
returns = strings.Join(returnList, "")
}
return fmt.Sprintf("<font color=blue>func</font>(%s) %s", strings.Join(params, ", "), returns), []string{}
}
func getEllipsis(v *ast.Ellipsis, aliases map[string]string) (string, []string) {
t, _ := getFieldType(v.Elt, aliases)
return fmt.Sprintf("...%s", t), []string{}
}
var globalPrimitives = map[string]struct{}{
"bool": {},
"string": {},
"int": {},
"int8": {},
"int16": {},
"int32": {},
"int64": {},
"uint": {},
"uint8": {},
"uint16": {},
"uint32": {},
"uint64": {},
"uintptr": {},
"byte": {},
"rune": {},
"float32": {},
"float64": {},
"complex64": {},
"complex128": {},
"error": {},
"*bool": {},
"*string": {},
"*int": {},
"*int8": {},
"*int16": {},
"*int32": {},
"*int64": {},
"*uint": {},
"*uint8": {},
"*uint16": {},
"*uint32": {},
"*uint64": {},
"*uintptr": {},
"*byte": {},
"*rune": {},
"*float32": {},
"*float64": {},
"*complex64": {},
"*complex128": {},
"*error": {},
}
func isPrimitive(ty *ast.Ident) bool {
return isPrimitiveString(ty.Name)
}
func isPrimitiveString(t string) bool {
_, ok := globalPrimitives[t]
return ok
}
func replacePackageConstant(field, packageName string) string {
if packageName != "" {
packageName = fmt.Sprintf("%s.", packageName)
}
return strings.Replace(field, packageConstant, packageName, 1)
}