@@ -7,6 +7,7 @@ package parse // import "gopkg.in/reform.v1/parse"
7
7
8
8
import (
9
9
"fmt"
10
+ "reflect"
10
11
"strconv"
11
12
"strings"
12
13
)
@@ -17,7 +18,7 @@ type FieldInfo struct {
17
18
Type string // field type as defined in source file, e.g. string; always present for primary key, may be absent otherwise
18
19
Column string // SQL database column name from "reform:" struct field tag, e.g. name
19
20
20
- // TODO Kind reflect.Kind // underlying type; set only by runtime parser
21
+ Kind reflect.Kind // underlying type; set only by runtime parser
21
22
}
22
23
23
24
// 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 {
129
130
return s .Fields [s .PKFieldIndex ]
130
131
}
131
132
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 {}) {
135
137
msg := fmt .Sprintf (`reform:
136
138
%s struct information is not up-to-date.
137
139
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{}) {
144
146
if ! structInfoInSync (si , si2 ) {
145
147
panic (msg )
146
148
}
149
+
150
+ // set runtime fields
151
+ for i := range si .Fields {
152
+ si .Fields [i ].Kind = si2 .Fields [i ].Kind
153
+ }
147
154
}
148
155
149
- // parseStructFieldTag is used by both file and runtime parsers
156
+ // parseStructFieldTag is used by both file and runtime parsers.
150
157
func parseStructFieldTag (tag string ) (sqlName string , isPK bool ) {
151
158
parts := strings .Split (tag , "," )
152
159
if len (parts ) == 0 || len (parts ) > 2 {
@@ -166,7 +173,7 @@ func parseStructFieldTag(tag string) (sqlName string, isPK bool) {
166
173
return
167
174
}
168
175
169
- // checkFields is used by both file and runtime parsers
176
+ // checkFields is used by both file and runtime parsers.
170
177
func checkFields (res * StructInfo ) error {
171
178
if len (res .Fields ) == 0 {
172
179
return fmt .Errorf (`reform: %s has no fields with "reform:" tag, it is not allowed` , res .Type )
0 commit comments