/
columns_for_struct.go
63 lines (52 loc) · 1.33 KB
/
columns_for_struct.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
package columns
import (
"reflect"
)
// ForStruct returns a Columns instance for
// the struct passed in.
func ForStruct(s interface{}, tableName, idField string) (columns Columns) {
return ForStructWithAlias(s, tableName, "", idField)
}
// ForStructWithAlias returns a Columns instance for the struct passed in.
// If the tableAlias is not empty, it will be used.
func ForStructWithAlias(s interface{}, tableName, tableAlias, idField string) (columns Columns) {
columns = NewColumnsWithAlias(tableName, tableAlias, idField)
defer func() {
if r := recover(); r != nil {
columns = NewColumnsWithAlias(tableName, tableAlias, idField)
columns.Add("*")
}
}()
st := reflect.TypeOf(s)
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
if st.Kind() == reflect.Slice {
st = st.Elem()
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
}
fieldCount := st.NumField()
for i := 0; i < fieldCount; i++ {
field := st.Field(i)
popTags := TagsFor(field)
tag := popTags.Find("db")
if !tag.Ignored() && !tag.Empty() {
col := tag.Value
// add writable or readable.
tag := popTags.Find("rw")
if !tag.Empty() {
col = col + "," + tag.Value
}
cs := columns.Add(col)
// add select clause.
tag = popTags.Find("select")
if !tag.Empty() {
c := cs[0]
c.SetSelectSQL(tag.Value)
}
}
}
return columns
}