This repository was archived by the owner on May 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.go
218 lines (174 loc) · 4.03 KB
/
basic.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//go:generate go run ../cmd/goquery/...
package main
import (
"context"
"database/sql"
"encoding/json"
"os"
"strings"
"time"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/sqliteshim"
"github.com/ffenix113/goquery"
)
type Book struct {
Title string
Author string
Released int
IsSelling bool
}
type a struct {
b struct {
c string
}
}
var globDB = func() *bun.DB {
dbSource := os.Getenv("DB_SOURCE")
sqldb, err := sql.Open(sqliteshim.ShimName, dbSource)
if err != nil {
panic(err)
}
return bun.NewDB(sqldb, sqlitedialect.New())
}()
func newQueryable[T any]() goquery.Queryable[T] {
return goquery.NewFactory[T](globDB).New()
}
func main() {
// basic()
// pointerAndMultiline()
// passingToAFunction()
// closure()
// badClosure()
// compareToTrue()
// unaryBoolean()
// someTimeFuncs()
// stringFuncs()
// stringPrefixSuffixFuncs()
// isNullAndInArray()
}
func basic() {
q := newQueryable[Book]()
var c a
q.Where(func(book Book) bool {
// Direct comparison with a constant works.
return book.Released == 2003 || book.Released == 2000
}).Where(func(bk Book) bool { // Chaining also works.
// Comparison against outside variables works
// as long as outside variable is provided as argument
// to `Where` method.
return bk.Title == c.b.c
}, c.b.c).Where(filter)
}
func filter(b Book) bool {
return b.Released > 0
}
func pointerAndMultiline() {
q := newQueryable[*Book]()
q.Where(func(book *Book) bool {
// Direct comparison with a constant works.
return book.Released == 2003 ||
book.Released == 2000
})
}
func passingToAFunction() {
q := newQueryable[*Book]()
addWhere(q)
}
func addWhere(q goquery.Queryable[*Book]) {
q.Where(func(b *Book) bool { return b.Title == "AnotherTitle" })
}
// Passing filter as a field will not work
// as it will not be possible to provide
// necessary caller information from runtime.
func filterFromArgument() {
// q := newQueryable[*Book]()
//
// func(f func(b *Book) bool) {
// q.Where(f)
// }(func(b *Book) bool {
// return b.Title == "AnotherTitle"
// })
}
func closure() {
q := newQueryable[*Book]()
a := 10
f := func(b *Book) bool {
return b.Released == a
}
// Only the name of the variable should match
q.Where(f, a)
}
func badClosure() {
q := newQueryable[*Book]()
a := 10
f := func(b *Book) bool {
return b.Released == a
}
{
// As only name of the variable should currently match
// it is possible to define wrong usages like this:
a := "string"
q.Where(f, a)
}
}
func compareToTrue() {
q := newQueryable[*Book]()
q.Where(func(book *Book) bool {
return book.IsSelling == true
})
}
func unaryBoolean() {
q := newQueryable[*Book]()
var b bool
q.Where(func(book *Book) bool {
return !!b || !book.IsSelling
}, b)
}
type str struct {
Time time.Time
}
func someTimeFuncs() {
q := newQueryable[*str]()
q.Where(func(s *str) bool {
return s.Time.After(time.Now()) || time.Now().Equal(time.Now()) || time.Now().Add(time.Hour).Equal(time.Now())
})
}
func stringFuncs() {
newQueryable[*Book]().Where(func(b *Book) bool {
return b.Title == strings.ToUpper(b.Title) || strings.Contains(b.Title, "contains")
})
}
func stringPrefixSuffixFuncs() {
newQueryable[*Book]().Where(func(b *Book) bool {
return strings.HasPrefix(b.Author, "prefix") || strings.HasSuffix(b.Title, "suffix")
})
}
func isNullAndInArray() {
args := []string{"1", "2"}
newQueryable[*Book]().Where(func(b *Book) bool {
return !goquery.IsNull(b.IsSelling) || goquery.In(b.Title, args)
}, args)
}
// Just some helpers
func runSingleQuery[T any](q goquery.Queryable[T]) (res T) {
if err := q.Query().Model(&res).
Scan(context.Background()); err != nil {
panic(err)
}
return res
}
func runAllQuery[T any](q goquery.Queryable[T]) (res []T) {
if err := q.Query().Model(&res).
Scan(context.Background()); err != nil {
panic(err)
}
return res
}
func encode(val any) {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "\t")
if err := enc.Encode(val); err != nil {
panic(err)
}
}