1 package filtering
2
3 import (
4 "time"
5
6 expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
7 )
8
9 func Not(arg *expr.Expr) *expr.Expr {
10 return Function(FunctionNot, arg)
11 }
12
13 func Member(operand *expr.Expr, field string) *expr.Expr {
14 return &expr.Expr{
15 ExprKind: &expr.Expr_SelectExpr{
16 SelectExpr: &expr.Expr_Select{
17 Operand: operand,
18 Field: field,
19 },
20 },
21 }
22 }
23
24 func Function(name string, args ...*expr.Expr) *expr.Expr {
25 return &expr.Expr{
26 ExprKind: &expr.Expr_CallExpr{
27 CallExpr: &expr.Expr_Call{
28 Function: name,
29 Args: args,
30 },
31 },
32 }
33 }
34
35 func Float(value float64) *expr.Expr {
36 return &expr.Expr{
37 ExprKind: &expr.Expr_ConstExpr{
38 ConstExpr: &expr.Constant{
39 ConstantKind: &expr.Constant_DoubleValue{
40 DoubleValue: value,
41 },
42 },
43 },
44 }
45 }
46
47 func Duration(value time.Duration) *expr.Expr {
48 return Function(FunctionDuration, String(value.String()))
49 }
50
51 func Timestamp(value time.Time) *expr.Expr {
52 return Function(FunctionTimestamp, String(value.Format(time.RFC3339)))
53 }
54
55 func Int(value int64) *expr.Expr {
56 return &expr.Expr{
57 ExprKind: &expr.Expr_ConstExpr{
58 ConstExpr: &expr.Constant{
59 ConstantKind: &expr.Constant_Int64Value{
60 Int64Value: value,
61 },
62 },
63 },
64 }
65 }
66
67 func Equals(lhs, rhs *expr.Expr) *expr.Expr {
68 return Function(FunctionEquals, lhs, rhs)
69 }
70
71 func NotEquals(lhs, rhs *expr.Expr) *expr.Expr {
72 return Function(FunctionNotEquals, lhs, rhs)
73 }
74
75 func Has(lhs, rhs *expr.Expr) *expr.Expr {
76 return Function(FunctionHas, lhs, rhs)
77 }
78
79 func Or(args ...*expr.Expr) *expr.Expr {
80 if len(args) <= 2 {
81 return Function(FunctionOr, args...)
82 }
83 result := Function(FunctionOr, args[:2]...)
84 for _, arg := range args[2:] {
85 result = Function(FunctionOr, result, arg)
86 }
87 return result
88 }
89
90 func And(args ...*expr.Expr) *expr.Expr {
91 if len(args) <= 2 {
92 return Function(FunctionAnd, args...)
93 }
94 result := Function(FunctionAnd, args[:2]...)
95 for _, arg := range args[2:] {
96 result = Function(FunctionAnd, result, arg)
97 }
98 return result
99 }
100
101 func LessThan(lhs, rhs *expr.Expr) *expr.Expr {
102 return Function(FunctionLessThan, lhs, rhs)
103 }
104
105 func LessEquals(lhs, rhs *expr.Expr) *expr.Expr {
106 return Function(FunctionLessEquals, lhs, rhs)
107 }
108
109 func GreaterEquals(lhs, rhs *expr.Expr) *expr.Expr {
110 return Function(FunctionGreaterEquals, lhs, rhs)
111 }
112
113 func GreaterThan(lhs, rhs *expr.Expr) *expr.Expr {
114 return Function(FunctionGreaterThan, lhs, rhs)
115 }
116
117 func Text(text string) *expr.Expr {
118 return &expr.Expr{
119 ExprKind: &expr.Expr_IdentExpr{
120 IdentExpr: &expr.Expr_Ident{
121 Name: text,
122 },
123 },
124 }
125 }
126
127 func String(s string) *expr.Expr {
128 return &expr.Expr{
129 ExprKind: &expr.Expr_ConstExpr{
130 ConstExpr: &expr.Constant{
131 ConstantKind: &expr.Constant_StringValue{
132 StringValue: s,
133 },
134 },
135 },
136 }
137 }
138
139 func Expression(sequences ...*expr.Expr) *expr.Expr {
140 return And(sequences...)
141 }
142
143 func Sequence(factors ...*expr.Expr) *expr.Expr {
144 if len(factors) <= 2 {
145 return Function(FunctionFuzzyAnd, factors...)
146 }
147 result := Function(FunctionFuzzyAnd, factors[:2]...)
148 for _, arg := range factors[2:] {
149 result = Function(FunctionFuzzyAnd, result, arg)
150 }
151 return result
152 }
153
154 func Factor(terms ...*expr.Expr) *expr.Expr {
155 return Or(terms...)
156 }
157
View as plain text