Skip to content

Commit d8f13dc

Browse files
committed
Set kind during initialisation
1 parent ba7c4de commit d8f13dc

File tree

6 files changed

+95
-37
lines changed

6 files changed

+95
-37
lines changed

internal/test/models/extra_reform.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/models/good_reform.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parse/base.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ func (s *StructInfo) PKField() FieldInfo {
130130
return s.Fields[s.PKFieldIndex]
131131
}
132132

133-
// AssertUpToDate checks that given StructInfo matches given object.
134-
// It is used during program initialization to check that generated files are up-to-date.
135-
func AssertUpToDate(si *StructInfo, obj interface{}) {
133+
// Init checks that given generated StructInfo matches object's runtime information
134+
// (those checking that generated file is up-to-date),
135+
// then adds information from the runtime parser.
136+
func Init(si *StructInfo, obj interface{}) {
136137
msg := fmt.Sprintf(`reform:
137138
%s struct information is not up-to-date.
138139
Typically this means that %s type definition was changed, but 'reform' command / 'go generate' was not run.
@@ -145,9 +146,14 @@ func AssertUpToDate(si *StructInfo, obj interface{}) {
145146
if !structInfoInSync(si, si2) {
146147
panic(msg)
147148
}
149+
150+
// set runtime fields
151+
for i := range si.Fields {
152+
si.Fields[i].Kind = si2.Fields[i].Kind
153+
}
148154
}
149155

150-
// parseStructFieldTag is used by both file and runtime parsers
156+
// parseStructFieldTag is used by both file and runtime parsers.
151157
func parseStructFieldTag(tag string) (sqlName string, isPK bool) {
152158
parts := strings.Split(tag, ",")
153159
if len(parts) == 0 || len(parts) > 2 {
@@ -167,7 +173,7 @@ func parseStructFieldTag(tag string) (sqlName string, isPK bool) {
167173
return
168174
}
169175

170-
// checkFields is used by both file and runtime parsers
176+
// checkFields is used by both file and runtime parsers.
171177
func checkFields(res *StructInfo) error {
172178
if len(res.Fields) == 0 {
173179
return fmt.Errorf(`reform: %s has no fields with "reform:" tag, it is not allowed`, res.Type)

parse/parse_test.go

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package parse_test
22

33
import (
44
"errors"
5+
"fmt"
56
"path/filepath"
67
"reflect"
78
"strings"
@@ -132,6 +133,8 @@ func cloneFile(s StructInfo) StructInfo {
132133
}
133134

134135
func TestFileGood(t *testing.T) {
136+
t.Parallel()
137+
135138
s, err := File(filepath.FromSlash("../internal/test/models/good.go"))
136139
assert.NoError(t, err)
137140
require.Len(t, s, 6)
@@ -144,6 +147,8 @@ func TestFileGood(t *testing.T) {
144147
}
145148

146149
func TestFileExtra(t *testing.T) {
150+
t.Parallel()
151+
147152
s, err := File(filepath.FromSlash("../internal/test/models/extra.go"))
148153
assert.NoError(t, err)
149154
require.Len(t, s, 2)
@@ -152,6 +157,8 @@ func TestFileExtra(t *testing.T) {
152157
}
153158

154159
func TestFileBogus(t *testing.T) {
160+
t.Parallel()
161+
155162
dir := filepath.FromSlash("../internal/test/models/bogus/")
156163
for file, msg := range map[string]error{
157164
"bogus1.go": errors.New(`reform: Bogus1 has anonymous field BogusType with "reform:" tag, it is not allowed`),
@@ -169,13 +176,20 @@ func TestFileBogus(t *testing.T) {
169176

170177
"bogus_ignore.go": nil,
171178
} {
172-
s, err := File(filepath.Join(dir, file))
173-
assert.Nil(t, s)
174-
assert.Equal(t, msg, err)
179+
file, msg := file, msg
180+
t.Run(file, func(t *testing.T) {
181+
t.Parallel()
182+
183+
s, err := File(filepath.Join(dir, file))
184+
assert.Nil(t, s)
185+
assert.Equal(t, msg, err)
186+
})
175187
}
176188
}
177189

178190
func TestObjectGood(t *testing.T) {
191+
t.Parallel()
192+
179193
s, err := Object(new(models.Person), "", "people")
180194
assert.NoError(t, err)
181195
assert.Equal(t, &person, s)
@@ -202,6 +216,8 @@ func TestObjectGood(t *testing.T) {
202216
}
203217

204218
func TestObjectExtra(t *testing.T) {
219+
t.Parallel()
220+
205221
s, err := Object(new(models.Extra), "", "extra")
206222
assert.NoError(t, err)
207223
assert.Equal(t, &extra, s)
@@ -212,6 +228,8 @@ func TestObjectExtra(t *testing.T) {
212228
}
213229

214230
func TestObjectBogus(t *testing.T) {
231+
t.Parallel()
232+
215233
for obj, msg := range map[interface{}]error{
216234
new(bogus.Bogus1): errors.New(`reform: Bogus1 has anonymous field BogusType with "reform:" tag, it is not allowed`),
217235
new(bogus.Bogus2): errors.New(`reform: Bogus2 has anonymous field bogusType with "reform:" tag, it is not allowed`),
@@ -228,14 +246,23 @@ func TestObjectBogus(t *testing.T) {
228246

229247
// new(bogus.BogusIgnore): do not test,
230248
} {
231-
s, err := Object(obj, "", "bogus")
232-
assert.Nil(t, s)
233-
assert.Equal(t, msg, err)
249+
obj, msg := obj, msg
250+
t.Run(fmt.Sprint(obj), func(t *testing.T) {
251+
t.Parallel()
252+
253+
s, err := Object(obj, "", "bogus")
254+
assert.Nil(t, s)
255+
assert.Equal(t, msg, err)
256+
})
234257
}
235258
}
236259

237260
func TestHelpersGood(t *testing.T) {
261+
t.Parallel()
262+
238263
t.Run("person", func(t *testing.T) {
264+
t.Parallel()
265+
239266
assert.Equal(t, strings.TrimSpace(`
240267
parse.StructInfo{
241268
Type: "Person",
@@ -265,6 +292,8 @@ parse.StructInfo{
265292
})
266293

267294
t.Run("project", func(t *testing.T) {
295+
t.Parallel()
296+
268297
assert.Equal(t, strings.TrimSpace(`
269298
parse.StructInfo{
270299
Type: "Project",
@@ -290,6 +319,8 @@ parse.StructInfo{
290319
})
291320

292321
t.Run("personProject", func(t *testing.T) {
322+
t.Parallel()
323+
293324
assert.Equal(t, strings.TrimSpace(`
294325
parse.StructInfo{
295326
Type: "PersonProject",
@@ -310,6 +341,8 @@ parse.StructInfo{
310341
})
311342

312343
t.Run("constraints", func(t *testing.T) {
344+
t.Parallel()
345+
313346
assert.Equal(t, strings.TrimSpace(`
314347
parse.StructInfo{
315348
Type: "Constraints",
@@ -331,6 +364,8 @@ parse.StructInfo{
331364
})
332365

333366
t.Run("idOnly", func(t *testing.T) {
367+
t.Parallel()
368+
334369
assert.Equal(t, strings.TrimSpace(`
335370
parse.StructInfo{
336371
Type: "IDOnly",
@@ -350,6 +385,8 @@ parse.StructInfo{
350385
})
351386

352387
t.Run("legacyPerson", func(t *testing.T) {
388+
t.Parallel()
389+
353390
assert.Equal(t, strings.TrimSpace(`
354391
parse.StructInfo{
355392
Type: "LegacyPerson",
@@ -373,7 +410,11 @@ parse.StructInfo{
373410
}
374411

375412
func TestHelpersExtra(t *testing.T) {
413+
t.Parallel()
414+
376415
t.Run("extra", func(t *testing.T) {
416+
t.Parallel()
417+
377418
assert.Equal(t, strings.TrimSpace(`
378419
parse.StructInfo{
379420
Type: "Extra",
@@ -420,6 +461,8 @@ parse.StructInfo{
420461
})
421462

422463
t.Run("notExported", func(t *testing.T) {
464+
t.Parallel()
465+
423466
assert.Equal(t, strings.TrimSpace(`
424467
parse.StructInfo{
425468
Type: "notExported",
@@ -439,21 +482,30 @@ parse.StructInfo{
439482
})
440483
}
441484

442-
func TestAssertUpToDate(t *testing.T) {
443-
AssertUpToDate(&person, new(models.Person))
485+
func TestInit(t *testing.T) {
486+
t.Parallel()
487+
488+
t.Run("NotPanics", func(t *testing.T) {
489+
t.Parallel()
444490

445-
func() {
446-
defer func() {
447-
expected := `reform:
491+
p := cloneFile(person)
492+
assert.Empty(t, p.PKField().Kind)
493+
Init(&p, new(models.Person))
494+
assert.NotEmpty(t, p.PKField().Kind)
495+
})
496+
497+
t.Run("Panics", func(t *testing.T) {
498+
t.Parallel()
499+
500+
expected := `reform:
448501
Person struct information is not up-to-date.
449502
Typically this means that Person type definition was changed, but 'reform' command / 'go generate' was not run.
450503
451504
`
452-
assert.Equal(t, expected, recover())
453-
}()
454-
455-
p := person
456-
p.PKFieldIndex = 1
457-
AssertUpToDate(&p, new(models.Person))
458-
}()
505+
assert.PanicsWithValue(t, expected, func() {
506+
p := cloneFile(person)
507+
p.PKFieldIndex = 1
508+
Init(&p, new(models.Person))
509+
})
510+
})
459511
}

reform-db/models_reform.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

reform/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ var (
153153
initTemplate = template.Must(template.New("init").Parse(`
154154
func init() {
155155
{{- range $i, $sd := . }}
156-
parse.AssertUpToDate(&{{ $sd.TableVar }}.s, new({{ $sd.Type }}))
156+
parse.Init(&{{ $sd.TableVar }}.s, new({{ $sd.Type }}))
157157
{{- end }}
158158
}
159159
`))

0 commit comments

Comments
 (0)