-
Notifications
You must be signed in to change notification settings - Fork 345
/
models.go
172 lines (147 loc) · 3.08 KB
/
models.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
package models
import (
"strings"
"unicode"
)
type Expression struct {
Value string
IsStar bool
IsVariadic bool
IsWriter bool
Underlying string
}
func (e *Expression) String() string {
value := e.Value
if e.IsStar {
value = "*" + value
}
if e.IsVariadic {
return "[]" + value
}
return value
}
type Field struct {
Name string
Type *Expression
Index int
}
func (f *Field) IsWriter() bool {
return f.Type.IsWriter
}
func (f *Field) IsStruct() bool {
return strings.HasPrefix(f.Type.Underlying, "struct")
}
func (f *Field) IsBasicType() bool {
return isBasicType(f.Type.String()) || isBasicType(f.Type.Underlying)
}
func isBasicType(t string) bool {
switch t {
case "bool", "string", "int", "int8", "int16", "int32", "int64", "uint",
"uint8", "uint16", "uint32", "uint64", "uintptr", "byte", "rune",
"float32", "float64", "complex64", "complex128":
return true
default:
return false
}
}
func (f *Field) IsNamed() bool {
return f.Name != "" && f.Name != "_"
}
func (f *Field) ShortName() string {
return strings.ToLower(string([]rune(f.Type.Value)[0]))
}
type Receiver struct {
*Field
Fields []*Field
}
type Function struct {
Name string
IsExported bool
Receiver *Receiver
Parameters []*Field
Results []*Field
ReturnsError bool
}
func (f *Function) TestParameters() []*Field {
var ps []*Field
for _, p := range f.Parameters {
if p.IsWriter() {
continue
}
ps = append(ps, p)
}
return ps
}
func (f *Function) TestResults() []*Field {
var ps []*Field
ps = append(ps, f.Results...)
for _, p := range f.Parameters {
if !p.IsWriter() {
continue
}
ps = append(ps, &Field{
Name: p.Name,
Type: &Expression{
Value: "string",
IsWriter: true,
Underlying: "string",
},
Index: len(ps),
})
}
return ps
}
func (f *Function) ReturnsMultiple() bool {
return len(f.Results) > 1
}
func (f *Function) OnlyReturnsOneValue() bool {
return len(f.Results) == 1 && !f.ReturnsError
}
func (f *Function) OnlyReturnsError() bool {
return len(f.Results) == 0 && f.ReturnsError
}
func (f *Function) FullName() string {
var r string
if f.Receiver != nil {
r = f.Receiver.Type.Value
}
return strings.Title(r) + strings.Title(f.Name)
}
func (f *Function) TestName() string {
if strings.HasPrefix(f.Name, "Test") {
return f.Name
}
if f.Receiver != nil {
receiverType := f.Receiver.Type.Value
if unicode.IsLower([]rune(receiverType)[0]) {
receiverType = "_" + receiverType
}
return "Test" + receiverType + "_" + f.Name
}
if unicode.IsLower([]rune(f.Name)[0]) {
return "Test_" + f.Name
}
return "Test" + f.Name
}
func (f *Function) IsNaked() bool {
return f.Receiver == nil && len(f.Parameters) == 0 && len(f.Results) == 0
}
type Import struct {
Name, Path string
}
type Header struct {
Comments []string
Package string
Imports []*Import
Code []byte
}
type Path string
func (p Path) TestPath() string {
if !p.IsTestPath() {
return strings.TrimSuffix(string(p), ".go") + "_test.go"
}
return string(p)
}
func (p Path) IsTestPath() bool {
return strings.HasSuffix(string(p), "_test.go")
}