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

Add an optional argument to converters to support hashing #27235

Merged
merged 8 commits into from
Oct 11, 2023
32 changes: 32 additions & 0 deletions .chloggen/ottl-replace-pattern.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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 optional parameters to converters
rnishtala-sumo marked this conversation as resolved.
Show resolved Hide resolved

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

# (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: |
Hashing functions are passed as optional arguments to the following converters:
rnishtala-sumo marked this conversation as resolved.
Show resolved Hide resolved
- `replace_pattern`
- `replace_all_patterns`
- `replace_match`
- `replace_all_matches`

# 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]
12 changes: 6 additions & 6 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,21 @@ type FunctionGetter[K any] interface {

// StandardFunctionGetter is a basic implementation of FunctionGetter.
type StandardFunctionGetter[K any] struct {
fCtx FunctionContext
fact Factory[K]
FCtx FunctionContext
Fact Factory[K]
}

// Get takes an Arguments struct containing arguments the caller wants passed to the
// function and instantiates the function with those arguments.
// If there is a mismatch between the function's signature and the arguments the caller
// wants to pass to the function, an error is returned.
func (g StandardFunctionGetter[K]) Get(args Arguments) (Expr[K], error) {
if g.fact == nil {
if g.Fact == nil {
return Expr[K]{}, fmt.Errorf("undefined function")
}
fArgs := g.fact.CreateDefaultArguments()
fArgs := g.Fact.CreateDefaultArguments()
if reflect.TypeOf(fArgs).Kind() != reflect.Pointer {
return Expr[K]{}, fmt.Errorf("factory for %q must return a pointer to an Arguments value in its CreateDefaultArguments method", g.fact.Name())
return Expr[K]{}, fmt.Errorf("factory for %q must return a pointer to an Arguments value in its CreateDefaultArguments method", g.Fact.Name())
}
if reflect.TypeOf(args).Kind() != reflect.Pointer {
return Expr[K]{}, fmt.Errorf("%q must be pointer to an Arguments value", reflect.TypeOf(args).Kind())
Expand All @@ -282,7 +282,7 @@ func (g StandardFunctionGetter[K]) Get(args Arguments) (Expr[K], error) {
field := argsVal.Field(i)
fArgsVal.Field(i).Set(field)
}
fn, err := g.fact.CreateFunction(g.fCtx, fArgs)
fn, err := g.Fact.CreateFunction(g.FCtx, fArgs)
if err != nil {
return Expr[K]{}, fmt.Errorf("couldn't create function: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func Test_FunctionGetter(t *testing.T) {
return "str", nil
},
},
function: StandardFunctionGetter[any]{fCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, fact: functions["SHA256"]},
function: StandardFunctionGetter[any]{FCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, Fact: functions["SHA256"]},
want: "anything",
valid: true,
},
Expand All @@ -731,7 +731,7 @@ func Test_FunctionGetter(t *testing.T) {
return nil, nil
},
},
function: StandardFunctionGetter[any]{fCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, fact: functions["SHA250"]},
function: StandardFunctionGetter[any]{FCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, Fact: functions["SHA250"]},
want: "anything",
valid: false,
expectedErrorMsg: "undefined function",
Expand All @@ -743,7 +743,7 @@ func Test_FunctionGetter(t *testing.T) {
return nil, nil
},
},
function: StandardFunctionGetter[any]{fCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, fact: functions["test_arg_mismatch"]},
function: StandardFunctionGetter[any]{FCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, Fact: functions["test_arg_mismatch"]},
want: "anything",
valid: false,
expectedErrorMsg: "incorrect number of arguments. Expected: 4 Received: 1",
Expand All @@ -755,7 +755,7 @@ func Test_FunctionGetter(t *testing.T) {
return nil, nil
},
},
function: StandardFunctionGetter[any]{fCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, fact: functions["cannot_create_function"]},
function: StandardFunctionGetter[any]{FCtx: FunctionContext{Set: componenttest.NewNopTelemetrySettings()}, Fact: functions["cannot_create_function"]},
want: "anything",
valid: false,
expectedErrorMsg: "couldn't create function: error",
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (p *Parser[K]) buildArgs(ed editor, argsVal reflect.Value) error {
if !ok {
return fmt.Errorf("undefined function %s", name)
}
val = StandardFunctionGetter[K]{fCtx: FunctionContext{Set: p.telemetrySettings}, fact: f}
val = StandardFunctionGetter[K]{FCtx: FunctionContext{Set: p.telemetrySettings}, Fact: f}
case fieldType.Kind() == reflect.Slice:
val, err = p.buildSliceArg(arg.Value, fieldType)
default:
Expand Down
Loading