forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
67 lines (60 loc) · 1.53 KB
/
fields.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
package driver
import (
"reflect"
"gnd.la/util/structs"
)
type Reference struct {
Model Model
Field string
}
type Fields struct {
*structs.Struct
// Quoted mangled names of the fields, including the table
// name (e.g. "table"."field").
QuotedNames []string
// Fields which should be omitted when they are empty
OmitEmpty []bool
// Fields which should become null when they are empty
NullEmpty []bool
// The index of the primary (-1 if there's no pk)
PrimaryKey int
// True if the primary key is an integer type with auto_increment
AutoincrementPk bool
// The fields which make the composite primary key, if any
CompositePrimaryKey []int
// Model methods called by the ORM
Methods *Methods
// Other models referenced by this model. The key
// is the field name in this model.
References map[string]*Reference
// Default values. Key is field index, value is the default
// which might be a reflect.Func with no arguments and one
// return value or simply a value assignable to the field.
Defaults map[int]reflect.Value
}
func (f *Fields) IsSubfield(field, parent []int) bool {
if len(field) <= len(parent) {
return false
}
for ii, v := range parent {
if field[ii] != v {
return false
}
}
return true
}
func (f *Fields) HasDefault(idx int) bool {
_, ok := f.Defaults[idx]
return ok
}
func (f *Fields) DefaultValue(idx int) interface{} {
if f.HasDefault(idx) {
val := f.Defaults[idx]
if val.Kind() == reflect.Func {
res := val.Call(nil)
return res[0].Interface()
}
return val.Interface()
}
return nil
}