Repository navigation
Expand file tree
/
Copy pathfields.go
More file actions
279 lines (231 loc) · 7.29 KB
/
Copy pathfields.go
File metadata and controls
279 lines (231 loc) · 7.29 KB
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
package makroud
import (
"fmt"
"reflect"
"github.com/pkg/errors"
"github.com/ulule/makroud/reflectx"
"github.com/ulule/makroud/snaker"
)
// Field defines the column name, field and options from model.
//
// For example: If we have an User, we could have this primary key defined in User's schema.
//
// Field {
// ModelName: User,
// TableName: users,
// FieldName: AvatarID,
// ColumnName: avatar_id,
// ColumnPath: users.avatar_id,
// }
//
type Field struct {
modelName string
tableName string
fieldName string
fieldIndex []int
columnPath string
columnName string
foreignKey string
relationName string
isPrimaryKey bool
isForeignKey bool
isAssociation bool
isExcluded bool
hasRelation bool
hasDefault bool
hasULID bool
hasUUIDV1 bool
hasUUIDV4 bool
isCreatedKey bool
isUpdatedKey bool
isDeletedKey bool
rtype reflect.Type
associationType AssociationType
}
// ModelName define the model name of this field.
func (field Field) ModelName() string {
return field.modelName
}
// FieldName define the struct field name used for this field.
func (field Field) FieldName() string {
return field.fieldName
}
// FieldIndex define the struct field index used for this field.
func (field Field) FieldIndex() []int {
return field.fieldIndex
}
// TableName returns the model name's table name of this field.
func (field Field) TableName() string {
return field.tableName
}
// ColumnPath returns the field's full column path.
func (field Field) ColumnPath() string {
return field.columnPath
}
// ColumnName returns the field's column name.
func (field Field) ColumnName() string {
return field.columnName
}
// IsPrimaryKey returns if the field is a primary key.
func (field Field) IsPrimaryKey() bool {
return field.isPrimaryKey
}
// IsExcluded returns if the field excluded. (anonymous, private...)
func (field Field) IsExcluded() bool {
return field.isExcluded
}
// IsForeignKey returns if the field is a foreign key.
func (field Field) IsForeignKey() bool {
return field.isForeignKey
}
// ForeignKey returns the field foreign key's table name.
func (field Field) ForeignKey() string {
return field.foreignKey
}
// HasRelation returns if the field has a explicit relation.
func (field Field) HasRelation() bool {
return field.hasRelation
}
// RelationName returns the field relation name.
func (field Field) RelationName() string {
return field.relationName
}
// IsAssociation returns if the field is an association.
func (field Field) IsAssociation() bool {
return field.isAssociation
}
// IsAssociationType returns if the field has given association type.
func (field Field) IsAssociationType(kind AssociationType) bool {
return field.isAssociation && field.associationType == kind
}
// HasDefault returns if the field has a default value and should be in returning statement.
func (field Field) HasDefault() bool {
return field.hasDefault
}
// HasULID returns if the field has a ulid type for it's primary key.
func (field Field) HasULID() bool {
return field.hasULID
}
// HasUUIDV1 returns if the field has a uuid v1 type for it's primary key.
func (field Field) HasUUIDV1() bool {
return field.hasUUIDV1
}
// HasUUIDV4 returns if the field has a uuid v4 type for it's primary key.
func (field Field) HasUUIDV4() bool {
return field.hasUUIDV4
}
// IsCreatedKey returns if the field is a created key.
func (field Field) IsCreatedKey() bool {
return field.isCreatedKey
}
// IsUpdatedKey returns if the field is a updated key.
func (field Field) IsUpdatedKey() bool {
return field.isUpdatedKey
}
// IsDeletedKey returns if the field is a deleted key.
func (field Field) IsDeletedKey() bool {
return field.isDeletedKey
}
// Type returns the reflect's type of the field.
func (field Field) Type() reflect.Type {
return field.rtype
}
// String returns a human readable version of current instance.
func (field Field) String() string {
return DebugField(field)
}
// NewField creates a new field using given model and name.
func NewField(driver Driver, schema *Schema, model Model, name string, args ...ModelOpts) (*Field, error) {
if schema == nil {
return nil, errors.New("schema is required to generate a field instance")
}
opts := defaultModelOpts()
if len(args) > 0 {
opts = args[0]
}
field, ok := reflectx.GetFieldByName(model, name)
if !ok {
return nil, errors.Errorf("field '%s' not found in model", name)
}
tags := GetTags(field)
rtype := field.Type
if rtype.Kind() == reflect.Ptr {
rtype = rtype.Elem()
}
modelName := reflectx.GetIndirectTypeName(model)
tableName := model.TableName()
columnName := tags.GetByKey(TagName, TagKeyColumn)
if columnName == "" {
columnName = snaker.CamelToSnake(name)
}
columnPath := fmt.Sprintf("%s.%s", tableName, columnName)
isPrimaryKey := tags.HasKey(TagName, TagKeyPrimaryKey)
foreignKey := tags.GetByKey(TagName, TagKeyForeignKey)
isForeignKey := foreignKey != ""
isExcluded := tags.HasKey(TagName, TagKeyIgnored) || field.PkgPath != ""
hasDefault := tags.HasKey(TagName, TagKeyDefault)
hasULID := tags.GetByKey(TagName, TagKeyPrimaryKey) == TagKeyULID
hasUUIDV1 := tags.GetByKey(TagName, TagKeyPrimaryKey) == TagKeyUUIDV1
hasUUIDV4 := tags.GetByKey(TagName, TagKeyPrimaryKey) == TagKeyUUIDV4
isCreatedKey := columnName == opts.CreatedKey
isUpdatedKey := columnName == opts.UpdatedKey
isDeletedKey := columnName == opts.DeletedKey
hasDefault = hasDefault || isCreatedKey || isUpdatedKey
instance := &Field{
modelName: modelName,
tableName: tableName,
fieldName: field.Name,
fieldIndex: field.Index,
columnName: columnName,
columnPath: columnPath,
isPrimaryKey: isPrimaryKey,
isForeignKey: isForeignKey,
foreignKey: foreignKey,
isExcluded: isExcluded,
isCreatedKey: isCreatedKey,
isUpdatedKey: isUpdatedKey,
isDeletedKey: isDeletedKey,
hasDefault: hasDefault,
hasULID: hasULID,
hasUUIDV1: hasUUIDV1,
hasUUIDV4: hasUUIDV4,
rtype: rtype,
}
// Early return if the field type is not an association.
reference := toModel(rtype)
if reference == nil {
return instance, nil
}
return getFieldAssocitationType(driver, instance, rtype, reference, tags)
}
func getFieldAssocitationType(driver Driver, instance *Field, rtype reflect.Type,
reference Model, tags Tags) (*Field, error) {
if instance.isPrimaryKey {
return nil, errors.Errorf("field '%s' cannot be a primary key and a association", instance.fieldName)
}
associationType := AssociationTypeUndefined
if rtype.Kind() == reflect.Struct {
associationType = AssociationTypeOne
}
if rtype.Kind() == reflect.Slice {
associationType = AssociationTypeMany
}
if associationType == AssociationTypeUndefined {
return nil, errors.Errorf("unable to infer the association type for field '%s'", instance.fieldName)
}
relationName := tags.GetByKey(TagName, TagKeyRelation)
hasRelation := relationName != ""
instance.isAssociation = true
instance.associationType = associationType
instance.columnName = ""
instance.columnPath = ""
instance.isCreatedKey = false
instance.isForeignKey = false
instance.isUpdatedKey = false
instance.isDeletedKey = false
instance.hasDefault = false
instance.hasULID = false
instance.hasRelation = hasRelation
instance.relationName = relationName
return instance, nil
}