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

[pkg/ottl] Add BoolGetter and IsBool converter #27900

Merged

Conversation

Dennis40816
Copy link
Contributor

Description:
This PR focuses on adding a new feature to the pkg/ottl package—namely, the IsBool converter, along with its prerequisites, BoolGetter and BoolLikeGetter. The series of commits implement the core logic, unit tests, and documentation to ensure that the IsBool converter is seamlessly integrated into the OTTL framework. By using this feature, users can conveniently determine whether a given value is a boolean or not.

Link to tracking Issue:
#27897

Testing:

  • A comprehensive set of unit tests were added to validate the functionality of the IsBool converter. The tests cover native boolean values, pcommon.ValueTypeBool objects, and incorrect types such as integers and slices.
  • Tests for BoolGetter and BoolLikeGetter were also included to verify their behavior and error handling mechanisms. Detailed test implementations can be found in pkg/ottl/expression_test.go.

Documentation:

  • Updated README.md in the pkg/ottl/ottlfuncs directory to include detailed descriptions, usage guidelines, and examples for the IsBool converter.
  • The README file also reflects the addition of BoolGetter and BoolLikeGetter to the list of supported types for single-value parameters in OTTL functions.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 21, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@Dennis40816
Copy link
Contributor Author

I'm not sure if I have misunderstood your issue, but I have written the IsBool converter referencing the IsString converter. Please let me know if anything goes wrong. I appreciate your time and effort in reviewing this. Thank you.

Copy link
Contributor

github-actions bot commented Nov 5, 2023

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Nov 5, 2023
Copy link
Member

@TylerHelmuth TylerHelmuth left a comment

Choose a reason for hiding this comment

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

@Dennis40816 thank you for your contribution, sorry I took so long to review this.

Copy link
Member

@TylerHelmuth TylerHelmuth left a comment

Choose a reason for hiding this comment

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

@Dennis40816 please add a changelog.

@TylerHelmuth TylerHelmuth added the ready to merge Code review completed; ready to merge by maintainers label Nov 11, 2023
@Dennis40816
Copy link
Contributor Author

Dennis40816 commented Nov 12, 2023

I'll rebase this branch to the latest main due to #29072.

Introduces two new types, BoolGetter and BoolLikeGetter, as
prerequisites for the upcoming IsBool Converter feature.

BoolGetter is designed to retrieve a bool value directly and will
return a TypeError if the type doesn't match. It only accepts:

  1. Native bool
  2. pcommon.ValueTypeBool

BoolLikeGetter will attempt to convert the underlying value to a bool,
if possible, and return an error for incompatible types. It accepts
bool, int, int64, string, and float64, as well as corresponding
pcommon.ValueTypes. If the received pointer is nil, it will return nil.
This commit focuses on enhancing the test coverage for boolean type
getter methods.

Four new test functions have been added:

- `Test_StandardBoolGetter`: Validates the behavior of standard Boolean
  getters.
- `Test_StandardBoolGetter_WrappedError`: Ensures that standard
  Boolean getters correctly wrap and return errors.
- `Test_StandardBoolLikeGetter`: Checks if object-based getters
  correctly convert to Boolean values.
- `Test_StandardBoolLikeGetter_WrappedError`: Validates that errors from
  Bool-like getters are correctly wrapped and returned.
This commit updates pkg/ottl/README.md to include BoolGetter and
BoolLikeGetter in the list of supported single-value parameters for OTTL
functions.
This commit adds a new file, `func_is_bool.go`, to the
`pkg/ottl/ottlfuncs` directory. This file houses the implementation of
the `IsBool` function, designed to validate the type of `BoolGetter`
objects.

The `IsBool` function is crafted to serve as an integral part of OTTL's expression evaluation mechanism.

The function will return `true` for the following two types:
- Native boolean type
- pcommon.ValueTypeBool
This commit introduces a new test file, func_is_bool_test.go, within the
pkg/ottl/ottlfuncs directory. The test file contains comprehensive unit
tests for the IsBool converter, ensuring its accuracy in type validation
for various input value scenarios.

Specifically, the tests cover:

- Native boolean values
- pcommon.ValueTypeBool objects
- Incorrect types such as integers and slices
- Error handling for type errors
This commit integrates the IsBool converter into the OTTL function
registry by modifying the `functions.go` file. Specifically, the
function `NewIsBoolFactory[K]()` has been included in the list of
available converters.

The update ensures that the IsBool converter is now available for use
within the existing OTTL framework.
This commit updates the README.md file in the `pkg/ottl/ottlfuncs`
directory to include documentation for the newly added IsBool converter.
Detailed explanations, usage guidelines, and examples have been provided
for the benefit of users.

The documentation serves as a comprehensive guide for utilizing the
IsBool converter within the OTTL ecosystem.
Add nolint:error lint for function
`Test_StandardBoolLikeGetter_WrappedError` in `expression_test.go`.
Replace interface{} to any according to PR 29072.
@TylerHelmuth TylerHelmuth merged commit 78da59a into open-telemetry:main Nov 13, 2023
83 checks passed
@github-actions github-actions bot added this to the next release milestone Nov 13, 2023
RoryCrispin pushed a commit to ClickHouse/opentelemetry-collector-contrib that referenced this pull request Nov 24, 2023
**Description:**
This PR focuses on adding a new feature to the pkg/ottl package—namely,
the `IsBool` converter, along with its prerequisites, `BoolGetter` and
`BoolLikeGetter`. The series of commits implement the core logic, unit
tests, and documentation to ensure that the `IsBool` converter is
seamlessly integrated into the OTTL framework. By using this feature,
users can conveniently determine whether a given value is a boolean or
not.

**Link to tracking Issue:** 
open-telemetry#27897

**Testing:**

- A comprehensive set of unit tests were added to validate the
functionality of the `IsBool` converter. The tests cover native boolean
values, `pcommon.ValueTypeBool` objects, and incorrect types such as
integers and slices.
- Tests for `BoolGetter` and `BoolLikeGetter` were also included to
verify their behavior and error handling mechanisms. Detailed test
implementations can be found in
[pkg/ottl/expression_test.go](https://github.com/Dennis40816/opentelemetry-collector-contrib/blob/0d1c2c0216f5647404573a6cfe66ebc0081a4167/pkg/ottl/expression_test.go#L1452).

**Documentation:**
- Updated README.md in the `pkg/ottl/ottlfuncs` directory to include
detailed descriptions, usage guidelines, and examples for the `IsBool`
converter.
- The README file also reflects the addition of `BoolGetter` and
`BoolLikeGetter` to the list of supported types for single-value
parameters in OTTL functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/ottl ready to merge Code review completed; ready to merge by maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants