Commit Graph

9 Commits

Author SHA1 Message Date
Anders Eknert dfd4be45b3 Remove any data attrubutes not used in the "YAML tests" (#4817)
Fixes #4813

Signed-off-by: Anders Eknert <anders@eknert.com>
2022-06-28 10:18:35 +02:00
Shuhei Kitagawa 52ddfd9149 topdown: Support indexof_n built-in function (#4172)
Fixes https://github.com/open-policy-agent/opa/issues/4155

Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>
2022-01-06 09:58:27 +01:00
Kristian Svalland 6f81c4a620 Add array.reverse(array) and strings.reverse(string) built-in functions. (#4161)
The function `array.reverse` takes an array as an argument, and returns an array with a reversed order of elements.
The function `strings.reverse` takes a string as an argument, and returns a string with a reversed order of unicode code points.
WASM support is included for both built-ins.

Fixes #3736

Signed-off-by: Kristian Svalland <kristian.svalland@gmail.com>
2021-12-27 12:47:39 +01:00
Anders Eknert 7841d59619 Fix indexof regression (#3610)
Use whole string for comparison, not just first char.

Signed-off-by: Anders Eknert <anders@eknert.com>
2021-07-02 11:35:00 +02:00
Stephan Renatus bba7b9ad02 wasm+topdown: misc utf-8 related issues (#3576)
* wasm: count() invalid utf-8 runes, don't abort

Previously, we'd bail out when the string count() is given contained
anything that isn't a valid utf-8 rune. Now, we'll have it count the
invalid chars -- they're replaced by chartorune() in the same manner
as happens when passing casting the string to []rune in golang:

    package main

    import (
    	"encoding/base64"
    	"fmt"
    )

    func main() {
    	sample, _ := base64.StdEncoding.DecodeString("2E84ZuPUd7zfvCZSNEchVpDEIj6PL7JfLpIqyxVG16k=")
    	fmt.Printf("% x\n%x\n%d, %d\n", sample, []rune(string(sample)), len(sample), len([]rune(string(sample))))
    }

This yields:

    d8 4f 38 66 e3 d4 77 bc df bc 26 52 34 47 21 56 90 c4 22 3e 8f 2f b2 5f 2e 92 2a cb 15 46 d7 a9
    [fffd 4f 38 66 fffd fffd 77 fffd 7fc 26 52 34 47 21 56 fffd fffd 22 3e fffd 2f fffd 5f 2e fffd 2a fffd 15 46 5e9]
    32, 30

Where fffd is the replacement char whenever something isn't a proper rune.

* topdown/builtins: fix indexof() result when using unicode

Previously, indexof() would count unicode characters as strings, so

    indexof("μx", "x")

would return 2 insteads of 1.

Now, we're converting to rune and compare explicitly, returnig the
proper result.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-06-22 11:56:07 +02:00
Stephan Renatus 372f07e3b2 wasm: synchronise behaviour with non-strict-builtin-error mode in topdown (#3022)
In topdown, we have two different error modes: strict/non-strict.
In WASM, everything is meant to be non-strict. Thus errors that
only appear in topdown with strict mode are annotated as such,
and checked for an empty result-set in the WASM test runner.

Several WASM builtins that have returned an error where they should
return NULL have been adjusted.

This allows us to fix most of the exceptions brought up in #2954.

Notable pieces:

* wasm sdk: ignore builtin errors

This should be in line with the non-strict builtin error semantics used
in WASM.

Before, when the WASM SDK had called out top a topdown-defined builtin,
and that builtin had returned an error, the WASM caller returned that
error. It's been at odds with how topdown evaluated builtin errors when
run without strict builtin errors.

Now, the errors are properly ignored, except for topdown.Halt. That one
doesn't seem like it's used at the moment, at least from this code base.

* cases: add want_result where non-strict eval yields something

This happens to work for both topdown and wasm:

- in topdown, the test runner checks for expected errors first, and
  ignores the wanted result;
- in wasm, we check for a desired result first, checking the error
  if no result was defined.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-01-08 15:25:37 +01:00
Stephan Renatus 9e52194ccf test/cases: disregard ordering in "strings/concat: set" test
Since anything can happen, we now list the possibilities (3! = 6),
and check that any of them was the result.

Fixes #2924.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2020-12-14 09:49:39 -05:00
Anders Eknert c84109a71f Fix substring and count unicode handling
This is done by working with the runes directly, both when counting length of strings as well as when slicing them to substrings.

Fixes #2799

Signed-off-by: Anders Eknert <anders.eknert@bisnode.com>
2020-10-23 09:59:47 -07:00
Torin Sandall 3982a3eac1 test: Move test cases out of topdown package
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-08-20 12:48:20 -04:00