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] Add StringGetter #18042

Merged
merged 8 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions .chloggen/ottl-type-specific-getter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: Introduce the concept of type-specific Getters that help simplify type assertions in functions.

# One or more tracking issues related to the change
issues: [18042]

# (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:
23 changes: 23 additions & 0 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type GetSetter[K any] interface {
Setter[K]
}

type StringGetter[K any] interface {
Get(ctx context.Context, tCtx K) (*string, error)
}

type StandardGetSetter[K any] struct {
Getter func(ctx context.Context, tCx K) (interface{}, error)
Setter func(ctx context.Context, tCx K, val interface{}) error
Expand Down Expand Up @@ -89,6 +93,25 @@ func (l *listGetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
return evaluated, nil
}

type StandardStringGetter[K any] struct {
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
Getter func(ctx context.Context, tCx K) (interface{}, error)
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
}

func (g StandardStringGetter[K]) Get(ctx context.Context, tCtx K) (*string, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
strVal, ok := val.(string)
if !ok {
return nil, fmt.Errorf("value was not a string")
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
}
return &strVal, nil
}

func (p *Parser[K]) newGetter(val value) (Getter[K], error) {
if val.IsNil != nil && *val.IsNil {
return &literal[K]{value: nil}, nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "StringGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardStringGetter[K]{Getter: arg.Get}, nil
case name == "Enum":
arg, err := p.enumParser(argVal.Enum)
if err != nil {
Expand Down
24 changes: 21 additions & 3 deletions pkg/ottl/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func Test_NewFunctionCall(t *testing.T) {
{
Literal: &mathExprLiteral{
Converter: &converter{
Function: "Testing_getter",
Function: "testing_getter",
Arguments: []value{
{
Literal: &mathExprLiteral{
Expand Down Expand Up @@ -599,7 +599,7 @@ func Test_NewFunctionCall(t *testing.T) {
{
Literal: &mathExprLiteral{
Converter: &converter{
Function: "Testing_getter",
Function: "testing_getter",
Arguments: []value{
{
Literal: &mathExprLiteral{
Expand All @@ -623,6 +623,18 @@ func Test_NewFunctionCall(t *testing.T) {
},
want: nil,
},
{
name: "stringgetter arg",
inv: invocation{
Function: "testing_stringgetter",
Arguments: []value{
{
String: ottltest.Strp("test"),
},
},
},
want: nil,
},
{
name: "string arg",
inv: invocation{
Expand Down Expand Up @@ -872,6 +884,12 @@ func functionWithGetter(Getter[interface{}]) (ExprFunc[interface{}], error) {
}, nil
}

func functionWithStringGetter(StringGetter[interface{}]) (ExprFunc[interface{}], error) {
return func(context.Context, interface{}) (interface{}, error) {
return "anything", nil
}, nil
}

func functionWithString(string) (ExprFunc[interface{}], error) {
return func(context.Context, interface{}) (interface{}, error) {
return "anything", nil
Expand Down Expand Up @@ -943,7 +961,7 @@ func defaultFunctionsForTests() map[string]interface{} {
functions["testing_setter"] = functionWithSetter
functions["testing_getsetter"] = functionWithGetSetter
functions["testing_getter"] = functionWithGetter
functions["Testing_getter"] = functionWithGetter
functions["testing_stringgetter"] = functionWithStringGetter
functions["testing_string"] = functionWithString
functions["testing_float"] = functionWithFloat
functions["testing_int"] = functionWithInt
Expand Down