-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.go
112 lines (98 loc) · 2.36 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
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
package pg
import (
"database/sql"
"reflect"
)
type fields struct {
Result
}
func (f fields) extract() []reflect.StructField {
mustBeStructPtr(f.Result)
entityType := reflect.TypeOf(f.Result).Elem()
list := make([]reflect.StructField, 0)
for i := 0; i < entityType.NumField(); i++ {
field := entityType.Field(i)
t := newTag(field)
if !t.omitfield {
list = append(list, field)
}
}
return list
}
func (f fields) all() []string {
list := make([]string, 0)
for _, field := range f.extract() {
t := newTag(field)
list = append(list, t.fieldname)
}
return list
}
func (f fields) autofilled(autofilled bool) []string {
list := make([]string, 0)
for _, field := range f.extract() {
t := newTag(field)
if t.autofilled == autofilled {
list = append(list, t.fieldname)
}
}
return list
}
func (f fields) autofilledValues(autofilled bool) []interface{} {
entityValue := reflect.ValueOf(f.Result).Elem()
values := make([]interface{}, 0)
for _, field := range f.extract() {
t := newTag(field)
if t.autofilled == autofilled {
value := entityValue.FieldByName(field.Name).Addr().Interface()
values = append(values, value)
}
}
return values
}
func (f fields) pk(pk bool) []string {
list := make([]string, 0)
for _, field := range f.extract() {
t := newTag(field)
if t.pk == pk {
list = append(list, t.fieldname)
}
}
return list
}
func (f fields) pkValues(pk bool) []interface{} {
entityValue := reflect.ValueOf(f.Result).Elem()
values := make([]interface{}, 0)
for _, field := range f.extract() {
t := newTag(field)
if t.pk == pk {
value := entityValue.FieldByName(field.Name).Addr().Interface()
values = append(values, value)
}
}
return values
}
func (f fields) updatable() []string {
return f.autofilled(false)
}
func (f fields) notUpdatable() []string {
return f.autofilled(true)
}
func (f fields) values() []interface{} {
entityValue := reflect.ValueOf(f.Result).Elem()
list := make([]interface{}, 0)
for _, field := range f.extract() {
value := entityValue.FieldByName(field.Name).Addr().Interface()
list = append(list, value)
}
return list
}
func (f fields) updatableValues() []interface{} {
return f.autofilledValues(false)
}
func (f fields) notUpdatableValues() []interface{} {
return f.autofilledValues(true)
}
func (f fields) Scan(rows *sql.Rows) error {
values := f.values()
return rows.Scan(values...)
}