-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathschema_filtering.go
640 lines (562 loc) · 17.5 KB
/
schema_filtering.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Copyright (c) Liam Stanley <liam@liam.sh>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package entrest
import (
"fmt"
"maps"
"slices"
"strings"
"entgo.io/ent/entc/gen"
"entgo.io/ent/schema/field"
"github.com/ogen-go/ogen"
)
// Predicate represents a filtering predicate provided by ent.
type Predicate int
// Mirrored from entgo.io/ent/entc/gen with special groupings and support for bitwise operations.
const (
// FilterEdge is a special filter which is applied to the edge itself, indicating
// that all of the edges fields should also be included in filtering options.
FilterEdge Predicate = 1 << iota
FilterEQ // =
FilterNEQ // <>
FilterGT // >
FilterGTE // >=
FilterLT // <
FilterLTE // <=
FilterIsNil // IS NULL / has
FilterIn // within
FilterNotIn // without
FilterEqualFold // equals case-insensitive
FilterContains // containing
FilterContainsFold // containing case-insensitive
FilterHasPrefix // startingWith
FilterHasSuffix // endingWith
// FilterNotNil // Since you can use IsNil=false (we have three states for passed parameters).
// FilterGroupEqualExact includes: eq, neq, equal fold, is nil.
FilterGroupEqualExact = FilterEQ | FilterNEQ | FilterEqualFold | FilterGroupNil
// FilterGroupEqual includes: eq, neq, equal fold, contains, contains case, prefix, suffix, nil.
FilterGroupEqual = FilterGroupEqualExact | FilterGroupContains | FilterHasPrefix | FilterHasSuffix
// FilterGroupContains includes: contains, contains case, is nil.
FilterGroupContains = FilterContains | FilterContainsFold | FilterGroupNil
// FilterGroupNil includes: is nil.
FilterGroupNil = FilterIsNil
// FilterGroupLength includes: gt, lt (often gte/lte isn't really needed).
FilterGroupLength = FilterGT | FilterLT
// FilterGroupArray includes: in, not in.
FilterGroupArray = FilterIn | FilterNotIn
)
// filterMap maps a predicate to the entgo.io/ent/entc/gen.Op (to get string representation).
var filterMap = map[Predicate]gen.Op{
FilterEQ: gen.EQ,
FilterNEQ: gen.NEQ,
FilterGT: gen.GT,
FilterGTE: gen.GTE,
FilterLT: gen.LT,
FilterLTE: gen.LTE,
FilterIsNil: gen.IsNil,
FilterIn: gen.In,
FilterNotIn: gen.NotIn,
FilterEqualFold: gen.EqualFold,
FilterContains: gen.Contains,
FilterContainsFold: gen.ContainsFold,
FilterHasPrefix: gen.HasPrefix,
FilterHasSuffix: gen.HasSuffix,
}
// String returns the gen.Op string representation of a predicate.
func (p Predicate) String() string {
if _, ok := filterMap[p]; ok {
return filterMap[p].Name()
}
panic("predicate.String() called with grouped predicate, use Explode() first")
}
// Has returns if the predicate has the provided predicate.
func (p Predicate) Has(v Predicate) bool {
return p&v != 0
}
// Add adds the provided predicate to the current predicate.
func (p Predicate) Add(v Predicate) Predicate {
p |= v
return p
}
// Remove removes the provided predicate from the current predicate.
func (p Predicate) Remove(v Predicate) Predicate {
p &^= v
return p
}
// Explode returns all individual predicates as []gen.Op.
func (p Predicate) Explode() (ops []gen.Op) {
for pred, op := range filterMap {
if p.Has(pred) {
ops = append(ops, op)
}
}
return ops
}
// predicateFormat returns the query string representation of a filter predicate.
func predicateFormat(op gen.Op) string {
switch op {
case gen.Contains:
return "has"
case gen.ContainsFold:
return "ihas"
case gen.EqualFold:
return "ieq"
case gen.HasPrefix:
return "prefix"
case gen.HasSuffix:
return "suffix"
case gen.IsNil:
return "null"
default:
return CamelCase(SnakeCase(op.Name()))
}
}
// predicateDescription returns the description of a filter predicate.
func predicateDescription(f *gen.Field, op gen.Op) string {
switch op {
case gen.EQ:
return fmt.Sprintf("Filters field %q to be equal to the provided value.", f.Name)
case gen.NEQ:
return fmt.Sprintf("Filters field %q to be not equal to the provided value.", f.Name)
case gen.GT:
if f.IsString() {
return fmt.Sprintf("Filters field %q to be longer than the provided value.", f.Name)
}
return fmt.Sprintf("Filters field %q to be greater than the provided value.", f.Name)
case gen.GTE:
if f.IsString() {
return fmt.Sprintf("Filters field %q to be longer than or equal in length to the provided value.", f.Name)
}
return fmt.Sprintf("Filters field %q to be greater than or equal to the provided value.", f.Name)
case gen.LT:
if f.IsString() {
return fmt.Sprintf("Filters field %q to be shorter than the provided value.", f.Name)
}
return fmt.Sprintf("Filters field %q to be less than the provided value.", f.Name)
case gen.LTE:
if f.IsString() {
return fmt.Sprintf("Filters field %q to be shorter than or equal in length to the provided value.", f.Name)
}
return fmt.Sprintf("Filters field %q to be less than or equal to the provided value.", f.Name)
case gen.IsNil:
return fmt.Sprintf("Filters field %q to be null/nil.", f.Name)
case gen.NotNil:
return fmt.Sprintf("Filters field %q to be not null/nil.", f.Name)
case gen.In:
return fmt.Sprintf("Filters field %q to be within the provided values.", f.Name)
case gen.NotIn:
return fmt.Sprintf("Filters field %q to be not within the provided values.", f.Name)
case gen.EqualFold:
return fmt.Sprintf("Filters field %q to be equal to the provided value, case-insensitive.", f.Name)
case gen.Contains:
return fmt.Sprintf("Filters field %q to contain the provided value.", f.Name)
case gen.ContainsFold:
return fmt.Sprintf("Filters field %q to contain the provided value, case-insensitive.", f.Name)
case gen.HasPrefix:
return fmt.Sprintf("Filters field %q to start with the provided value.", f.Name)
case gen.HasSuffix:
return fmt.Sprintf("Filters field %q to end with the provided value.", f.Name)
default:
panic("unknown predicate")
}
}
// FilterableFieldOp represents a filterable field, that filters based on a specific
// operation (e.g. eq, neq, gt, lt, etc).
type FilterableFieldOp struct {
Type *gen.Type
Edge *gen.Edge // Edge may be nil.
Field *gen.Field // Field may be nil (if so, assume we want a parameter to check for the edges existence).
Operation gen.Op // The associated operation.
fieldSchema *ogen.Schema // The base schema for the field, this may change based on the operation provided.
}
// ParameterName returns the raw query parameter name for the filterable field.
func (f *FilterableFieldOp) ParameterName() string {
if f.Edge != nil {
if f.Field == nil {
return "has." + CamelCase(SnakeCase(Singularize(f.Edge.Name)))
}
return CamelCase(SnakeCase(Singularize(f.Edge.Name))) + "." + CamelCase(f.Field.Name) + "." + predicateFormat(f.Operation)
}
return CamelCase(f.Field.Name) + "." + predicateFormat(f.Operation)
}
// ComponentName returns the name/component alias for the parameter.
func (f *FilterableFieldOp) ComponentName() string {
if f.Edge != nil {
if f.Field == nil {
return "EdgeHas" + PascalCase(Singularize(f.Edge.Name))
}
return "Edge" + PascalCase(Singularize(f.Edge.Name)) + PascalCase(f.Field.Name) + PascalCase(f.Operation.Name())
}
return PascalCase(f.Type.Name) + PascalCase(f.Field.Name) + PascalCase(f.Operation.Name())
}
// StructTag returns the struct tag for the filterable field, which uses json and
// github.com/go-playground/validator based tags by default.
func (f *FilterableFieldOp) StructTag() string {
return fmt.Sprintf(
`form:%q json:%q`,
f.ParameterName()+",omitempty",
SnakeCase(f.ComponentName())+",omitempty",
)
}
func generatePredicateBuilder(
t *gen.Type,
f *gen.Field,
e *gen.Edge,
op gen.Op,
structName, componentName string,
) string {
if op.Niladic() {
if e != nil {
if f == nil {
return fmt.Sprintf("%s.Has%s()", t.Package(), e.StructField())
}
pkg := t.Package()
if e.Ref != nil {
pkg = e.Ref.Type.Package()
} else if e.Owner != nil {
pkg = e.Owner.Package()
}
return fmt.Sprintf(
"%s.Has%sWith(%s.%s%s())",
pkg,
e.StructField(),
t.Package(),
f.StructField(),
op.Name(),
)
}
return fmt.Sprintf("%s.%s%s()", t.Package(), f.StructField(), op.Name())
}
ftype := structName + "." + componentName
if op.Variadic() {
ftype += "..."
} else {
ftype = "*" + ftype
}
builder := fmt.Sprintf(
"%s.%s%s(%s)",
t.Package(),
f.StructField(),
op.Name(),
ftype,
)
if e != nil {
pkg := t.Package()
if e.Ref != nil {
pkg = e.Ref.Type.Package()
} else if e.Owner != nil {
pkg = e.Owner.Package()
}
return fmt.Sprintf("%s.Has%sWith(%s)", pkg, e.StructField(), builder)
}
return builder
}
func (f *FilterableFieldOp) PredicateBuilder(structName string) string {
return generatePredicateBuilder(
f.Type,
f.Field,
f.Edge,
f.Operation,
structName,
f.ComponentName(),
)
}
// TypeString returns the struct field type for the filterable field.
func (f *FilterableFieldOp) TypeString() string {
if (f.Edge != nil && f.Field == nil) || f.Operation.Niladic() {
return "*bool"
}
if f.Operation.Variadic() {
return "[]" + f.Field.Type.String()
}
return "*" + f.Field.Type.String()
}
// Description returns a description for the filterable field.
func (f *FilterableFieldOp) Description() string {
if f.Edge != nil && f.Field == nil {
return fmt.Sprintf("If true, only return entities that have a %s edge.", Singularize(f.Edge.Name))
}
return predicateDescription(f.Field, f.Operation)
}
// Parameter returns the parameter for the filterable field.
func (f *FilterableFieldOp) Parameter() *ogen.Parameter {
if f.Edge != nil && f.Field == nil {
return &ogen.Parameter{
Name: f.ParameterName(),
In: "query",
Description: f.Description(),
Schema: &ogen.Schema{Type: "boolean"},
}
}
schema := &ogen.Schema{
Ref: f.fieldSchema.Ref,
Type: f.fieldSchema.Type,
Format: f.fieldSchema.Format,
Items: f.fieldSchema.Items,
Minimum: f.fieldSchema.Minimum,
Maximum: f.fieldSchema.Maximum,
MinLength: f.fieldSchema.MinLength,
MaxLength: f.fieldSchema.MaxLength,
Enum: f.fieldSchema.Enum,
Deprecated: f.fieldSchema.Deprecated,
}
if f.Operation == gen.GT || f.Operation == gen.LT || f.Operation == gen.GTE || f.Operation == gen.LTE {
if schema.Items != nil && f.fieldSchema.Format != "date-time" {
schema.Items.Item.Type = "number"
} else if f.fieldSchema.Format != "date-time" {
schema.Type = "number"
}
}
if f.Operation == gen.IsNil {
if schema.Items != nil {
schema.Items.Item.Type = "boolean"
schema.Items.Item.Format = ""
} else {
schema.Type = "boolean"
schema.Format = ""
}
}
if f.Operation.Variadic() {
schema = schema.AsArray() // If not already.
}
return &ogen.Parameter{
Name: f.ParameterName(),
In: "query",
Description: f.Description(),
Schema: schema,
}
}
// GetFilterableFields returns a list of filterable fields for the given type, where
// the key is the component name, and the value is the parameter which includes the
// name, description and schema for the parameter.
func GetFilterableFields(t *gen.Type, edge *gen.Edge) (filters []*FilterableFieldOp) {
cfg := GetConfig(t.Config)
ta := GetAnnotation(t)
if ta.GetSkip(cfg) {
return nil
}
fields := t.Fields
if cfg.DefaultFilterID && t.ID != nil && (edge == nil || edge.Field() == nil) {
ida := GetAnnotation(t.ID)
if ida.Filter == 0 {
ida.Filter = FilterGroupEqualExact | FilterGroupArray
}
t.ID.Annotations.Set(ida.Name(), ida)
fields = append([]*gen.Field{t.ID}, fields...)
}
for _, f := range fields {
fa := GetAnnotation(f)
if fa.Filter == 0 || fa.GetSkip(cfg) || f.Sensitive() {
continue
}
for _, op := range intersectSorted(f.Ops(), fa.Filter.Explode()) {
if f.IsBool() && op == gen.NEQ {
continue // Since you can use EQ=false instead.
}
fieldSchema, err := GetSchemaField(f)
if err != nil {
continue // Just skip things that can't be generated/easily mapped.
}
if _, asRef, _, ok := hoistEnums(t, f, fieldSchema); ok {
fieldSchema = asRef
}
filters = append(filters, &FilterableFieldOp{
Type: t,
Edge: edge,
Field: f,
Operation: op,
fieldSchema: fieldSchema,
})
}
}
if edge == nil {
for _, e := range t.Edges {
ea := GetAnnotation(e)
if ea.GetSkip(cfg) || ea.Filter == 0 || ea.Filter != FilterEdge {
continue
}
filters = append(filters, &FilterableFieldOp{
Type: t,
Edge: e,
Operation: gen.IsNil,
fieldSchema: &ogen.Schema{
Type: "boolean",
},
})
filters = append(filters, GetFilterableFields(e.Type, e)...)
}
}
return filters
}
type FilterGroup struct {
Name string
Type *gen.Type
FieldType *field.TypeInfo
Operations []gen.Op
FieldPairs []*FilterGroupFieldPair
Schema *ogen.Schema
}
// ParameterName returns the raw query parameter name for the filter group.
func (g *FilterGroup) ParameterName(op gen.Op) string {
return g.Name + "." + predicateFormat(op)
}
// Parameter returns the parameter for the filter group.
func (g *FilterGroup) Parameter(op gen.Op) *ogen.Parameter {
return &ogen.Parameter{
Name: g.ParameterName(op),
In: "query",
Description: g.Description(op),
Schema: g.Schema,
}
}
func (g *FilterGroup) Description(op gen.Op) string {
var fields []string
for _, fp := range g.FieldPairs {
if fp.Edge != nil {
fields = append(fields, fp.Edge.Name+"."+fp.Field.Name)
continue
}
fields = append(fields, fp.Field.Name)
}
return fmt.Sprintf(
"Field %q filters across multiple fields (case insensitive): %s.",
g.ParameterName(op),
strings.Join(fields, ", "),
)
}
// StructTag returns the struct tag for the filter group, which uses json and
// github.com/go-playground/validator based tags by default.
func (g *FilterGroup) StructTag(op gen.Op) string {
return fmt.Sprintf(
`form:%q json:%q`,
g.ParameterName(op)+",omitempty",
SnakeCase(g.ComponentName(op))+",omitempty",
)
}
func (g *FilterGroup) PredicateBuilder(structName string, op gen.Op) string {
component := g.ComponentName(op)
var fields []string
for _, fp := range g.FieldPairs {
fields = append(fields, generatePredicateBuilder(
fp.Type,
fp.Field,
fp.Edge,
op,
structName,
component,
))
}
return fmt.Sprintf("sql.OrPredicates(\n%s,\n)", strings.Join(fields, ",\n"))
}
// TypeString returns the struct field type for the filter group.
func (g *FilterGroup) TypeString(op gen.Op) string {
if op.Niladic() {
return "*bool"
}
if op.Variadic() {
return "[]" + g.FieldType.String()
}
return "*" + g.FieldType.String()
}
// ComponentName returns the name/component alias for the parameter.
func (g *FilterGroup) ComponentName(op gen.Op) string {
return PascalCase(g.Type.Name) + "FilterGroup" + PascalCase(g.Name) + PascalCase(op.Name())
}
type FilterGroupFieldPair struct {
Type *gen.Type
Edge *gen.Edge
Field *gen.Field
}
func GetFilterGroups(t *gen.Type, edge *gen.Edge) []*FilterGroup {
cfg := GetConfig(t.Config)
ta := GetAnnotation(t)
if ta.GetSkip(cfg) {
return nil
}
groups := map[string]*FilterGroup{}
for _, f := range t.Fields {
fa := GetAnnotation(f)
if fa.FilterGroup == "" || fa.GetSkip(cfg) || f.Sensitive() {
continue
}
fieldSchema, err := GetSchemaField(f)
if err != nil {
panic(fmt.Sprintf(
"failed to generate schema for field %s within filter group %q: %v",
f.StructField(),
fa.FilterGroup,
err,
))
}
ops := intersectSorted(f.Ops(), fa.Filter.Explode())
if f.IsBool() {
ops = slices.DeleteFunc(ops, func(op gen.Op) bool {
return op == gen.NEQ
})
}
if len(ops) == 0 {
panic(fmt.Sprintf(
"filter group %q on type %q and field %q has no filter predicates configured",
fa.FilterGroup,
t.Name,
f.StructField(),
))
}
if _, ok := groups[fa.FilterGroup]; !ok {
groups[fa.FilterGroup] = &FilterGroup{
Name: fa.FilterGroup,
Type: t,
FieldType: f.Type,
Operations: ops,
Schema: fieldSchema,
}
}
if groups[fa.FilterGroup].FieldType.String() != f.Type.String() {
panic(fmt.Sprintf(
"filter group %q on type %q and field %q has a different type than another field in the group: %q vs %q",
fa.FilterGroup,
t.Name,
f.StructField(),
groups[fa.FilterGroup].FieldType.String(),
f.Type.String(),
))
}
if newOps := intersectSorted(groups[fa.FilterGroup].Operations, ops); len(groups[fa.FilterGroup].Operations) < len(newOps) {
if len(newOps) == 0 {
panic(fmt.Sprintf(
"filter group %q on type %q and field %q has no intersecting predicates when compared to all other fields in the group",
fa.FilterGroup,
t.Name,
f.StructField(),
))
}
groups[fa.FilterGroup].Operations = newOps
}
groups[fa.FilterGroup].FieldPairs = append(groups[fa.FilterGroup].FieldPairs, &FilterGroupFieldPair{
Type: t,
Field: f,
})
}
if edge == nil {
for _, e := range t.Edges {
ea := GetAnnotation(e)
// Only include the edge as part of the inner filter group if it has a filter group
// that's a part of the main type.
if _, ok := groups[ea.FilterGroup]; !ok || ea.GetSkip(cfg) {
continue
}
edgeGroups := GetFilterGroups(e.Type, e)
for _, eg := range edgeGroups {
if _, ok := groups[eg.Name]; !ok {
groups[eg.Name] = eg
continue
}
groups[eg.Name].FieldPairs = append(groups[eg.Name].FieldPairs, eg.FieldPairs...)
}
}
}
return slices.SortedFunc(maps.Values(groups), func(a, b *FilterGroup) int {
return strings.Compare(a.Name, b.Name)
})
}