-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(ottl): add Bool function #44854
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
base: main
Are you sure you want to change the base?
Conversation
| }, | ||
| { | ||
| name: "empty string", | ||
| value: "", |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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{}{}, |
There was a problem hiding this comment.
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?
edmocosta
left a comment
There was a problem hiding this 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, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| }, | |
| }, | |
| { | |
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| The returned type is bool. | |
| The returned type is `bool`. |
|
|
||
| The returned type is bool. | ||
|
|
||
| The input `value` types: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| If `value` is another type or parsing failed nil is always returned. | |
| If `value` is another type or parsing failed, `nil` is always returned. |
| - 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` | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - 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`. |
Description
Adds a
Boolfunction. 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.