/
strings.go
264 lines (248 loc) · 8.13 KB
/
strings.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package puml
import (
"fmt"
"go/ast"
"go/doc"
"go/token"
"go/types"
"strings"
)
const (
Main = "main"
ClassModifier = "class"
InterfaceModifier = "interface"
PublicModifier = "+"
PrivateModifier = "-"
StaticModifier = "{static}"
FieldModifier = "{field}"
MethodModifier = "{method}"
PrototypeArea = "..prototype.."
ConstArea = "..const.."
VarArea = "..var.."
FuncArea = "..func.."
FieldArea = "..field.."
MethodArea = "..method.."
SupperArea = "..supper.."
ConstructorArea = "..constructor.."
ScopeArea = "==scope=="
SPkgHeader = "package %s {"
)
type DepKind int
const (
SupperWeek DepKind = 1 << iota
SupperStrong
SpecWeek
SpecStrong
ArrayWeek
ArrayStrong
)
func DepString(a, b, indent string, kind DepKind) string {
switch kind {
case SupperWeek:
return indent + a + " ..|> " + b
case SupperStrong:
return indent + a + " --|> " + b
case SpecWeek:
return indent + a + " ..> " + b
case SpecStrong:
return indent + a + " --> " + b
case ArrayWeek:
return indent + a + " *..> " + b
case ArrayStrong:
return indent + a + " *--> " + b
default:
return indent + a + " .. " + b
}
}
func (k DepKind) IsStrong() bool {
return k == SupperStrong || k == SpecStrong || k == ArrayStrong
}
func EscapeString(text string) string {
if len(text) > 0 {
if len(text) > 32 {
text = text[:32] + " .."
}
text = strings.ReplaceAll(text, "\\r", "\\\\r")
text = strings.ReplaceAll(text, "\\n", "\\\\n")
text = strings.ReplaceAll(text, "\\t", "\\\\t")
text = strings.ReplaceAll(text, "\r", "\\\\r")
text = strings.ReplaceAll(text, "\n", "\\\\n")
text = strings.ReplaceAll(text, "\t", "\\\\t")
}
return text
}
func AstValueSpecString(spec ast.Spec, sep string) string {
if spec != nil {
var names, values []string
for _, ident := range spec.(*ast.ValueSpec).Names {
names = append(names, ident.Name)
}
for _, expr := range spec.(*ast.ValueSpec).Values {
values = append(values, types.ExprString(expr))
}
n, v := strings.Join(names, ", "), EscapeString(strings.Join(values, ", "))
if len(v) > 0 {
if token.IsExported(n) {
return fmt.Sprintf("%s %s %s %s %s", FieldModifier, PublicModifier, n, sep, v)
} else {
return fmt.Sprintf("%s %s %s %s %s", FieldModifier, PrivateModifier, n, sep, v)
}
} else {
if token.IsExported(n) {
return fmt.Sprintf("%s %s %s", FieldModifier, PublicModifier, n)
} else {
return fmt.Sprintf("%s %s %s", FieldModifier, PrivateModifier, n)
}
}
}
return ""
}
func AstFuncString(fc *ast.FuncType, pkgName, fcName string) string {
if fc != nil {
decl := strings.Replace(types.ExprString(fc), "func", fcName, 1)
if pkgName == Main && fcName == Main {
return fmt.Sprintf("%s %s **%s**", MethodModifier, PublicModifier, decl)
} else {
if token.IsExported(fcName) {
return fmt.Sprintf("%s %s %s", MethodModifier, PublicModifier, decl)
} else {
return fmt.Sprintf("%s %s %s", MethodModifier, PrivateModifier, decl)
}
}
}
return ""
}
func DocFuncString(fc *doc.Func, pkgName string) string {
if fc != nil && fc.Decl != nil {
return AstFuncString(fc.Decl.Type, pkgName, fc.Name)
}
return ""
}
func DocValuesStrings(values []*doc.Value, indent, sep string) string {
var builder strings.Builder
for _, value := range values {
for _, spec := range value.Decl.Specs {
builder.WriteString(fmt.Sprintf("%s%s\n", indent, AstValueSpecString(spec, sep)))
}
}
return builder.String()
}
func DocFuncsStrings(fcs []*doc.Func, indent, pkgName string) string {
var builder strings.Builder
for _, fc := range fcs {
builder.WriteString(fmt.Sprintf("%s%s\n", indent, DocFuncString(fc, pkgName)))
}
return builder.String()
}
func StructFieldsStrings(styp *ast.StructType, indent string) (supers, fields []string) {
if styp != nil && styp.Fields != nil {
for _, field := range styp.Fields.List {
var names []string
for _, ident := range field.Names {
names = append(names, ident.Name)
}
t := types.ExprString(field.Type)
if len(field.Names) == 0 {
supers = append(supers, fmt.Sprintf("%s%s %s\n", indent, StaticModifier, t))
} else {
n := strings.Join(names, ", ")
if token.IsExported(n) {
fields = append(fields, fmt.Sprintf("%s%s %s %s : %s\n", indent, FieldModifier, PublicModifier, n, t))
} else {
fields = append(fields, fmt.Sprintf("%s%s %s %s : %s\n", indent, FieldModifier, PrivateModifier, n, t))
}
}
}
}
return
}
func InterfaceFieldsStrings(ityp *ast.InterfaceType, indent string) (supers, methods []string) {
if ityp != nil && ityp.Methods != nil {
for _, method := range ityp.Methods.List {
if len(method.Names) == 0 {
supers = append(supers, fmt.Sprintf("%s%s %s\n", indent, StaticModifier, types.ExprString(method.Type)))
} else {
methods = append(methods, fmt.Sprintf("%s%s\n", indent, AstFuncString(method.Type.(*ast.FuncType), "", method.Names[0].Name)))
}
}
}
return
}
func DocGlobalScopeString(cons []*doc.Value, vrs []*doc.Value, fcs []*doc.Func, indentHead, PkgNumberName string) string {
var builder strings.Builder
indentBody := indentHead + "\t"
if len(cons)+len(vrs)+len(fcs) > 0 {
builder.WriteString(fmt.Sprintf("%s%s %s.* << (G,DarkSeaGreen) >> {\n", indentHead, ClassModifier, PkgNumberName))
if len(cons) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, ConstArea, DocValuesStrings(cons, indentBody, "=")))
}
if len(vrs) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, VarArea, DocValuesStrings(vrs, indentBody, ":")))
}
if len(fcs) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, FuncArea, DocFuncsStrings(fcs, indentBody, SelectorTail(PkgNumberName))))
}
builder.WriteString(fmt.Sprintf("%s}\n", indentHead))
}
return builder.String()
}
func SelectorTail(PkgNumberName string) string {
if strings.Index(PkgNumberName, ".") != -1 {
split := strings.Split(PkgNumberName, ".")
return split[len(split)-1]
}
return PkgNumberName
}
func DocTypeString(dtyp *doc.Type, indentHead string, PkgNumberName string) string {
var builder strings.Builder
indentBody := indentHead + "\t"
for _, spec := range dtyp.Decl.Specs {
name := any(spec).(*ast.TypeSpec).Name.Name
switch typ := any(spec).(*ast.TypeSpec).Type.(type) {
case *ast.InterfaceType:
builder.WriteString(fmt.Sprintf("%s%s %s.%s {\n", indentHead, InterfaceModifier, PkgNumberName, name))
supers, methods := InterfaceFieldsStrings(typ, indentBody)
if len(supers) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, SupperArea, strings.Join(supers, "")))
}
if len(methods) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, MethodArea, strings.Join(methods, "")))
}
default:
builder.WriteString(fmt.Sprintf("%s%s %s.%s {\n", indentHead, ClassModifier, PkgNumberName, name))
switch typ.(type) {
case *ast.StructType:
switch typ.(type) {
case *ast.StructType:
supers, fields := StructFieldsStrings(typ.(*ast.StructType), indentBody)
if len(supers) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, SupperArea, strings.Join(supers, "")))
}
if len(fields) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, FieldArea, strings.Join(fields, "")))
}
}
if len(dtyp.Methods) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, MethodArea, DocFuncsStrings(dtyp.Methods, indentBody, "")))
}
if len(dtyp.Funcs) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n%s", indentBody, ConstructorArea, DocFuncsStrings(dtyp.Funcs, indentBody, "")))
}
if len(dtyp.Vars)+len(dtyp.Consts) > 0 {
builder.WriteString(fmt.Sprintf("%s%s\n", indentBody, ScopeArea))
if len(dtyp.Consts) > 0 {
builder.WriteString(DocValuesStrings(dtyp.Consts, indentBody, "="))
}
if len(dtyp.Vars) > 0 {
builder.WriteString(DocValuesStrings(dtyp.Vars, indentBody, ":"))
}
}
default:
builder.WriteString(fmt.Sprintf("%s%s\n", indentBody, PrototypeArea))
builder.WriteString(fmt.Sprintf("%s%s\n", indentBody, types.ExprString(typ)))
}
}
builder.WriteString(fmt.Sprintf("%s}\n", indentHead))
}
return builder.String()
}