Skip to content

Commit

Permalink
Add type conversion function for string -> bool to standard definitio…
Browse files Browse the repository at this point in the history
…ns (#372)
  • Loading branch information
l46kok authored Aug 12, 2024
1 parent 0789389 commit 41c2cd4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
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

0 comments on commit 41c2cd4

Please sign in to comment.