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] Handle nil val in IsMatch #17572

Merged
merged 2 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/ottl-fix-ismatch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix issue where IsMatch returned an error if the target val was nil

# One or more tracking issues related to the change
issues: [17572]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
- Affected components
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the usual approach with changes to libraries like this? Should each impacted component get its own changelog entry? I'm good with doing it like this, but could also see the other side of things.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we have an established pattern. I did this once before in the 0.65.0 release.

- `filterprocessor`
- `routingprocessor`
- `transformprocessor`
3 changes: 3 additions & 0 deletions pkg/ottl/ottlfuncs/func_is_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func IsMatch[K any](target ottl.Getter[K], pattern string) (ottl.ExprFunc[K], er
if err != nil {
return nil, err
}
if val == nil {
return false, nil
}

switch v := val.(type) {
Copy link
Member

Choose a reason for hiding this comment

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

Not related to this PR, but keep in mind that this path is a slippery path, and will for sure hurt us. The fact that you accept multiple types instead of asking users to call this as IsMatch(..., string(value)) and enforce always having string here. The reason is because I feel like this will get duplicate in multiple places and will diverge.

You can at least start by adding "String()" func and call it on the value to avoid duplicate code.

Copy link
Member Author

@TylerHelmuth TylerHelmuth Jan 21, 2023

Choose a reason for hiding this comment

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

I don't think we want to force users to use a String() Converter function every time they use IsMatch so I like that the function handles conversion for you. Conversion is only happening for the target, not the pattern.

If your concern is only the potential duplication of the type switch statement for strings, I can pull that out into a helper function for future reuse.

Copy link
Member

Choose a reason for hiding this comment

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

Let's start with avoiding potential duplication, I bet we already duplicate this.

case string:
Expand Down
10 changes: 10 additions & 0 deletions pkg/ottl/ottlfuncs/func_is_match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func Test_isMatch(t *testing.T) {
pattern: `test`,
expected: true,
},
{
name: "nil target",
target: &ottl.StandardGetSetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return nil, nil
},
},
pattern: "impossible to match",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down