Special cases of refs to data (#41)

1. Numeric indices will be converted to strings for refs beginning with `data`
   if there is not valid numeric key
2. Error out if input document already contains value for a ref

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2023-11-11 00:15:56 -08:00
committed by GitHub
parent 02c6c6b827
commit 1c492144b3
6 changed files with 164 additions and 87 deletions
+69
View File
@@ -0,0 +1,69 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
cases:
- note: numbers are converted to string as needed when indexing data
data:
"1": "hello"
"1.2": "world"
test:
"2": 100
"2.2": 200
play:
"2": 100
modules:
- |
package test
p = v {
q = data.play
# 2 is not converted to "2" since refr doesn't being with `data`
v = q[2]
}
a = [
data[1],
data[1.2],
data.test[2],
data.test[2.2]
]
query: data.test
want_result:
"2": 100
"2.2": 200
a:
- "hello"
- "world"
- 100
- 200
- note: overriding refs in data produces error
data:
test:
rule1: 0
modules:
- |
package test
rule1 = 6
query: data.test
error: value for rule has already been specified
- note: rule named data
data:
test:
rule1: 8
modules:
- |
package test
data.test.rule1 = 9
data.test.rule1 = 9
query: data.test
want_result:
rule1: 8
data:
test:
rule1: 9
+1 -1
View File
@@ -133,7 +133,7 @@ fn run_opa_tests() -> Result<()> {
} else {
for (i, m) in modules.iter().enumerate() {
std::fs::write(
path.join(format!("rego{n}_{i}.json")),
path.join(format!("rego{n}_{i}.rego")),
m.as_bytes(),
)?;
}