-
Notifications
You must be signed in to change notification settings - Fork 531
/
component.go
251 lines (224 loc) · 6.48 KB
/
component.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
package scaffolder
import (
"context"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/pkg/multiformatname"
"github.com/ignite/cli/v28/ignite/pkg/protoanalysis"
"github.com/ignite/cli/v28/ignite/templates/field/datatype"
)
const (
componentType = "type"
componentMessage = "message"
componentQuery = "query"
componentPacket = "packet"
protoFolder = "proto"
)
// checkComponentValidity performs various checks common to all components to verify if it can be scaffolded.
func checkComponentValidity(appPath, moduleName string, compName multiformatname.Name, noMessage bool) error {
ok, err := moduleExists(appPath, moduleName)
if err != nil {
return err
}
if !ok {
return errors.Errorf("the module %s doesn't exist", moduleName)
}
// Ensure the name is valid, otherwise it would generate an incorrect code
if err := checkForbiddenComponentName(compName); err != nil {
return errors.Errorf("%s can't be used as a component name: %w", compName.LowerCamel, err)
}
// Check component name is not already used
return checkComponentCreated(appPath, moduleName, compName, noMessage)
}
// checkComponentCreated checks if the component has been already created with Ignite in the project.
func checkComponentCreated(appPath, moduleName string, compName multiformatname.Name, noMessage bool) (err error) {
// associate the type to check with the component that scaffold this type
typesToCheck := map[string]string{
compName.UpperCamel: componentType,
fmt.Sprintf("queryall%srequest", compName.LowerCase): componentType,
fmt.Sprintf("queryall%sresponse", compName.LowerCase): componentType,
fmt.Sprintf("queryget%srequest", compName.LowerCase): componentType,
fmt.Sprintf("queryget%sresponse", compName.LowerCase): componentType,
fmt.Sprintf("query%srequest", compName.LowerCase): componentQuery,
fmt.Sprintf("query%sresponse", compName.LowerCase): componentQuery,
fmt.Sprintf("%spacketdata", compName.LowerCase): componentPacket,
}
if !noMessage {
typesToCheck[fmt.Sprintf("msgcreate%s", compName.LowerCase)] = componentType
typesToCheck[fmt.Sprintf("msgupdate%s", compName.LowerCase)] = componentType
typesToCheck[fmt.Sprintf("msgdelete%s", compName.LowerCase)] = componentType
typesToCheck[fmt.Sprintf("msg%s", compName.LowerCase)] = componentMessage
typesToCheck[fmt.Sprintf("msgsend%s", compName.LowerCase)] = componentPacket
}
absPath, err := filepath.Abs(filepath.Join(appPath, "x", moduleName, "types"))
if err != nil {
return err
}
fileSet := token.NewFileSet()
all, err := parser.ParseDir(fileSet, absPath, func(os.FileInfo) bool { return true }, parser.ParseComments)
if err != nil {
return err
}
for _, pkg := range all {
for _, f := range pkg.Files {
ast.Inspect(f, func(x ast.Node) bool {
typeSpec, ok := x.(*ast.TypeSpec)
if !ok {
return true
}
if _, ok := typeSpec.Type.(*ast.StructType); !ok {
return true
}
// Check if the parsed type is from a scaffolded component with the name
if compType, ok := typesToCheck[strings.ToLower(typeSpec.Name.Name)]; ok {
err = errors.Errorf("component %s with name %s is already created (type %s exists)",
compType,
compName.Original,
typeSpec.Name.Name,
)
return false
}
return true
})
if err != nil {
return err
}
}
}
return err
}
// checkCustomTypes returns error if one of the types is invalid.
func checkCustomTypes(ctx context.Context, path, appName, module string, fields []string) error {
protoPath := filepath.Join(path, protoFolder, appName, module)
customFieldTypes := make([]string, 0)
for _, field := range fields {
ft, ok := fieldType(field)
if !ok {
continue
}
if _, ok := datatype.IsSupportedType(datatype.Name(ft)); !ok {
customFieldTypes = append(customFieldTypes, ft)
}
}
return protoanalysis.HasMessages(ctx, protoPath, customFieldTypes...)
}
// checkForbiddenComponentName returns true if the name is forbidden as a component name.
func checkForbiddenComponentName(name multiformatname.Name) error {
// Check with names already used from the scaffolded code
switch name.LowerCase {
case
"oracle",
"logger",
"keeper",
"query",
"genesis",
"types",
"tx",
datatype.TypeCustom:
return errors.Errorf("%s is used by Ignite scaffolder", name.LowerCamel)
}
if strings.HasSuffix(name.LowerCase, "test") {
return errors.New(`name cannot end with "test"`)
}
return checkGoReservedWord(name.LowerCamel)
}
// checkGoReservedWord checks if the name can't be used because it is a go reserved keyword.
func checkGoReservedWord(name string) error {
// Check keyword or literal
if token.Lookup(name).IsKeyword() {
return errors.Errorf("%s is a Go keyword", name)
}
// Check with builtin identifier
switch name {
case
"panic",
"recover",
"append",
"bool",
"byte",
"cap",
"close",
"complex",
"complex64",
"complex128",
"uint16",
"copy",
"false",
"float32",
"float64",
"imag",
"int",
"int8",
"int16",
"uint32",
"int32",
"int64",
"iota",
"len",
"make",
"new",
"nil",
"uint64",
"print",
"println",
"real",
"string",
"true",
"uint",
"uint8",
"uintptr":
return errors.Errorf("%s is a Go built-in identifier", name)
}
return nil
}
// checkForbiddenOracleFieldName returns true if the name is forbidden as an oracle field name.
//
// Deprecated: This function is no longer maintained.
func checkForbiddenOracleFieldName(name string) error {
mfName, err := multiformatname.NewName(name, multiformatname.NoNumber)
if err != nil {
return err
}
// Check with names already used from the scaffolded code
switch mfName.UpperCase {
case
"CLIENTID",
"ORACLESCRIPTID",
"SOURCECHANNEL",
"CALLDATA",
"ASKCOUNT",
"MINCOUNT",
"FEELIMIT",
"PREPAREGAS",
"EXECUTEGAS":
return errors.Errorf("%s is used by Ignite scaffolder", name)
}
return nil
}
// containsCustomTypes returns true if the list of fields contains at least one custom type.
func containsCustomTypes(fields []string) bool {
for _, field := range fields {
ft, ok := fieldType(field)
if !ok {
continue
}
if _, ok := datatype.IsSupportedType(datatype.Name(ft)); !ok {
return true
}
}
return false
}
// checks if a field is given. Returns type if true.
func fieldType(field string) (fieldType string, isCustom bool) {
fieldSplit := strings.Split(field, datatype.Separator)
if len(fieldSplit) <= 1 {
return "", false
}
return fieldSplit[1], true
}