-
Notifications
You must be signed in to change notification settings - Fork 7
/
struct.go
138 lines (127 loc) · 3.79 KB
/
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
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
package goqux
import (
"reflect"
"strings"
"time"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
"github.com/doug-martin/goqu/v9/exp"
"github.com/iancoleman/strcase"
)
const (
// TagName goqux tag field query building
tagName = "goqux"
// TagName of db to change column field
tagNameDb = "db"
// skip is field that allows skipping the column
skipSelect = "skip_select"
skipUpdate = "skip_update"
skipInsert = "skip_insert"
skipReturningDelete = "skip_delete"
// if the field is of type time.Time it will inject time.Now
defaultNow = "now"
// Same as default now but will inject time.Now().UTC()
defaultNowUtc = "now_utc"
// omitempty will skip the field if it is zero value
omitEmpty = ",omitempty"
)
func convertMapToSQLValuer(m map[string]any) map[string]SQLValuer {
values := make(map[string]SQLValuer)
for k, v := range m {
values[k] = SQLValuer{v}
}
return values
}
func encodeValues(v any, skipType string, skipZeroValues bool) map[string]SQLValuer {
t := reflect.ValueOf(v)
// if we received a map we will just convert it to a map of SQLValuer
if t.Kind() == reflect.Map {
return convertMapToSQLValuer(v.(map[string]any))
}
fields := reflect.VisibleFields(t.Type())
values := make(map[string]SQLValuer)
for _, f := range fields {
if !f.IsExported() || strings.Contains(f.Tag.Get(tagName), skipType) {
continue
}
value := t.FieldByName(f.Name)
// We want to support the case when there is no value in one of the fields
if skipZeroValues && value.IsZero() {
continue
}
columnName := strcase.ToSnake(f.Name)
if dbTag := f.Tag.Get(tagNameDb); dbTag != "" {
if strings.Contains(dbTag, omitEmpty) {
if value.IsZero() {
continue
}
dbTag = cleanDbTag(dbTag)
}
columnName = dbTag
}
switch {
case strings.Contains(f.Tag.Get(tagName), defaultNowUtc):
values[columnName] = SQLValuer{time.Now().UTC()}
case strings.Contains(f.Tag.Get(tagName), defaultNow):
values[columnName] = SQLValuer{time.Now()}
default:
values[columnName] = SQLValuer{value.Interface()}
}
}
return values
}
func getColumnsFromStruct(table exp.IdentifierExpression, s any, skipType string) []exp.IdentifierExpression {
t := reflect.TypeOf(s)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
fields := reflect.VisibleFields(t)
var cols = make([]exp.IdentifierExpression, 0)
for _, f := range fields {
if !f.IsExported() || strings.Contains(f.Tag.Get(tagName), skipType) {
continue
}
var colName string
if dbTag := f.Tag.Get(tagNameDb); dbTag != "" {
colName = cleanDbTag(dbTag)
} else {
colName = strcase.ToSnake(f.Name)
}
cols = append(cols, table.Col(colName))
}
return cols
}
func cleanDbTag(tag string) string {
if strings.Contains(tag, omitEmpty) {
tag = strings.ReplaceAll(tag, omitEmpty, "")
}
return tag
}
func getSelectionFieldsFromSelectionStruct(s interface{}) []exp.AliasedExpression {
cols := make([]exp.AliasedExpression, 0)
t := reflect.TypeOf(s)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
tableFields := reflect.VisibleFields(t)
for _, tf := range tableFields {
if !tf.IsExported() {
continue
}
if tf.Type.Kind() != reflect.Struct && !(tf.Type.Kind() == reflect.Ptr && tf.Type.Elem().Kind() == reflect.Struct) {
continue
}
tableName := strcase.ToSnake(tf.Name)
if dbTag := tf.Tag.Get(tagNameDb); dbTag != "" {
tableName = cleanDbTag(dbTag)
}
subTableColumns := getColumnsFromStruct(goqu.T(tableName), reflect.New(tf.Type).Elem().Interface(), skipSelect)
for _, c := range subTableColumns {
// SELECT "table"."column" AS "table.column" will make sure dbscan scans all the columns correctly
cc := c.GetCol()
cName := tableName + "." + cc.(string)
cols = append(cols, goqu.T(tableName).Col(cc).As(goqu.C(cName)))
}
}
return cols
}