forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
topdown: Honor default keyword on functions
Default functions satisfy the following properties: * Same arity as other functions with the same name * Arguments should only be plain variables ie. no composite values. For ex, default f([x]) = 1 is an invalid default function * Variable names should not be repeated ie. default f(x, x) = 1 is an invalid default function Fixes: open-policy-agent#2445 Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
- Loading branch information
1 parent
57c4daa
commit a1ca32a
Showing
7 changed files
with
209 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 95 additions & 17 deletions
112
test/cases/testdata/functions/test-functions-default.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,99 @@ | ||
cases: | ||
- data: | ||
modules: | ||
- | | ||
package p.m | ||
default hello = false | ||
hello() = m { | ||
m = input.message | ||
1 == 2 | ||
m = "world" | ||
} | ||
h = m { | ||
m = hello() | ||
} | ||
note: functions/default # not supported but shouldn't panic | ||
query: data.p.m = x | ||
- | | ||
package test | ||
default f(x) = 1 | ||
f(x) = x { | ||
x > 0 | ||
} | ||
p { | ||
f(-1) == 1 | ||
} | ||
note: functions/default | ||
query: data.test.p = x | ||
want_result: | ||
- x: true | ||
|
||
- data: | ||
modules: | ||
- | | ||
package test | ||
default f(x) = 1 | ||
f(x) = x { | ||
x > 0 | ||
} | ||
p { | ||
f(2) == 2 | ||
} | ||
note: functions/non default | ||
query: data.test.p = x | ||
want_result: | ||
- x: true | ||
|
||
- data: | ||
modules: | ||
- | | ||
package test | ||
default f(x) = 1 | ||
p { | ||
f(2) == 1 | ||
} | ||
note: functions/only default | ||
query: data.test.p = x | ||
want_result: | ||
- x: true | ||
|
||
- data: | ||
modules: | ||
- | | ||
package test | ||
default f(_, _) = 1 | ||
f(x, y) = x { | ||
x == y | ||
} | ||
p { | ||
f(2, 2) == 2 | ||
} | ||
note: functions/only default | ||
query: data.test.p = x | ||
want_result: | ||
- x: true | ||
|
||
- data: | ||
modules: | ||
- | | ||
package test | ||
default f(x) = 1000 | ||
f(x) = x { | ||
x > 0 | ||
} | ||
p = xs { | ||
xs := [y | x = [1, -2, 3][_]; y := f(x)] | ||
} | ||
note: functions/comprehensions | ||
query: data.test.p = x | ||
want_result: | ||
- x: | ||
hello: false | ||
- x: | ||
- 1 | ||
- 1000 | ||
- 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters