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] use IntGetter argument for Substring function #25874

Merged
merged 6 commits into from
Aug 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
27 changes: 27 additions & 0 deletions .chloggen/ottl-substring-intgetter.yaml
Original file line number Diff line number Diff line change
@@ -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: breaking

# 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: use IntGetter argument for Substring function

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [25852]

# (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: [api]
31 changes: 19 additions & 12 deletions pkg/ottl/ottlfuncs/func_substring.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

type SubstringArguments[K any] struct {
Target ottl.StringGetter[K] `ottlarg:"0"`
Start int64 `ottlarg:"1"`
Length int64 `ottlarg:"2"`
Start ottl.IntGetter[K] `ottlarg:"1"`
Length ottl.IntGetter[K] `ottlarg:"2"`
}

func NewSubstringFactory[K any]() ottl.Factory[K] {
Expand All @@ -27,18 +27,25 @@ func createSubstringFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments
return nil, fmt.Errorf("SubstringFactory args must be of type *SubstringArguments[K]")
}

return substring(args.Target, args.Start, args.Length)
return substring(args.Target, args.Start, args.Length), nil
}

func substring[K any](target ottl.StringGetter[K], start int64, length int64) (ottl.ExprFunc[K], error) {
if start < 0 {
return nil, fmt.Errorf("invalid start for substring function, %d cannot be negative", start)
}
if length <= 0 {
return nil, fmt.Errorf("invalid length for substring function, %d cannot be negative or zero", length)
}

func substring[K any](target ottl.StringGetter[K], startGetter ottl.IntGetter[K], lengthGetter ottl.IntGetter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (interface{}, error) {
start, err := startGetter.Get(ctx, tCtx)
if err != nil {
return nil, err
}
if start < 0 {
return nil, fmt.Errorf("invalid start for substring function, %d cannot be negative", start)
}
length, err := lengthGetter.Get(ctx, tCtx)
if err != nil {
return nil, err
}
if length <= 0 {
return nil, fmt.Errorf("invalid length for substring function, %d cannot be negative or zero", length)
}
val, err := target.Get(ctx, tCtx)
if err != nil {
return nil, err
Expand All @@ -47,5 +54,5 @@ func substring[K any](target ottl.StringGetter[K], start int64, length int64) (o
return nil, fmt.Errorf("invalid range for substring function, %d cannot be greater than the length of target string(%d)", start+length, len(val))
}
return val[start : start+length], nil
}, nil
}
}
118 changes: 91 additions & 27 deletions pkg/ottl/ottlfuncs/func_substring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func Test_substring(t *testing.T) {
tests := []struct {
name string
target ottl.StringGetter[interface{}]
start int64
length int64
start ottl.IntGetter[interface{}]
length ottl.IntGetter[interface{}]
expected interface{}
}{
{
Expand All @@ -27,8 +27,16 @@ func Test_substring(t *testing.T) {
return "123456789", nil
},
},
start: 3,
length: 3,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(3), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(3), nil
},
},
expected: "456",
},
{
Expand All @@ -38,15 +46,22 @@ func Test_substring(t *testing.T) {
return "123456789", nil
},
},
start: 0,
length: 9,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(0), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(9), nil
},
},
expected: "123456789",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := substring(tt.target, tt.start, tt.length)
assert.NoError(t, err)
exprFunc := substring(tt.target, tt.start, tt.length)
result, err := exprFunc(nil, nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
Expand All @@ -58,8 +73,8 @@ func Test_substring_validation(t *testing.T) {
tests := []struct {
name string
target ottl.StringGetter[interface{}]
start int64
length int64
start ottl.IntGetter[interface{}]
length ottl.IntGetter[interface{}]
}{
{
name: "substring with result of empty string",
Expand All @@ -68,8 +83,16 @@ func Test_substring_validation(t *testing.T) {
return "123456789", nil
},
},
start: 0,
length: 0,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(0), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(0), nil
},
},
},
{
name: "substring with invalid start index",
Expand All @@ -78,14 +101,24 @@ func Test_substring_validation(t *testing.T) {
return "123456789", nil
},
},
start: -1,
length: 6,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(-1), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(6), nil
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := substring(tt.target, tt.start, tt.length)
exprFunc := substring(tt.target, tt.start, tt.length)
result, err := exprFunc(nil, nil)
assert.Error(t, err)
assert.Nil(t, result)
})
}
}
Expand All @@ -94,8 +127,8 @@ func Test_substring_error(t *testing.T) {
tests := []struct {
name string
target ottl.StringGetter[interface{}]
start int64
length int64
start ottl.IntGetter[interface{}]
length ottl.IntGetter[interface{}]
}{
{
name: "substring empty string",
Expand All @@ -104,8 +137,16 @@ func Test_substring_error(t *testing.T) {
return "", nil
},
},
start: 3,
length: 6,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(3), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(6), nil
},
},
},
{
name: "substring with invalid length index",
Expand All @@ -114,8 +155,16 @@ func Test_substring_error(t *testing.T) {
return "123456789", nil
},
},
start: 3,
length: 20,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(3), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(20), nil
},
},
},
{
name: "substring non-string",
Expand All @@ -124,8 +173,16 @@ func Test_substring_error(t *testing.T) {
return 123456789, nil
},
},
start: 3,
length: 6,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(3), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(6), nil
},
},
},
{
name: "substring nil string",
Expand All @@ -134,14 +191,21 @@ func Test_substring_error(t *testing.T) {
return nil, nil
},
},
start: 3,
length: 6,
start: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(3), nil
},
},
length: &ottl.StandardIntGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return int64(6), nil
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := substring(tt.target, tt.start, tt.length)
assert.NoError(t, err)
exprFunc := substring(tt.target, tt.start, tt.length)
result, err := exprFunc(nil, nil)
assert.Error(t, err)
assert.Equal(t, nil, result)
Expand Down