Skip to content

Commit

Permalink
[pkg/ottl] Add StringGetter (#18042)
Browse files Browse the repository at this point in the history
Adds a new concept to Getters that allow specifying type. These type-specific Getters can be used by functions that require a specific type instead of using the normal Getter that always returns an interface. This PR implements a StringGetter, but we can add Getter's for int64, float64, bool, pcommon.Map, etc in the future.

With these Getters functions and rely on OTTL for type assertion and with the ongoing error handling discussion these will help standardize how functions handle and return errors.

Related to #16519
  • Loading branch information
TylerHelmuth authored Feb 10, 2023
1 parent d21679b commit 2ce8945
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 9 deletions.
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:
39 changes: 33 additions & 6 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ type GetSetter[K any] interface {
}

type StandardGetSetter[K any] struct {
Getter func(ctx context.Context, tCx K) (interface{}, error)
Setter func(ctx context.Context, tCx K, val interface{}) error
Getter func(ctx context.Context, tCtx K) (interface{}, error)
Setter func(ctx context.Context, tCtx K, val interface{}) error
}

func (path StandardGetSetter[K]) Get(ctx context.Context, tCx K) (interface{}, error) {
return path.Getter(ctx, tCx)
func (path StandardGetSetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
return path.Getter(ctx, tCtx)
}

func (path StandardGetSetter[K]) Set(ctx context.Context, tCx K, val interface{}) error {
return path.Setter(ctx, tCx, val)
func (path StandardGetSetter[K]) Set(ctx context.Context, tCtx K, val interface{}) error {
return path.Setter(ctx, tCtx, val)
}

type literal[K any] struct {
Expand Down 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, tCtx K) (interface{}, error)
}

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
43 changes: 43 additions & 0 deletions pkg/ottl/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,46 @@ func Test_newGetter(t *testing.T) {
assert.Error(t, err)
})
}

func Test_StandardTypeGetter(t *testing.T) {
tests := []struct {
name string
getter StandardTypeGetter[interface{}, string]
want interface{}
valid bool
expectedErrorMsg string
}{
{
name: "Correct type",
getter: StandardTypeGetter[interface{}, string]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "str", nil
},
},
want: "str",
valid: true,
},
{
name: "Incorrect type",
getter: StandardTypeGetter[interface{}, string]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return true, nil
},
},
valid: false,
expectedErrorMsg: "expected string but got bool",
},
}

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.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

0 comments on commit 2ce8945

Please sign in to comment.