Skip to content

Commit 26cbc0c

Browse files
committed
WIP
1 parent c76e205 commit 26cbc0c

File tree

7 files changed

+162
-83
lines changed

7 files changed

+162
-83
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: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package parse // import "gopkg.in/reform.v1/parse"
77

88
import (
99
"fmt"
10+
"reflect"
1011
"strconv"
1112
"strings"
1213
)
@@ -17,7 +18,7 @@ type FieldInfo struct {
1718
Type string // field type as defined in source file, e.g. string; always present for primary key, may be absent otherwise
1819
Column string // SQL database column name from "reform:" struct field tag, e.g. name
1920

20-
// TODO Kind reflect.Kind // underlying type; set only by runtime parser
21+
Kind reflect.Kind // underlying type; set only by runtime parser
2122
}
2223

2324
// fieldInfoInSync returns true if FieldInfo fields that are set by both file and runtime parser are equal.
@@ -129,9 +130,10 @@ func (s *StructInfo) PKField() FieldInfo {
129130
return s.Fields[s.PKFieldIndex]
130131
}
131132

132-
// AssertUpToDate checks that given StructInfo matches given object.
133-
// It is used during program initialization to check that generated files are up-to-date.
134-
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{}) {
135137
msg := fmt.Sprintf(`reform:
136138
%s struct information is not up-to-date.
137139
Typically this means that %s type definition was changed, but 'reform' command / 'go generate' was not run.
@@ -144,9 +146,14 @@ func AssertUpToDate(si *StructInfo, obj interface{}) {
144146
if !structInfoInSync(si, si2) {
145147
panic(msg)
146148
}
149+
150+
// set runtime fields
151+
for i := range si.Fields {
152+
si.Fields[i].Kind = si2.Fields[i].Kind
153+
}
147154
}
148155

149-
// parseStructFieldTag is used by both file and runtime parsers
156+
// parseStructFieldTag is used by both file and runtime parsers.
150157
func parseStructFieldTag(tag string) (sqlName string, isPK bool) {
151158
parts := strings.Split(tag, ",")
152159
if len(parts) == 0 || len(parts) > 2 {
@@ -166,7 +173,7 @@ func parseStructFieldTag(tag string) (sqlName string, isPK bool) {
166173
return
167174
}
168175

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

0 commit comments

Comments
 (0)