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 4 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:
27 changes: 27 additions & 0 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ func (l *listGetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
return evaluated, nil
}

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

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

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

func (g StandardTypeGetter[K, T]) Get(ctx context.Context, tCtx K) (*T, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
v, ok := val.(T)
if !ok {
return nil, fmt.Errorf("expected %T but got %T", v, val)
}
return &v, 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
105 changes: 105 additions & 0 deletions pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ottl

import (
"context"
"go.opentelemetry.io/collector/component/componenttest"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -318,3 +319,107 @@ func Test_newGetter(t *testing.T) {
assert.Error(t, err)
})
}

func Test_StringGetter(t *testing.T) {
evan-bradley marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
name string
val value
want interface{}
valid bool
expectedErrorMsg string
}{
{
name: "Correct type",
val: value{
String: ottltest.Strp("str"),
},
want: "str",
valid: true,
},
{
name: "Incorrect type",
val: value{
Bool: (*boolean)(ottltest.Boolp(true)),
},
valid: false,
expectedErrorMsg: "expected string but got bool",
},
}

p := NewParser[any](
defaultFunctionsForTests(),
testParsePath,
componenttest.NewNopTelemetrySettings(),
WithEnumParser[any](testParseEnum),
)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g, err := p.newGetter(tt.val)
assert.NoError(t, err)

stg := StandardTypeGetter[interface{}, string]{Getter: g.Get}
val, err := stg.Get(context.Background(), nil)

if tt.valid {
assert.NoError(t, err)
assert.Equal(t, tt.want, *val)
} else {
assert.EqualError(t, err, tt.expectedErrorMsg)
}
})
}
}

func Test_IntGetter(t *testing.T) {
tests := []struct {
name string
val value
want interface{}
valid bool
expectedErrorMsg string
}{
{
name: "Correct type",
val: value{
Literal: &mathExprLiteral{
Int: ottltest.Intp(1),
},
},
want: int64(1),
valid: true,
},
{
name: "Incorrect type",
val: value{
String: ottltest.Strp("1"),
},
valid: false,
expectedErrorMsg: "expected int64 but got string",
},
}

p := NewParser[any](
defaultFunctionsForTests(),
testParsePath,
componenttest.NewNopTelemetrySettings(),
WithEnumParser[any](testParseEnum),
)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g, err := p.newGetter(tt.val)
assert.NoError(t, err)

stg := StandardTypeGetter[interface{}, int64]{Getter: g.Get}
val, err := stg.Get(context.Background(), nil)

if tt.valid {
assert.NoError(t, err)
assert.Equal(t, tt.want, *val)
} else {
assert.EqualError(t, err, tt.expectedErrorMsg)
}
})
}
}
12 changes: 12 additions & 0 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ 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 StandardTypeGetter[K, string]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "IntGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardTypeGetter[K, int64]{Getter: arg.Get}, nil
case name == "Enum":
arg, err := p.enumParser(argVal.Enum)
if err != nil {
Expand Down
45 changes: 42 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,32 @@ 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: "intgetter arg",
inv: invocation{
Function: "testing_intgetter",
Arguments: []value{
{
Literal: &mathExprLiteral{
Int: ottltest.Intp(1),
},
},
},
},
want: nil,
},
{
name: "string arg",
inv: invocation{
Expand Down Expand Up @@ -872,6 +898,18 @@ 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 functionWithIntGetter(IntGetter[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 +981,8 @@ 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_intgetter"] = functionWithIntGetter
functions["testing_string"] = functionWithString
functions["testing_float"] = functionWithFloat
functions["testing_int"] = functionWithInt
Expand Down