Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type conversion function for string -> bool to standard definitions #372

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions doc/langdef.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`.
Expand Down Expand Up @@ -1835,7 +1835,7 @@ See [cel-go/issues/9](https://github.com/google/cel-go/issues/9).
</td>
</tr>
<tr>
<th rowspan="2">
<th rowspan="3">
bool
</th>
<td>
Expand All @@ -1853,6 +1853,14 @@ See [cel-go/issues/9](https://github.com/google/cel-go/issues/9).
identity
</td>
</tr>
<tr>
<td>
(string) -> bool
</td>
<td>
type conversion
</td>
</tr>
<tr>
<th rowspan="3">
bytes
Expand Down
70 changes: 69 additions & 1 deletion tests/simple/testdata/conversions.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -535,7 +603,7 @@ section {
}
test {
name: "bytes"
expr: "bytes(bytes('abc'))"
expr: "bytes(b'abc')"
value: { bytes_value: "abc" }
}
test {
Expand Down
2 changes: 1 addition & 1 deletion tests/simple/testdata/proto3.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ section {
eval_error: {
errors: { message: "unsupported field type" }
}
}
}
test {
name: "list_value"
container: "google.api.expr.test.v1.proto3"
Expand Down