forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.go
340 lines (285 loc) · 7.14 KB
/
builtins.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
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package ast
// Builtins is the registry of built-in functions supported by OPA.
// Call RegisterBuiltin to add a new built-in.
var Builtins []*Builtin
// RegisterBuiltin adds a new built-in function to the registry.
func RegisterBuiltin(b *Builtin) {
Builtins = append(Builtins, b)
BuiltinMap[b.Name] = b
if len(b.Infix) > 0 {
BuiltinMap[b.Infix] = b
}
}
// DefaultBuiltins is the registry of built-in functions supported in OPA
// by default. When adding a new built-in function to OPA, update this
// list.
var DefaultBuiltins = [...]*Builtin{
// =
Equality,
// Comparisons
GreaterThan, GreaterThanEq, LessThan, LessThanEq, NotEqual,
// Arithmetic
Plus, Minus, Multiply, Divide, Round, Abs,
// Binary
And, Or,
// Aggregates
Count, Sum, Max,
// Casting
ToNumber,
// Regular Expressions
RegexMatch,
// Sets
SetDiff,
// Strings
Concat, FormatInt, IndexOf, Substring, Lower, Upper, Contains, StartsWith, EndsWith,
}
// BuiltinMap provides a convenient mapping of built-in names to
// built-in definitions.
var BuiltinMap map[Var]*Builtin
/**
* Unification
*/
// Equality represents the "=" operator.
var Equality = &Builtin{
Name: Var("eq"),
Infix: Var("="),
NumArgs: 2,
TargetPos: []int{0, 1},
}
/**
* Comparisons
*/
// GreaterThan represents the ">" comparison operator.
var GreaterThan = &Builtin{
Name: Var("gt"),
Infix: Var(">"),
NumArgs: 2,
}
// GreaterThanEq represents the ">=" comparison operator.
var GreaterThanEq = &Builtin{
Name: Var("gte"),
Infix: Var(">="),
NumArgs: 2,
}
// LessThan represents the "<" comparison operator.
var LessThan = &Builtin{
Name: Var("lt"),
Infix: Var("<"),
NumArgs: 2,
}
// LessThanEq represents the "<=" comparison operator.
var LessThanEq = &Builtin{
Name: Var("lte"),
Infix: Var("<="),
NumArgs: 2,
}
// NotEqual represents the "!=" comparison operator.
var NotEqual = &Builtin{
Name: Var("neq"),
Infix: Var("!="),
NumArgs: 2,
}
/**
* Arithmetic
*/
// Plus adds two numbers together.
var Plus = &Builtin{
Name: Var("plus"),
Infix: Var("+"),
NumArgs: 3,
TargetPos: []int{2},
}
// Minus subtracts the second number from the first number or computes the diff
// between two sets.
var Minus = &Builtin{
Name: Var("minus"),
Infix: Var("-"),
NumArgs: 3,
TargetPos: []int{2},
}
// Multiply multiplies two numbers together.
var Multiply = &Builtin{
Name: Var("mul"),
Infix: Var("*"),
NumArgs: 3,
TargetPos: []int{2},
}
// Divide divides the first number by the second number.
var Divide = &Builtin{
Name: Var("div"),
Infix: Var("/"),
NumArgs: 3,
TargetPos: []int{2},
}
// Round rounds the number up to the nearest integer.
var Round = &Builtin{
Name: Var("round"),
NumArgs: 2,
TargetPos: []int{1},
}
// Abs returns the number without its sign.
var Abs = &Builtin{
Name: Var("abs"),
NumArgs: 2,
TargetPos: []int{1},
}
/**
* Binary
*/
// And performs a binary AND operation on numbers and an intersection operation
// on sets.
var And = &Builtin{
Name: Var("and"),
Infix: Var("&"),
NumArgs: 3,
TargetPos: []int{2},
}
// Or performs a binary OR operation on numbers and a union operation on sets.
var Or = &Builtin{
Name: Var("or"),
Infix: Var("|"),
NumArgs: 3,
TargetPos: []int{2},
}
/**
* Aggregates
*/
// Count takes a collection and counts the number of elements in it.
var Count = &Builtin{
Name: Var("count"),
NumArgs: 2,
TargetPos: []int{1},
}
// Sum takes an array of numbers and sums them.
var Sum = &Builtin{
Name: Var("sum"),
NumArgs: 2,
TargetPos: []int{1},
}
// Max returns the maximum value in a collection.
var Max = &Builtin{
Name: Var("max"),
NumArgs: 2,
TargetPos: []int{1},
}
/**
* Casting
*/
// ToNumber takes a string, bool, or number value and converts it to a number.
// Strings are converted to numbers using strconv.Atoi.
// Boolean false is converted to 0 and boolean true is converted to 1.
var ToNumber = &Builtin{
Name: Var("to_number"),
NumArgs: 2,
TargetPos: []int{1},
}
/**
* Regular Expressions
*/
// RegexMatch takes two strings and evaluates to true if the string in the second
// position matches the pattern in the first position.
var RegexMatch = &Builtin{
Name: Var("re_match"),
NumArgs: 2,
}
/**
* Strings
*/
// Concat joins an array of strings to with an input string.
var Concat = &Builtin{
Name: Var("concat"),
NumArgs: 3,
TargetPos: []int{2},
}
// FormatInt returns the string representation of the number in the given base after converting it to an integer value.
var FormatInt = &Builtin{
Name: Var("format_int"),
NumArgs: 3,
TargetPos: []int{2},
}
// IndexOf returns the index of a substring contained inside a string
var IndexOf = &Builtin{
Name: Var("indexof"),
NumArgs: 3,
TargetPos: []int{2},
}
// Substring returns the portion of a string for a given start index and a length.
// If the length is less than zero, then substring returns the remainder of the string.
var Substring = &Builtin{
Name: Var("substring"),
NumArgs: 4,
TargetPos: []int{3},
}
// Contains returns true if the search string is included in the base string
var Contains = &Builtin{
Name: Var("contains"),
NumArgs: 2,
}
// StartsWith returns true if the search string begins with the base string
var StartsWith = &Builtin{
Name: Var("startswith"),
NumArgs: 2,
}
// EndsWith returns true if the search string begins with the base string
var EndsWith = &Builtin{
Name: Var("endswith"),
NumArgs: 2,
}
// Lower returns the input string but with all characters in lower-case
var Lower = &Builtin{
Name: Var("lower"),
NumArgs: 2,
TargetPos: []int{1},
}
// Upper returns the input string but with all characters in upper-case
var Upper = &Builtin{
Name: Var("upper"),
NumArgs: 2,
TargetPos: []int{1},
}
/**
* Deprecated built-ins.
*/
// SetDiff has been replaced by the minus built-in.
var SetDiff = &Builtin{
Name: Var("set_diff"),
NumArgs: 3,
TargetPos: []int{2},
}
// Builtin represents a built-in function supported by OPA. Every
// built-in function is uniquely identified by a name.
type Builtin struct {
Name Var // Unique name of built-in function, e.g., <Name>(term,term,...,term)
Infix Var // Unique name of infix operator. Default should be unset.
NumArgs int // Total number of args required by built-in.
TargetPos []int // Argument positions that bind outputs. Indexing is zero-based.
}
// Expr creates a new expression for the built-in with the given terms.
func (b *Builtin) Expr(terms ...*Term) *Expr {
ts := []*Term{VarTerm(string(b.Name))}
for _, t := range terms {
ts = append(ts, t)
}
return &Expr{
Terms: ts,
}
}
// IsTargetPos returns true if a variable in the i-th position will be
// bound when the expression is evaluated.
func (b *Builtin) IsTargetPos(i int) bool {
for _, x := range b.TargetPos {
if x == i {
return true
}
}
return false
}
func init() {
BuiltinMap = map[Var]*Builtin{}
for _, b := range DefaultBuiltins {
RegisterBuiltin(b)
}
}