Skip to content

Conversation

@danelson
Copy link
Contributor

@danelson danelson commented Dec 9, 2025

Description

Adds a Bool function. This function can be used to convert values to booleans.

- set(attributes["my.bool"], Bool("true"))

Link to tracking issue

Closes #44770

Testing

Added new unit and e2e tests.

Documentation

Added function doc.

},
{
name: "empty string",
value: "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be false? Like in Python

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great question, I think I'd stick to the strconv.ParseBool approach (used by ottl.BoolLikeGetter) and continue to treat empty strings as errors instead of false. It's also consistent with other type conversions we support such as Int and Double, that also relies on the strconv package for that.

},
{
name: "some struct",
value: struct{}{},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test an empty slice?

Copy link
Contributor

@edmocosta edmocosta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this @danelson, it looks good, just a few suggestions.

value: "FaLsE",
expected: nil,
err: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
},
},
{
name: "string t",
value: "t",
expected: true,
},
{
name: "string f",
value: "f",
expected: false,
},
{
name: "string T",
value: "T",
expected: true,
},
{
name: "string F",
value: "F",
expected: false,
},

Those are also valid string values.


The `Bool` Converter converts the `value` to a bool type.

The returned type is bool.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The returned type is bool.
The returned type is `bool`.


The returned type is bool.

The input `value` types:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The input `value` types:
The accepted input `value` types are:

| `"1"` | `true` |
| `"0"` | `false` |

If `value` is another type or parsing failed nil is always returned.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If `value` is another type or parsing failed nil is always returned.
If `value` is another type or parsing failed, `nil` is always returned.

Comment on lines +574 to +588
- bool. Keeps value as is.
- float64. Zero is false, any non-zero value is true.
- int64. Zero is false, any non-zero value is true.
- string. Tries to parse an bool from string. If it fails then nil will be returned. The following are some valid inputs that are parsed as true or false:

| Input | Output |
| --- | --- |
| `"true"` | `true` |
| `"false"` | `false` |
| `"True"` | `true` |
| `"False"` | `false` |
| `"TRUE"` | `true` |
| `"FALSE"` | `false` |
| `"1"` | `true` |
| `"0"` | `false` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- bool. Keeps value as is.
- float64. Zero is false, any non-zero value is true.
- int64. Zero is false, any non-zero value is true.
- string. Tries to parse an bool from string. If it fails then nil will be returned. The following are some valid inputs that are parsed as true or false:
| Input | Output |
| --- | --- |
| `"true"` | `true` |
| `"false"` | `false` |
| `"True"` | `true` |
| `"False"` | `false` |
| `"TRUE"` | `true` |
| `"FALSE"` | `false` |
| `"1"` | `true` |
| `"0"` | `false` |
- `bool`: Returns the value without changes.
- `float64`: Returns `true` if non-zero, otherwise `false`.
- `int64`: Returns `true` if non-zero, otherwise `false`.
- `string`: Tries to parse a boolean from the string. It returns `true` for "1", "t", "T", "true", "TRUE", or "True"; returns `false` for "0", "f", "F", "false", "FALSE" or "False".
- `nil`: Returns `nil`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[pkg/ottl] Support string to boolean conversion.

4 participants