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 Contains Converter #35482

Conversation

lkwronski
Copy link
Contributor

Description: Adds a new contains function to check if slice contains item.

Link to tracking Issue: Fixes #30420

Testing: Added unit test

Documentation: Added documentation to Readme

I reopened the PR because my commissions were closed, sorry for the long response time. #33078

@github-actions github-actions bot requested a review from kentquirk September 28, 2024 10:04
@djaglowski djaglowski changed the title Add contains function [pkg/ottl] Add Contains Converter Oct 1, 2024
{
name: `set attribute when tag "staging" is in tags attributes slice using Contains`,
statement: []string{
`set(attributes["tags"], ["staging", "hello", "world", "work"])`,
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me why we need to support a slice of statements in this test. Isn't this first statement just a form of setup? Could it be moved into constructLogTransformContext in a way that doesn't interfere with the other tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, instead of adding new attributes in the constructLogTransformContext function, I simply reuse the already created slice, so the effect is the same, but I don't have to add new attributes to the current code.

pkg/ottl/ottlfuncs/func_contains_test.go Outdated Show resolved Hide resolved
pkg/ottl/ottlfuncs/func_contains_test.go Outdated Show resolved Hide resolved
Comment on lines +44 to +47
val := slice.At(i).AsRaw()
if val == item {
return true, nil
}
Copy link
Member

Choose a reason for hiding this comment

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

Can this return incomparable types? (e.g. slices or maps)

Can we handle the various types explicitly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it can return incomparable types, let me think how to do it explicitly. Do you think about such solution?

		for i := 0; i < slice.Len(); i++ {
			val := slice.At(i).AsRaw()

			switch val.(type) {
			case string:
				if s, ok := item.(string); ok && s == val {
					return true, nil
				}
			}
		}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can check if the value is a complex type like a slice or map and perform some kind of deep equality check where necessary. For any primitive type, we can just compare them directly; I don't think we have to go as far as casting like this since two different primitive types will trivially fail the equality check.

@lkwronski lkwronski force-pushed the lkwronski.issue-30420-contains branch from 417027d to e398730 Compare October 1, 2024 16:03
@lkwronski lkwronski force-pushed the lkwronski.issue-30420-contains branch from e398730 to f928122 Compare October 1, 2024 16:05
Copy link
Contributor

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

Copy link
Contributor

github-actions bot commented Nov 2, 2024

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 2, 2024
Copy link
Contributor

Closed as inactive. Feel free to reopen if this PR is still being worked on.

@github-actions github-actions bot closed this Nov 16, 2024
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.

ottl: Efficiently test if a value is in a list or map
4 participants