Skip to content

Commit

Permalink
[pkg/ottl] Handle nil val in IsMatch (#17572)
Browse files Browse the repository at this point in the history
* Handle nil val in IsMatch

* add changelog entry
  • Loading branch information
TylerHelmuth committed Jan 21, 2023
1 parent 7e65c2e commit e797d0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
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
- `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) {
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

0 comments on commit e797d0a

Please sign in to comment.