-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_filter.go
More file actions
168 lines (148 loc) · 5.76 KB
/
lambda_filter.go
File metadata and controls
168 lines (148 loc) · 5.76 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 traverse
import (
"fmt"
"strings"
)
// LambdaBuilder constructs the expression inside an OData lambda operator (any/all).
//
// LambdaBuilder is obtained from [QueryBuilder.LambdaAny] or [QueryBuilder.LambdaAll]
// via a callback function. Use Field() to start building conditions within the lambda.
//
// The variable name used in the lambda expression is automatically derived from the
// first letter of the collection field name (e.g. "tags" -> "t", "items" -> "i").
type LambdaBuilder struct {
varName string
exprs []string
}
// Field begins a condition expression for the given field within the lambda.
// The field is referenced as varName/fieldName in the generated OData expression.
//
// b.Field("name").Eq("admin")
// // produces: t/name eq 'admin' (when varName is "t")
func (b *LambdaBuilder) Field(name string) *LambdaCondition {
return &LambdaCondition{
builder: b,
path: b.varName + "/" + name,
}
}
// build returns the combined lambda body expression, joining multiple conditions with " and ".
func (b *LambdaBuilder) build() string {
return strings.Join(b.exprs, " and ")
}
// LambdaCondition builds a comparison or function expression within a lambda.
//
// LambdaCondition is obtained from [LambdaBuilder.Field] and provides comparison
// and string function operators. Each operator appends an expression to the parent
// LambdaBuilder and returns the LambdaBuilder for further chaining.
type LambdaCondition struct {
builder *LambdaBuilder
path string
}
func (c *LambdaCondition) addExpr(expr string) *LambdaBuilder {
c.builder.exprs = append(c.builder.exprs, expr)
return c.builder
}
// Eq adds an equality condition: path eq value.
func (c *LambdaCondition) Eq(v any) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("%s eq %s", c.path, serializeValue(v)))
}
// Ne adds a not-equal condition: path ne value.
func (c *LambdaCondition) Ne(v any) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("%s ne %s", c.path, serializeValue(v)))
}
// Lt adds a less-than condition: path lt value.
func (c *LambdaCondition) Lt(v any) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("%s lt %s", c.path, serializeValue(v)))
}
// Le adds a less-than-or-equal condition: path le value.
func (c *LambdaCondition) Le(v any) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("%s le %s", c.path, serializeValue(v)))
}
// Gt adds a greater-than condition: path gt value.
func (c *LambdaCondition) Gt(v any) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("%s gt %s", c.path, serializeValue(v)))
}
// Ge adds a greater-than-or-equal condition: path ge value.
func (c *LambdaCondition) Ge(v any) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("%s ge %s", c.path, serializeValue(v)))
}
// Contains adds a contains() function condition.
func (c *LambdaCondition) Contains(s string) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("contains(%s, %s)", c.path, serializeValue(s)))
}
// StartsWith adds a startswith() function condition.
func (c *LambdaCondition) StartsWith(s string) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("startswith(%s, %s)", c.path, serializeValue(s)))
}
// EndsWith adds an endswith() function condition.
func (c *LambdaCondition) EndsWith(s string) *LambdaBuilder {
return c.addExpr(fmt.Sprintf("endswith(%s, %s)", c.path, serializeValue(s)))
}
// lambdaVarName derives a short variable name from a collection field.
// "tags" -> "t", "items" -> "i", "orderItems" -> "o".
func lambdaVarName(collectionField string) string {
// Strip any navigation path prefix (e.g. "Order/Tags" -> use "Tags")
if idx := strings.LastIndex(collectionField, "/"); idx >= 0 {
collectionField = collectionField[idx+1:]
}
if len(collectionField) == 0 {
return "x"
}
return strings.ToLower(string(collectionField[0]))
}
// appendFilter combines a new expression with any existing filter using "and".
func (q *QueryBuilder) appendFilter(expr string) *QueryBuilder {
if q.filterExpr == "" {
q.filterExpr = expr
} else {
q.filterExpr = q.filterExpr + " and " + expr
}
q.urlDirty = true
return q
}
// FilterLambda appends a raw lambda expression string to the query filter.
// Use this for complex lambda expressions that cannot be expressed through
// the typed LambdaAny/LambdaAll builders.
//
// The expression is combined with any existing filter using "and".
//
// query.FilterLambda("tags/any(t: t/name eq 'admin')")
func (q *QueryBuilder) FilterLambda(expression string) *QueryBuilder {
return q.appendFilter(expression)
}
// LambdaAny appends an OData "any" lambda filter to the query.
// The fn callback receives a LambdaBuilder; use Field() to build conditions.
//
// Generated OData: collectionField/any(v: <expression>)
//
// The result is combined with any existing filter using "and".
//
// query.LambdaAny("Tags", func(b *LambdaBuilder) {
// b.Field("Name").Eq("admin")
// })
// // $filter=Tags/any(t: t/Name eq 'admin')
func (q *QueryBuilder) LambdaAny(collectionField string, fn func(*LambdaBuilder)) *QueryBuilder {
varName := lambdaVarName(collectionField)
lb := &LambdaBuilder{varName: varName}
fn(lb)
expr := fmt.Sprintf("%s/any(%s: %s)", collectionField, varName, lb.build())
return q.appendFilter(expr)
}
// LambdaAll appends an OData "all" lambda filter to the query.
// The fn callback receives a LambdaBuilder; use Field() to build conditions.
//
// Generated OData: collectionField/all(v: <expression>)
//
// The result is combined with any existing filter using "and".
//
// query.LambdaAll("Items", func(b *LambdaBuilder) {
// b.Field("Price").Gt(100)
// })
// // $filter=Items/all(i: i/Price gt 100)
func (q *QueryBuilder) LambdaAll(collectionField string, fn func(*LambdaBuilder)) *QueryBuilder {
varName := lambdaVarName(collectionField)
lb := &LambdaBuilder{varName: varName}
fn(lb)
expr := fmt.Sprintf("%s/all(%s: %s)", collectionField, varName, lb.build())
return q.appendFilter(expr)
}