From 41c2cd45aab162c322c78d08d2d594e397f33c43 Mon Sep 17 00:00:00 2001 From: swh Date: Mon, 12 Aug 2024 13:58:56 -0700 Subject: [PATCH] Add type conversion function for string -> bool to standard definitions (#372) --- doc/langdef.md | 16 +++-- tests/simple/testdata/conversions.textproto | 70 ++++++++++++++++++++- tests/simple/testdata/proto3.textproto | 2 +- 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/doc/langdef.md b/doc/langdef.md index b8af4aa..fe48105 100644 --- a/doc/langdef.md +++ b/doc/langdef.md @@ -708,13 +708,13 @@ macros are: function given by the expression `t`, which can use the variable `x`. For instance, `[1, 2, 3].map(n, n * n)` evaluates to `[1, 4, 9]`. Any evaluation error for any element causes the macro to raise an error. - * transforms a map `e` by taking each key in the map `x` to the function + * transforms a map `e` by taking each key in the map `x` to the function given by the expression `t`, which can use the variable `x`. For - instance, `{'one': 1, 'two': 2}.map(k, k)` evaluates to `['one', 'two']`. + instance, `{'one': 1, 'two': 2}.map(k, k)` evaluates to `['one', 'two']`. Any evaluation error for any element causes the macro to raise an error. * `e.map(x, p, t)`: Same as the two-arg map but with a conditional `p` filter before the value is transformed. -* `e.filter(x, p)`: +* `e.filter(x, p)`: * for a list `e`, returns the sublist of all elements `x` which evaluate to `true` in the predicate expression `p` (which can use variable `x`). For instance, `[1, 2, 3].filter(i, i % 2 > 0)` evaluates to `[1, 3]`. @@ -1835,7 +1835,7 @@ See [cel-go/issues/9](https://github.com/google/cel-go/issues/9). - + bool @@ -1853,6 +1853,14 @@ See [cel-go/issues/9](https://github.com/google/cel-go/issues/9). identity + + + (string) -> bool + + + type conversion + + bytes diff --git a/tests/simple/testdata/conversions.textproto b/tests/simple/testdata/conversions.textproto index 75ffddd..ed8f408 100644 --- a/tests/simple/testdata/conversions.textproto +++ b/tests/simple/testdata/conversions.textproto @@ -505,6 +505,74 @@ section { value: { uint64_value: 300 } } } +section { + name: "bool" + description: "Conversions to bool" + test { + name: "string_1" + expr: "bool('1')" + value: { bool_value: true } + } + test { + name: "string_t" + expr: "bool('t')" + value: { bool_value: true } + } + test { + name: "string_true_lowercase" + expr: "bool('true')" + value: { bool_value: true } + } + test { + name: "string_true_uppercase" + expr: "bool('TRUE')" + value: { bool_value: true } + } + test { + name: "string_true_pascalcase" + expr: "bool('True')" + value: { bool_value: true } + } + test { + name: "string_0" + expr: "bool('0')" + value: { bool_value: false } + } + test { + name: "string_f" + expr: "bool('f')" + value: { bool_value: false } + } + test { + name: "string_false_lowercase" + expr: "bool('false')" + value: { bool_value: false } + } + test { + name: "string_false_uppercase" + expr: "bool('FALSE')" + value: { bool_value: false } + } + test { + name: "string_false_pascalcase" + expr: "bool('False')" + value: { bool_value: false } + } + test { + name: "string_true_badcase" + expr: "bool('TrUe')" + eval_error { + errors { message: "Type conversion error" } + } + } + test { + name: "string_false_badcase" + expr: "bool('FaLsE')" + eval_error { + errors { message: "Type conversion error" } + } + } +} section { name: "identity" description: "Identity functions" @@ -535,7 +603,7 @@ section { } test { name: "bytes" - expr: "bytes(bytes('abc'))" + expr: "bytes(b'abc')" value: { bytes_value: "abc" } } test { diff --git a/tests/simple/testdata/proto3.textproto b/tests/simple/testdata/proto3.textproto index 3d98d8c..2d1fcbf 100644 --- a/tests/simple/testdata/proto3.textproto +++ b/tests/simple/testdata/proto3.textproto @@ -724,7 +724,7 @@ section { eval_error: { errors: { message: "unsupported field type" } } - } + } test { name: "list_value" container: "google.api.expr.test.v1.proto3"