-
Notifications
You must be signed in to change notification settings - Fork 532
/
type.go
290 lines (250 loc) · 7.66 KB
/
type.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package scaffolder
import (
"context"
"strings"
"github.com/gobuffalo/genny/v2"
"github.com/ignite/cli/v29/ignite/pkg/errors"
"github.com/ignite/cli/v29/ignite/pkg/multiformatname"
"github.com/ignite/cli/v29/ignite/pkg/placeholder"
"github.com/ignite/cli/v29/ignite/templates/field"
"github.com/ignite/cli/v29/ignite/templates/field/datatype"
modulecreate "github.com/ignite/cli/v29/ignite/templates/module/create"
"github.com/ignite/cli/v29/ignite/templates/typed"
"github.com/ignite/cli/v29/ignite/templates/typed/dry"
"github.com/ignite/cli/v29/ignite/templates/typed/list"
maptype "github.com/ignite/cli/v29/ignite/templates/typed/map"
"github.com/ignite/cli/v29/ignite/templates/typed/singleton"
)
// AddTypeOption configures options for AddType.
type AddTypeOption func(*addTypeOptions)
// AddTypeKind configures the type kind option for AddType.
type AddTypeKind func(*addTypeOptions)
type addTypeOptions struct {
moduleName string
fields []string
isList bool
isMap bool
isSingleton bool
index string
withoutMessage bool
withoutSimulation bool
signer string
}
// newAddTypeOptions returns a addTypeOptions with default options.
func newAddTypeOptions(moduleName string) addTypeOptions {
return addTypeOptions{
moduleName: moduleName,
signer: "creator",
}
}
// ListType makes the type stored in a list convention in the storage.
func ListType() AddTypeKind {
return func(o *addTypeOptions) {
o.isList = true
}
}
// MapType makes the type stored in a key-value convention in the storage with an index option.
func MapType(index string) AddTypeKind {
return func(o *addTypeOptions) {
o.isMap = true
o.index = index
}
}
// SingletonType makes the type stored in a fixed place as a single entry in the storage.
func SingletonType() AddTypeKind {
return func(o *addTypeOptions) {
o.isSingleton = true
}
}
// DryType only creates a type with a basic definition.
func DryType() AddTypeKind {
return func(*addTypeOptions) {}
}
// TypeWithModule module to scaffold type into.
func TypeWithModule(name string) AddTypeOption {
return func(o *addTypeOptions) {
o.moduleName = name
}
}
// TypeWithFields adds fields to the type to be scaffolded.
func TypeWithFields(fields ...string) AddTypeOption {
return func(o *addTypeOptions) {
o.fields = fields
}
}
// TypeWithoutMessage disables generating sdk compatible messages and tx related APIs.
func TypeWithoutMessage() AddTypeOption {
return func(o *addTypeOptions) {
o.withoutMessage = true
}
}
// TypeWithoutSimulation disables generating messages simulation.
func TypeWithoutSimulation() AddTypeOption {
return func(o *addTypeOptions) {
o.withoutSimulation = true
}
}
// TypeWithSigner provides a custom signer name for the message.
func TypeWithSigner(signer string) AddTypeOption {
return func(o *addTypeOptions) {
o.signer = signer
}
}
// AddType adds a new type to a scaffolded app.
// if none of the list, map or singleton given, a dry type without anything extra (like a storage layer, models, CLI etc.)
// will be scaffolded.
// if no module is given, the type will be scaffolded inside the app's default module.
func (s Scaffolder) AddType(
ctx context.Context,
typeName string,
kind AddTypeKind,
options ...AddTypeOption,
) error {
// apply options.
o := newAddTypeOptions(s.modpath.Package)
for _, apply := range append(options, AddTypeOption(kind)) {
apply(&o)
}
mfName, err := multiformatname.NewName(o.moduleName, multiformatname.NoNumber)
if err != nil {
return err
}
moduleName := mfName.LowerCase
name, err := multiformatname.NewName(typeName)
if err != nil {
return err
}
if err := checkComponentValidity(s.appPath, moduleName, name, o.withoutMessage); err != nil {
return err
}
// Check and parse provided fields
if err := checkCustomTypes(ctx, s.appPath, s.modpath.Package, s.protoDir, moduleName, o.fields); err != nil {
return err
}
tFields, err := parseTypeFields(o)
if err != nil {
return err
}
mfSigner, err := multiformatname.NewName(o.signer)
if err != nil {
return err
}
isIBC, err := isIBCModule(s.appPath, moduleName)
if err != nil {
return err
}
var (
g *genny.Generator
opts = &typed.Options{
AppName: s.modpath.Package,
AppPath: s.appPath,
ProtoDir: s.protoDir,
ProtoVer: "v1", // TODO(@julienrbrt): possibly in the future add flag to specify custom proto version.
ModulePath: s.modpath.RawPath,
ModuleName: moduleName,
TypeName: name,
Fields: tFields,
NoMessage: o.withoutMessage,
NoSimulation: o.withoutSimulation,
MsgSigner: mfSigner,
IsIBC: isIBC,
}
gens []*genny.Generator
)
// Check and support MsgServer convention
gens, err = supportMsgServer(
gens,
s.runner.Tracer(),
&modulecreate.MsgServerOptions{
ModuleName: opts.ModuleName,
ModulePath: opts.ModulePath,
AppName: opts.AppName,
AppPath: opts.AppPath,
ProtoDir: opts.ProtoDir,
ProtoVer: opts.ProtoVer,
},
)
if err != nil {
return err
}
// create the type generator depending on the model
switch {
case o.isList:
g, err = list.NewGenerator(s.Tracer(), opts)
case o.isMap:
g, err = mapGenerator(s.Tracer(), opts, o.index)
case o.isSingleton:
g, err = singleton.NewGenerator(s.Tracer(), opts)
default:
g, err = dry.NewGenerator(opts)
}
if err != nil {
return err
}
// run the generation
return s.Run(append(gens, g)...)
}
// checkForbiddenTypeIndex returns true if the name is forbidden as a index name.
func checkForbiddenTypeIndex(index string) error {
indexSplit := strings.Split(index, datatype.Separator)
if len(indexSplit) > 1 {
index = indexSplit[0]
indexType := datatype.Name(indexSplit[1])
if f, ok := datatype.IsSupportedType(indexType); !ok || f.NonIndex {
return errors.Errorf("invalid index type %s", indexType)
}
}
return checkForbiddenTypeField(index)
}
// checkForbiddenTypeField returns true if the name is forbidden as a field name.
func checkForbiddenTypeField(field string) error {
mfName, err := multiformatname.NewName(field)
if err != nil {
return err
}
switch mfName.LowerCase {
case
"id",
"params",
"appendedvalue",
datatype.TypeCustom:
return errors.Errorf("%s is used by type scaffolder", field)
}
return checkGoReservedWord(field)
}
// parseTypeFields validates the fields and returns an error if the validation fails.
func parseTypeFields(opts addTypeOptions) (field.Fields, error) {
signer := ""
if opts.isList || opts.isMap || opts.isSingleton {
if !opts.withoutMessage {
signer = opts.signer
}
return field.ParseFields(opts.fields, checkForbiddenTypeField, signer)
}
// For simple types, only check if it's a reserved keyword and don't pass a signer.
return field.ParseFields(opts.fields, checkGoReservedWord, signer)
}
// mapGenerator returns the template generator for a map.
func mapGenerator(replacer placeholder.Replacer, opts *typed.Options, index string) (*genny.Generator, error) {
// Parse indexes with the associated type
if strings.Contains(index, ",") {
return nil, errors.Errorf("multi-index map isn't supported")
}
parsedIndexes, err := field.ParseFields([]string{index}, checkForbiddenTypeIndex)
if err != nil {
return nil, err
}
if len(parsedIndexes) == 0 {
return nil, errors.Errorf("no index found, a valid map index must be provided")
}
// Indexes and type fields must be disjoint
exists := make(map[string]struct{})
for _, name := range opts.Fields {
exists[name.Name.LowerCamel] = struct{}{}
}
if _, ok := exists[parsedIndexes[0].Name.LowerCamel]; ok {
return nil, errors.Errorf("%s cannot simultaneously be an index and a field", parsedIndexes[0].Name.Original)
}
opts.Index = parsedIndexes[0]
return maptype.NewGenerator(replacer, opts)
}