-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.go
More file actions
184 lines (165 loc) · 4.53 KB
/
encoder.go
File metadata and controls
184 lines (165 loc) · 4.53 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package bigqueryutil
import (
"reflect"
"strconv"
"strings"
"time"
"cloud.google.com/go/bigquery"
"github.com/arquivei/foundationkit/errors"
)
// EncodeBigqueryWhereClause transforms a struct into a bigquery's query and parameters list.
//
// struct {
// Namespace string `bq:",omitempty"`
// CreatedAt *TimeRange `bq:",omitempty"`
// EmissionDate *TimeRange `bq:",omitempty"`
// EmissionDateWithoutTime *TimeRange `bq:",omitempty" format:"2006-01-02"`
// Owners []string `bq:"Owner,omitempty"`
// OwnerRoles []string `bq:",unnest,omitempty"`
// IsTaker *bool `bq:",omitempty"`
// }
func EncodeBigqueryWhereClause(filter interface{}) (string, []bigquery.QueryParameter, error) {
rv := reflect.ValueOf(filter)
if rv.Kind() != reflect.Struct {
return "", nil, errors.New("filter must be a struct: " + rv.Kind().String())
}
sb := strings.Builder{} // main string builder
fsb := strings.Builder{} // string builder for a single field
nFields := rv.Type().NumField()
// This is an approximation. In reality, TimeRange uses two slots and booleans use none.
params := make([]bigquery.QueryParameter, 0, nFields)
for i := 0; i < nFields; i++ {
// Get value, type and tags
fvalue := rv.Field(i)
ftype := rv.Type().Field(i)
fparam := parseFieldParameters(ftype.Tag.Get("bq"))
// Skip zero values if omitempty is enabled for the field
if fparam.omitEmpty && fvalue.IsZero() {
continue
}
// Rename field if specified a new name inside the tag
name := ftype.Name
if fparam.name != "" {
name = fparam.name
}
// Fix kind and value if field is a pointer
fkind := ftype.Type.Kind()
if fkind == reflect.Ptr {
fvalue = fvalue.Elem()
fkind = fvalue.Kind()
}
fsb.Reset()
// the filed will be temporary stored
if fparam.unnest {
fsb.WriteString("EXISTS (SELECT * FROM UNNEST(")
fsb.WriteString(name)
fsb.WriteString(") AS x WHERE x")
} else {
fsb.WriteString(name)
}
switch fkind {
case reflect.String:
fsb.WriteString(" = @")
fsb.WriteString(name)
params = AppendParam(params, name, fvalue.Interface())
case reflect.Slice:
if rv.Field(i).Len() == 0 {
continue
}
fsb.WriteString(" IN (")
for j := 0; j < fvalue.Len(); j++ {
elemName := name + strconv.Itoa(j)
if j > 0 {
fsb.WriteString(",")
}
fsb.WriteString("@")
fsb.WriteString(elemName)
params = AppendParam(params, elemName, fvalue.Index(j).Interface())
}
fsb.WriteString(")")
case reflect.Bool:
if !fvalue.Bool() {
fsb.Reset()
fsb.WriteString("NOT ")
fsb.WriteString(name)
}
case reflect.Struct:
switch v := fvalue.Interface().(type) {
case TimeRange:
fsb.WriteString(" BETWEEN @")
fsb.WriteString(name)
fsb.WriteString("From AND @")
fsb.WriteString(name)
fsb.WriteString("To")
format := ftype.Tag.Get("format")
if format == "" {
format = time.RFC3339
}
params = AppendParam(params, name+"From", v.From.Format(format))
params = AppendParam(params, name+"To", v.To.Format(format))
default:
return "", nil, errors.New(name + " struct is not supported")
}
default:
return "", nil, errors.New(name + " is of unknown type: " + fkind.String())
}
if fparam.unnest {
fsb.WriteString(")")
}
// Apped to main query
if sb.Len() > 0 {
sb.WriteString(" AND ")
}
sb.WriteString(fsb.String())
}
return sb.String(), params, nil
}
func parseFieldParameters(tag string) fieldParameters {
var params fieldParameters
if tag == "" {
return params
}
var part string
// Parse field name
i := strings.IndexByte(tag, ',')
if i < 0 {
part, tag = tag, ""
} else {
part, tag = tag[:i], tag[i+1:]
}
params.name = part
// Parse field parameters
for len(tag) > 0 {
// This loop uses IndexByte and explicit slicing
// instead of strings.Split(str, ",") to reduce allocations.
i = strings.IndexByte(tag, ',')
if i < 0 {
part, tag = tag, ""
} else {
part, tag = tag[:i], tag[i+1:]
}
switch part {
case "unnest":
params.unnest = true
case "omitempty":
params.omitEmpty = true
}
}
return params
}
type fieldParameters struct {
unnest bool
omitEmpty bool
name string
}
// AppendParam append parameters for given @params.
func AppendParam(
params []bigquery.QueryParameter,
name string,
value interface{},
) []bigquery.QueryParameter {
return append(params, bigquery.QueryParameter{
Name: name,
Value: value,
})
}