-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
137 lines (100 loc) · 2.08 KB
/
helpers.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
package pkgdmp
import (
"fmt"
"go/ast"
"go/printer"
"go/token"
"regexp"
"strings"
)
var fieldSTMap = map[SymbolType]struct{}{
SymbolStructField: {},
SymbolParamField: {},
SymbolResultField: {},
SymbolReceiverField: {},
}
var fieldTagRegexp = regexp.MustCompile(`(\w+):"(.*?)"`)
func identNames(idents []*ast.Ident) []string {
iLen := len(idents)
if iLen == 0 {
return nil
}
res := make([]string, iLen)
for i, ident := range idents {
res[i] = ident.Name
}
return res
}
func isExportedIdent(name string) bool {
return strings.ToUpper(name[:1]) == name[:1]
}
func isFieldSymbolType(st SymbolType) bool {
_, ok := fieldSTMap[st]
return ok
}
func mkComment(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return ""
}
var b strings.Builder
lines := strings.Split(s, "\n")
if len(lines) > 1 {
for _, line := range lines {
fmt.Fprintf(&b, "// %s\n", line)
}
return b.String()
}
lineLen, _ := fmt.Fprintf(&b, "// ")
words := strings.Fields(s)
for _, word := range words {
wLen := len(word)
if lineLen+wLen+1 < 80 {
n, _ := fmt.Fprintf(&b, "%s ", word)
lineLen += n
continue
}
lineLen, _ = fmt.Fprintf(&b, "\n// %s ", word)
}
b.WriteRune('\n')
return b.String()
}
func fieldsList(fl []Field) string {
fLen := len(fl)
if fLen == 0 {
return ""
}
res := make([]string, fLen)
for i, f := range fl {
res[i] = f.String()
}
return strings.Join(res, ", ")
}
func resultsList(fl []Field) string {
s := fieldsList(fl)
if len(fl) > 1 {
return fmt.Sprintf("(%s)", s)
}
return s
}
func printNodes(nodes any) string {
var b strings.Builder
fset := token.NewFileSet()
printer.Fprint(&b, fset, nodes)
return strings.TrimSpace(b.String())
}
func parseFieldTags(s string) [][]string {
s = strings.Trim(s, "`")
matches := fieldTagRegexp.FindAllStringSubmatch(s, -1)
if len(matches) == 0 {
return nil
}
tags := make([][]string, 0, len(matches))
for _, m := range matches {
name := m[1]
values := strings.Split(m[2], ",")
tag := append([]string{name}, values...)
tags = append(tags, tag)
}
return tags
}