-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement isBool and add to list of supported functions
Also updates the changelog
- Loading branch information
Lennart Elsen
committed
Nov 9, 2023
1 parent
eb320a1
commit e317204
Showing
6 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# 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: The IsBool function is now available to OTTL | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [27897] | ||
|
||
# (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. | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type IsBoolArguments[K any] struct { | ||
Target ottl.BoolGetter[K] | ||
} | ||
|
||
func NewIsBoolFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("IsBool", &IsBoolArguments[K]{}, createIsBoolFunction[K]) | ||
} | ||
|
||
func createIsBoolFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*IsBoolArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("IsBoolFactory args must be of type *IsBoolArguments[K]") | ||
} | ||
|
||
return isBool(args.Target), nil | ||
} | ||
|
||
// nolint:errorlint | ||
func isBool[K any](target ottl.BoolGetter[K]) ottl.ExprFunc[K] { | ||
return func(ctx context.Context, tCtx K) (interface{}, error) { | ||
_, err := target.Get(ctx, tCtx) | ||
|
||
// Use type assertion because we don't want to check wrapped errors | ||
switch err.(type) { | ||
case ottl.TypeError: | ||
return false, nil | ||
case nil: | ||
return true, nil | ||
default: | ||
return false, err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_IsBool(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value interface{} | ||
expected bool | ||
}{ | ||
{ | ||
name: "bool", | ||
value: true, | ||
expected: true, | ||
}, | ||
{ | ||
name: "ValueTypeBool", | ||
value: pcommon.NewValueBool(true), | ||
expected: true, | ||
}, | ||
{ | ||
name: "not bool", | ||
value: 1, | ||
expected: false, | ||
}, | ||
{ | ||
name: "ValueTypeSlice", | ||
value: pcommon.NewValueSlice(), | ||
expected: false, | ||
}, | ||
{ | ||
name: "nil", | ||
value: nil, | ||
expected: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc := isBool[any](&ottl.StandardBoolGetter[any]{ | ||
Getter: func(context.Context, interface{}) (interface{}, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
result, err := exprFunc(context.Background(), nil) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
// nolint:errorlint | ||
func Test_IsBool_Error(t *testing.T) { | ||
exprFunc := isBool[any](&ottl.StandardBoolGetter[any]{ | ||
Getter: func(context.Context, interface{}) (interface{}, error) { | ||
return nil, ottl.TypeError("") | ||
}, | ||
}) | ||
result, err := exprFunc(context.Background(), nil) | ||
assert.Equal(t, false, result) | ||
assert.Error(t, err) | ||
_, ok := err.(ottl.TypeError) | ||
assert.False(t, ok) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters