-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstruct.go
More file actions
168 lines (153 loc) · 4.74 KB
/
struct.go
File metadata and controls
168 lines (153 loc) · 4.74 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
package goqux
import (
"fmt"
"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"
// omitnil will skip the field if it is nil
omitNil = "omitnil"
)
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.IsValid() || value.IsZero() { // IsZero panic on valid values
continue
}
dbTag = cleanDbTag(dbTag, omitEmpty)
}
if strings.Contains(dbTag, omitNil) {
if value.IsNil() {
continue
}
dbTag = cleanDbTag(dbTag, omitNil)
}
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, omitEmpty, omitNil)
} else {
colName = strcase.ToSnake(f.Name)
}
cols = append(cols, table.Col(colName))
}
return cols
}
func cleanDbTag(tag string, tagsToClean ...string) string {
for _, tagToClean := range tagsToClean {
// Handle case where tag is just the partToClean
if tag == tagToClean {
return ""
}
// Handle case where tag starts with partToClean
if strings.HasPrefix(tag, fmt.Sprintf("%s,", tagToClean)) {
tag = strings.TrimPrefix(tag, fmt.Sprintf("%s,", tagToClean))
}
// Handle case where tag ends with partToClean
if strings.HasSuffix(tag, fmt.Sprintf(",%s", tagToClean)) {
tag = strings.TrimSuffix(tag, fmt.Sprintf(",%s", tagToClean))
}
// Handle case where tagToClean tag is in the middle
if strings.Contains(tag, fmt.Sprintf(",%s,", tagToClean)) {
tag = strings.ReplaceAll(tag, fmt.Sprintf(",%s,", tagToClean), ",")
}
}
// Clean up any remaining commas
tag = strings.Trim(tag, ",")
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
}