/
genesis.go
258 lines (233 loc) · 7.19 KB
/
genesis.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
package list
import (
"fmt"
"path/filepath"
"github.com/gobuffalo/genny/v2"
"github.com/spellshape/cli/spellshape/pkg/placeholder"
"github.com/spellshape/cli/spellshape/pkg/protoanalysis/protoutil"
"github.com/spellshape/cli/spellshape/templates/module"
"github.com/spellshape/cli/spellshape/templates/typed"
)
func genesisModify(replacer placeholder.Replacer, opts *typed.Options, g *genny.Generator) {
g.RunFn(genesisProtoModify(opts))
g.RunFn(genesisTypesModify(replacer, opts))
g.RunFn(genesisModuleModify(replacer, opts))
g.RunFn(genesisTestsModify(replacer, opts))
g.RunFn(genesisTypesTestsModify(replacer, opts))
}
// Modifies the genesis.proto file to add a new field.
//
// What it depends on:
// - Existence of a message with name "GenesisState". Adds the field there.
func genesisProtoModify(opts *typed.Options) genny.RunFn {
return func(r *genny.Runner) error {
path := opts.ProtoPath("genesis.proto")
f, err := r.Disk.Find(path)
if err != nil {
return err
}
protoFile, err := protoutil.ParseProtoFile(f)
if err != nil {
return err
}
// Add initial import for the new type
gogoImport := protoutil.NewImport(typed.GoGoProtoImport)
if err = protoutil.AddImports(protoFile, true, gogoImport, opts.ProtoTypeImport()); err != nil {
return fmt.Errorf("failed while adding imports in %s: %w", path, err)
}
// Get next available sequence number from GenesisState.
genesisState, err := protoutil.GetMessageByName(protoFile, typed.ProtoGenesisStateMessage)
if err != nil {
return fmt.Errorf("failed while looking up message '%s' in %s: %w", typed.ProtoGenesisStateMessage, path, err)
}
seqNumber := protoutil.NextUniqueID(genesisState)
typenameLower, typenameUpper := opts.TypeName.LowerCamel, opts.TypeName.UpperCamel
// Create option and List field.
gogoOption := protoutil.NewOption("gogoproto.nullable", "false", protoutil.Custom())
typeList := protoutil.NewField(
typenameLower+"List",
typenameUpper,
seqNumber,
protoutil.Repeated(),
protoutil.WithFieldOptions(gogoOption),
)
// Create count field.
countFIeld := protoutil.NewField(typenameLower+"Count", "uint64", seqNumber+1)
protoutil.Append(genesisState, typeList, countFIeld)
newFile := genny.NewFileS(path, protoutil.Print(protoFile))
return r.File(newFile)
}
}
func genesisTypesModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/genesis.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}
content := typed.PatchGenesisTypeImport(replacer, f.String())
templateTypesImport := `"fmt"`
content = replacer.ReplaceOnce(content, typed.PlaceholderGenesisTypesImport, templateTypesImport)
templateTypesDefault := `%[2]vList: []%[2]v{},
%[1]v`
replacementTypesDefault := fmt.Sprintf(
templateTypesDefault,
typed.PlaceholderGenesisTypesDefault,
opts.TypeName.UpperCamel,
)
content = replacer.Replace(content, typed.PlaceholderGenesisTypesDefault, replacementTypesDefault)
templateTypesValidate := `// Check for duplicated ID in %[2]v
%[2]vIdMap := make(map[uint64]bool)
%[2]vCount := gs.Get%[3]vCount()
for _, elem := range gs.%[3]vList {
if _, ok := %[2]vIdMap[elem.Id]; ok {
return fmt.Errorf("duplicated id for %[2]v")
}
if elem.Id >= %[2]vCount {
return fmt.Errorf("%[2]v id should be lower or equal than the last id")
}
%[2]vIdMap[elem.Id] = true
}
%[1]v`
replacementTypesValidate := fmt.Sprintf(
templateTypesValidate,
typed.PlaceholderGenesisTypesValidate,
opts.TypeName.LowerCamel,
opts.TypeName.UpperCamel,
)
content = replacer.Replace(content, typed.PlaceholderGenesisTypesValidate, replacementTypesValidate)
newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}
func genesisModuleModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "genesis.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}
templateModuleInit := `// Set all the %[2]v
for _, elem := range genState.%[3]vList {
k.Set%[3]v(ctx, elem)
}
// Set %[2]v count
k.Set%[3]vCount(ctx, genState.%[3]vCount)
%[1]v`
replacementModuleInit := fmt.Sprintf(
templateModuleInit,
typed.PlaceholderGenesisModuleInit,
opts.TypeName.LowerCamel,
opts.TypeName.UpperCamel,
)
content := replacer.Replace(f.String(), typed.PlaceholderGenesisModuleInit, replacementModuleInit)
templateModuleExport := `genesis.%[2]vList = k.GetAll%[2]v(ctx)
genesis.%[2]vCount = k.Get%[2]vCount(ctx)
%[1]v`
replacementModuleExport := fmt.Sprintf(
templateModuleExport,
typed.PlaceholderGenesisModuleExport,
opts.TypeName.UpperCamel,
)
content = replacer.Replace(content, typed.PlaceholderGenesisModuleExport, replacementModuleExport)
newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}
func genesisTestsModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "genesis_test.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}
templateState := `%[2]vList: []types.%[2]v{
{
Id: 0,
},
{
Id: 1,
},
},
%[2]vCount: 2,
%[1]v`
replacementValid := fmt.Sprintf(
templateState,
module.PlaceholderGenesisTestState,
opts.TypeName.UpperCamel,
)
content := replacer.Replace(f.String(), module.PlaceholderGenesisTestState, replacementValid)
templateAssert := `require.ElementsMatch(t, genesisState.%[2]vList, got.%[2]vList)
require.Equal(t, genesisState.%[2]vCount, got.%[2]vCount)
%[1]v`
replacementTests := fmt.Sprintf(
templateAssert,
module.PlaceholderGenesisTestAssert,
opts.TypeName.UpperCamel,
)
content = replacer.Replace(content, module.PlaceholderGenesisTestAssert, replacementTests)
newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}
func genesisTypesTestsModify(replacer placeholder.Replacer, opts *typed.Options) genny.RunFn {
return func(r *genny.Runner) error {
path := filepath.Join(opts.AppPath, "x", opts.ModuleName, "types/genesis_test.go")
f, err := r.Disk.Find(path)
if err != nil {
return err
}
templateValid := `%[2]vList: []types.%[2]v{
{
Id: 0,
},
{
Id: 1,
},
},
%[2]vCount: 2,
%[1]v`
replacementValid := fmt.Sprintf(
templateValid,
module.PlaceholderTypesGenesisValidField,
opts.TypeName.UpperCamel,
)
content := replacer.Replace(f.String(), module.PlaceholderTypesGenesisValidField, replacementValid)
templateTests := `{
desc: "duplicated %[2]v",
genState: &types.GenesisState{
%[3]vList: []types.%[3]v{
{
Id: 0,
},
{
Id: 0,
},
},
},
valid: false,
},
{
desc: "invalid %[2]v count",
genState: &types.GenesisState{
%[3]vList: []types.%[3]v{
{
Id: 1,
},
},
%[3]vCount: 0,
},
valid: false,
},
%[1]v`
replacementTests := fmt.Sprintf(
templateTests,
module.PlaceholderTypesGenesisTestcase,
opts.TypeName.LowerCamel,
opts.TypeName.UpperCamel,
)
content = replacer.Replace(content, module.PlaceholderTypesGenesisTestcase, replacementTests)
newFile := genny.NewFileS(path, content)
return r.File(newFile)
}
}