From 8e9a07f36cdc804c66f9fd265e31c2506893b444 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 21 Oct 2023 21:56:23 +0800 Subject: [PATCH 01/10] Add BoolGetter and BoolLikeGetter for IsBool 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. --- pkg/ottl/expression.go | 93 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/pkg/ottl/expression.go b/pkg/ottl/expression.go index 26da44c6c073..b52b134951c6 100644 --- a/pkg/ottl/expression.go +++ b/pkg/ottl/expression.go @@ -247,6 +247,41 @@ func (g StandardFloatGetter[K]) Get(ctx context.Context, tCtx K) (float64, error } } +// BoolGetter is a Getter that must return a bool. +type BoolGetter[K any] interface { + // Get retrieves a bool value. + Get(ctx context.Context, tCtx K) (bool, error) +} + +// StandardBoolGetter is a basic implementation of BoolGetter +type StandardBoolGetter[K any] struct { + Getter func(ctx context.Context, tCtx K) (interface{}, error) +} + +// Get retrieves a bool value. +// If the value is not a bool a new TypeError is returned. +// If there is an error getting the value it will be returned. +func (g StandardBoolGetter[K]) Get(ctx context.Context, tCtx K) (bool, error) { + val, err := g.Getter(ctx, tCtx) + if err != nil { + return false, fmt.Errorf("error getting value in %T: %w", g, err) + } + if val == nil { + return false, TypeError("expected bool but got nil") + } + switch v := val.(type) { + case bool: + return v, nil + case pcommon.Value: + if v.Type() == pcommon.ValueTypeBool { + return v.Bool(), nil + } + return false, TypeError(fmt.Sprintf("expected bool but got %v", v.Type())) + default: + return false, TypeError(fmt.Sprintf("expected bool but got %T", val)) + } +} + // FunctionGetter uses a function factory to return an instantiated function as an Expr. type FunctionGetter[K any] interface { Get(args Arguments) (Expr[K], error) @@ -507,6 +542,64 @@ func (g StandardIntLikeGetter[K]) Get(ctx context.Context, tCtx K) (*int64, erro return &result, nil } +// BoolLikeGetter is a Getter that returns a bool by converting the underlying value to a bool if necessary. +type BoolLikeGetter[K any] interface { + // Get retrieves a bool value. + // Unlike `BoolGetter`, the expectation is that the underlying value is converted to a bool if possible. + // If the value cannot be converted to a bool, nil and an error are returned. + // If the value is nil, nil is returned without an error. + Get(ctx context.Context, tCtx K) (*bool, error) +} + +type StandardBoolLikeGetter[K any] struct { + Getter func(ctx context.Context, tCtx K) (interface{}, error) +} + +func (g StandardBoolLikeGetter[K]) Get(ctx context.Context, tCtx K) (*bool, error) { + val, err := g.Getter(ctx, tCtx) + if err != nil { + return nil, fmt.Errorf("error getting value in %T: %w", g, err) + } + if val == nil { + return nil, nil + } + var result bool + switch v := val.(type) { + case bool: + result = v + case int: + result = v != 0 + case int64: + result = v != 0 + case string: + result, err = strconv.ParseBool(v) + if err != nil { + return nil, err + } + case float64: + result = v != 0.0 + case pcommon.Value: + switch v.Type() { + case pcommon.ValueTypeBool: + result = v.Bool() + case pcommon.ValueTypeInt: + result = v.Int() != 0 + case pcommon.ValueTypeStr: + result, err = strconv.ParseBool(v.Str()) + if err != nil { + return nil, err + } + case pcommon.ValueTypeDouble: + result = v.Double() != 0.0 + default: + return nil, TypeError(fmt.Sprintf("unsupported value type: %v", v.Type())) + } + default: + return nil, TypeError(fmt.Sprintf("unsupported type: %T", val)) + } + return &result, nil +} + func (p *Parser[K]) newGetter(val value) (Getter[K], error) { if val.IsNil != nil && *val.IsNil { return &literal[K]{value: nil}, nil From c7e72a223e91ede3a3870b8116e80274a41c99e2 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 21 Oct 2023 22:01:20 +0800 Subject: [PATCH 02/10] Add unit tests for BoolGetter 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. --- pkg/ottl/expression_test.go | 232 ++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) diff --git a/pkg/ottl/expression_test.go b/pkg/ottl/expression_test.go index d99c44f92fb9..55286118a20c 100644 --- a/pkg/ottl/expression_test.go +++ b/pkg/ottl/expression_test.go @@ -1449,6 +1449,238 @@ func Test_StandardIntLikeGetter_WrappedError(t *testing.T) { assert.False(t, ok) } +func Test_StandardBoolGetter(t *testing.T) { + tests := []struct { + name string + getter StandardBoolGetter[interface{}] + want bool + valid bool + expectedErrorMsg string + }{ + { + name: "primitive bool type", + getter: StandardBoolGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return true, nil + }, + }, + want: true, + valid: true, + }, + { + name: "ValueTypeBool type", + getter: StandardBoolGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return pcommon.NewValueBool(true), nil + }, + }, + want: true, + valid: true, + }, + { + name: "Incorrect type", + getter: StandardBoolGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return 1, nil + }, + }, + valid: false, + expectedErrorMsg: "expected bool but got int", + }, + { + name: "nil", + getter: StandardBoolGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return nil, nil + }, + }, + valid: false, + expectedErrorMsg: "expected bool but got nil", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + val, err := tt.getter.Get(context.Background(), nil) + if tt.valid { + assert.NoError(t, err) + assert.Equal(t, tt.want, val) + } else { + assert.IsType(t, TypeError(""), err) + assert.EqualError(t, err, tt.expectedErrorMsg) + } + }) + } +} + +// nolint:errorlint +func Test_StandardBoolGetter_WrappedError(t *testing.T) { + getter := StandardBoolGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return nil, TypeError("") + }, + } + _, err := getter.Get(context.Background(), nil) + assert.Error(t, err) + _, ok := err.(TypeError) + assert.False(t, ok) +} + +func Test_StandardBoolLikeGetter(t *testing.T) { + tests := []struct { + name string + getter BoolLikeGetter[interface{}] + want interface{} + valid bool + expectedErrorMsg string + }{ + { + name: "string type true", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "true", nil + }, + }, + want: true, + valid: true, + }, + { + name: "string type false", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "false", nil + }, + }, + want: false, + valid: true, + }, + { + name: "int type", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return 0, nil + }, + }, + want: false, + valid: true, + }, + { + name: "float64 type", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return float64(0.0), nil + }, + }, + want: false, + valid: true, + }, + { + name: "pcommon.value type int", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + v := pcommon.NewValueInt(int64(0)) + return v, nil + }, + }, + want: false, + valid: true, + }, + { + name: "pcommon.value type string", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + v := pcommon.NewValueStr("false") + return v, nil + }, + }, + want: false, + valid: true, + }, + { + name: "pcommon.value type bool", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + v := pcommon.NewValueBool(true) + return v, nil + }, + }, + want: true, + valid: true, + }, + { + name: "pcommon.value type double", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + v := pcommon.NewValueDouble(float64(0.0)) + return v, nil + }, + }, + want: false, + valid: true, + }, + { + name: "nil", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return nil, nil + }, + }, + want: nil, + valid: true, + }, + { + name: "invalid type", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return []byte{}, nil + }, + }, + valid: false, + expectedErrorMsg: "unsupported type: []uint8", + }, + { + name: "invalid pcommon.value type", + getter: StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + v := pcommon.NewValueMap() + return v, nil + }, + }, + valid: false, + expectedErrorMsg: "unsupported value type: Map", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + val, err := tt.getter.Get(context.Background(), nil) + if tt.valid { + assert.NoError(t, err) + if tt.want == nil { + assert.Nil(t, val) + } else { + assert.Equal(t, tt.want, *val) + } + } else { + assert.IsType(t, TypeError(""), err) + assert.EqualError(t, err, tt.expectedErrorMsg) + } + }) + } +} + +func Test_StandardBoolLikeGetter_WrappedError(t *testing.T) { + getter := StandardBoolLikeGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return nil, TypeError("") + }, + } + _, err := getter.Get(context.Background(), nil) + assert.Error(t, err) + _, ok := err.(TypeError) + assert.False(t, ok) +} + func Test_StandardPMapGetter(t *testing.T) { tests := []struct { name string From 409a117695a24bb91d77b03395ccc8c1460f2a7f Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 21 Oct 2023 22:11:04 +0800 Subject: [PATCH 03/10] Add BoolGetter into supported type list This commit updates pkg/ottl/README.md to include BoolGetter and BoolLikeGetter in the list of supported single-value parameters for OTTL functions. --- pkg/ottl/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/ottl/README.md b/pkg/ottl/README.md index 82d97db70495..49aca8a48a08 100644 --- a/pkg/ottl/README.md +++ b/pkg/ottl/README.md @@ -77,6 +77,8 @@ The following types are supported for single-value parameters in OTTL functions: - `StringLikeGetter` - `IntGetter` - `IntLikeGetter` +- `BoolGetter` +- `BoolLikeGetter` - `Enum` - `string` - `float64` From 2c4135b154e0423b282c17be641b2f35e4fd1809 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 21 Oct 2023 22:18:36 +0800 Subject: [PATCH 04/10] Implement IsBool converter 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 --- pkg/ottl/ottlfuncs/func_is_bool.go | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pkg/ottl/ottlfuncs/func_is_bool.go diff --git a/pkg/ottl/ottlfuncs/func_is_bool.go b/pkg/ottl/ottlfuncs/func_is_bool.go new file mode 100644 index 000000000000..b62866e0ab62 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_is_bool.go @@ -0,0 +1,45 @@ +// 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 + } + } +} From 703b390e23e458fa8b50a0c285d4aab08052e1e3 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 21 Oct 2023 22:25:40 +0800 Subject: [PATCH 05/10] Add unit tests for IsBool converter 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 --- pkg/ottl/ottlfuncs/func_is_bool_test.go | 74 +++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 pkg/ottl/ottlfuncs/func_is_bool_test.go diff --git a/pkg/ottl/ottlfuncs/func_is_bool_test.go b/pkg/ottl/ottlfuncs/func_is_bool_test.go new file mode 100644 index 000000000000..780ab61139d5 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_is_bool_test.go @@ -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(false), + 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) +} From 15db093025f101585e51394ed75fb3cd1cda6108 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 21 Oct 2023 22:31:06 +0800 Subject: [PATCH 06/10] Register IsBool converter 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. --- pkg/ottl/ottlfuncs/functions.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/ottl/ottlfuncs/functions.go b/pkg/ottl/ottlfuncs/functions.go index e43507192392..e892135c45e8 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -42,6 +42,7 @@ func converters[K any]() []ottl.Factory[K] { NewFnvFactory[K](), NewHoursFactory[K](), NewIntFactory[K](), + NewIsBoolFactory[K](), NewIsMapFactory[K](), NewIsMatchFactory[K](), NewIsStringFactory[K](), From f8720c5a6e10a1a207e843f8c18f6347d968d3be Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sat, 21 Oct 2023 22:32:50 +0800 Subject: [PATCH 07/10] Update doc for IsBool converter 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. --- pkg/ottl/ottlfuncs/README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index ac5d7dff851f..c975b6aad5b6 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -297,6 +297,7 @@ Available Converters: - [Double](#double) - [Duration](#duration) - [Int](#int) +- [IsBool](#isbool) - [IsMap](#ismap) - [IsMatch](#ismatch) - [IsString](#isstring) @@ -480,6 +481,32 @@ Examples: - `Int("2.0")` +### IsBool + +`IsBool(value)` + +The `IsBool` Converter evaluates whether the given `value` is a boolean or not. + +Specifically, it will return `true` if the provided `value` is one of the following: + +1. A Go's native `bool` type. +2. A `pcommon.ValueTypeBool`. + +Otherwise, it will return `false`. + +Examples: + +- `IsBool(false)` + + +- `IsBool(pcommon.NewValueBool(false))` + + +- `IsBool(42)` + + +- `IsBool(attributes["any key"])` + ### IsMap `IsMap(value)` From f7a3680f23e509c3c52e6f777b8ef0508791add6 Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Fri, 10 Nov 2023 23:43:00 +0800 Subject: [PATCH 08/10] Fix linting error in `expression_test.go` Add nolint:error lint for function `Test_StandardBoolLikeGetter_WrappedError` in `expression_test.go`. --- pkg/ottl/expression_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/ottl/expression_test.go b/pkg/ottl/expression_test.go index 55286118a20c..54b956220b23 100644 --- a/pkg/ottl/expression_test.go +++ b/pkg/ottl/expression_test.go @@ -1669,6 +1669,7 @@ func Test_StandardBoolLikeGetter(t *testing.T) { } } +// nolint:errorlint func Test_StandardBoolLikeGetter_WrappedError(t *testing.T) { getter := StandardBoolLikeGetter[interface{}]{ Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { From cb8018630fa2d3fb5fdb71b4de0d26be56ee6e9e Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Fri, 10 Nov 2023 23:52:06 +0800 Subject: [PATCH 09/10] Add changelog --- .chloggen/issue-27897-IsBool-Converter.yaml | 27 +++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 .chloggen/issue-27897-IsBool-Converter.yaml diff --git a/.chloggen/issue-27897-IsBool-Converter.yaml b/.chloggen/issue-27897-IsBool-Converter.yaml new file mode 100755 index 000000000000..e0ac3f0687d7 --- /dev/null +++ b/.chloggen/issue-27897-IsBool-Converter.yaml @@ -0,0 +1,27 @@ +# 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: Add IsBool function into 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. +subtext: + +# 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] From 6cdf73959f1e223e5dc8f59992049c2624166d8e Mon Sep 17 00:00:00 2001 From: Dennis Liu Date: Sun, 12 Nov 2023 12:39:03 +0800 Subject: [PATCH 10/10] Replace interface{} to any Replace interface{} to any according to PR 29072. --- pkg/ottl/expression.go | 4 +- pkg/ottl/expression_test.go | 74 ++++++++++++------------- pkg/ottl/ottlfuncs/func_is_bool.go | 2 +- pkg/ottl/ottlfuncs/func_is_bool_test.go | 6 +- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/pkg/ottl/expression.go b/pkg/ottl/expression.go index b52b134951c6..1c5ac3f69b17 100644 --- a/pkg/ottl/expression.go +++ b/pkg/ottl/expression.go @@ -255,7 +255,7 @@ type BoolGetter[K any] interface { // StandardBoolGetter is a basic implementation of BoolGetter type StandardBoolGetter[K any] struct { - Getter func(ctx context.Context, tCtx K) (interface{}, error) + Getter func(ctx context.Context, tCtx K) (any, error) } // Get retrieves a bool value. @@ -552,7 +552,7 @@ type BoolLikeGetter[K any] interface { } type StandardBoolLikeGetter[K any] struct { - Getter func(ctx context.Context, tCtx K) (interface{}, error) + Getter func(ctx context.Context, tCtx K) (any, error) } func (g StandardBoolLikeGetter[K]) Get(ctx context.Context, tCtx K) (*bool, error) { diff --git a/pkg/ottl/expression_test.go b/pkg/ottl/expression_test.go index 54b956220b23..478d56defead 100644 --- a/pkg/ottl/expression_test.go +++ b/pkg/ottl/expression_test.go @@ -1452,15 +1452,15 @@ func Test_StandardIntLikeGetter_WrappedError(t *testing.T) { func Test_StandardBoolGetter(t *testing.T) { tests := []struct { name string - getter StandardBoolGetter[interface{}] + getter StandardBoolGetter[any] want bool valid bool expectedErrorMsg string }{ { name: "primitive bool type", - getter: StandardBoolGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return true, nil }, }, @@ -1469,8 +1469,8 @@ func Test_StandardBoolGetter(t *testing.T) { }, { name: "ValueTypeBool type", - getter: StandardBoolGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return pcommon.NewValueBool(true), nil }, }, @@ -1479,8 +1479,8 @@ func Test_StandardBoolGetter(t *testing.T) { }, { name: "Incorrect type", - getter: StandardBoolGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return 1, nil }, }, @@ -1489,8 +1489,8 @@ func Test_StandardBoolGetter(t *testing.T) { }, { name: "nil", - getter: StandardBoolGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return nil, nil }, }, @@ -1515,8 +1515,8 @@ func Test_StandardBoolGetter(t *testing.T) { // nolint:errorlint func Test_StandardBoolGetter_WrappedError(t *testing.T) { - getter := StandardBoolGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter := StandardBoolGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return nil, TypeError("") }, } @@ -1529,15 +1529,15 @@ func Test_StandardBoolGetter_WrappedError(t *testing.T) { func Test_StandardBoolLikeGetter(t *testing.T) { tests := []struct { name string - getter BoolLikeGetter[interface{}] - want interface{} + getter BoolLikeGetter[any] + want any valid bool expectedErrorMsg string }{ { name: "string type true", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return "true", nil }, }, @@ -1546,8 +1546,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "string type false", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return "false", nil }, }, @@ -1556,8 +1556,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "int type", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return 0, nil }, }, @@ -1566,8 +1566,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "float64 type", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return float64(0.0), nil }, }, @@ -1576,8 +1576,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "pcommon.value type int", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { v := pcommon.NewValueInt(int64(0)) return v, nil }, @@ -1587,8 +1587,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "pcommon.value type string", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { v := pcommon.NewValueStr("false") return v, nil }, @@ -1598,8 +1598,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "pcommon.value type bool", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { v := pcommon.NewValueBool(true) return v, nil }, @@ -1609,8 +1609,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "pcommon.value type double", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { v := pcommon.NewValueDouble(float64(0.0)) return v, nil }, @@ -1620,8 +1620,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "nil", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return nil, nil }, }, @@ -1630,8 +1630,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "invalid type", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return []byte{}, nil }, }, @@ -1640,8 +1640,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { }, { name: "invalid pcommon.value type", - getter: StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter: StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { v := pcommon.NewValueMap() return v, nil }, @@ -1671,8 +1671,8 @@ func Test_StandardBoolLikeGetter(t *testing.T) { // nolint:errorlint func Test_StandardBoolLikeGetter_WrappedError(t *testing.T) { - getter := StandardBoolLikeGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + getter := StandardBoolLikeGetter[any]{ + Getter: func(ctx context.Context, tCtx any) (any, error) { return nil, TypeError("") }, } diff --git a/pkg/ottl/ottlfuncs/func_is_bool.go b/pkg/ottl/ottlfuncs/func_is_bool.go index b62866e0ab62..b2845e919c33 100644 --- a/pkg/ottl/ottlfuncs/func_is_bool.go +++ b/pkg/ottl/ottlfuncs/func_is_bool.go @@ -30,7 +30,7 @@ func createIsBoolFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) ( // nolint:errorlint func isBool[K any](target ottl.BoolGetter[K]) ottl.ExprFunc[K] { - return func(ctx context.Context, tCtx K) (interface{}, error) { + return func(ctx context.Context, tCtx K) (any, error) { _, err := target.Get(ctx, tCtx) // Use type assertion, because we don't want to check wrapped errors switch err.(type) { diff --git a/pkg/ottl/ottlfuncs/func_is_bool_test.go b/pkg/ottl/ottlfuncs/func_is_bool_test.go index 780ab61139d5..4cb780385923 100644 --- a/pkg/ottl/ottlfuncs/func_is_bool_test.go +++ b/pkg/ottl/ottlfuncs/func_is_bool_test.go @@ -16,7 +16,7 @@ import ( func Test_IsBool(t *testing.T) { tests := []struct { name string - value interface{} + value any expected bool }{ { @@ -48,7 +48,7 @@ func Test_IsBool(t *testing.T) { 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) { + Getter: func(context.Context, any) (any, error) { return tt.value, nil }, }) @@ -62,7 +62,7 @@ func Test_IsBool(t *testing.T) { // nolint:errorlint func Test_IsBool_Error(t *testing.T) { exprFunc := isBool[any](&ottl.StandardBoolGetter[any]{ - Getter: func(context.Context, interface{}) (interface{}, error) { + Getter: func(context.Context, any) (any, error) { return nil, ottl.TypeError("") }, })