Skip to content

Commit

Permalink
Make time and dur getters usable
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Sep 1, 2023
1 parent 250ead1 commit b5666a7
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ func (p *Parser[K]) buildSliceArg(argVal value, argType reflect.Type) (any, erro
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "DurationGetter"):
arg, err := buildSlice[DurationGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "TimeGetter"):
arg, err := buildSlice[TimeGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
default:
return nil, fmt.Errorf("unsupported slice type %q for function", argType.Elem().Name())
}
Expand Down Expand Up @@ -249,6 +261,18 @@ func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) {
return nil, err
}
return StandardPMapGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "DurationGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardDurationGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "TimeGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardTimeGetter[K]{Getter: arg.Get}, nil
case name == "Enum":
arg, err := p.enumParser(argVal.Enum)
if err != nil {
Expand Down
116 changes: 116 additions & 0 deletions pkg/ottl/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,40 @@ func Test_NewFunctionCall(t *testing.T) {
},
want: 2,
},
{
name: "durationgetter slice arg",
inv: editor{
Function: "testing_durationgetter_slice",
Arguments: []value{
{
List: &list{
Values: []value{
{
String: ottltest.Strp("test"),
},
},
},
},
},
},
},
{
name: "timegetter slice arg",
inv: editor{
Function: "testing_timegetter_slice",
Arguments: []value{
{
List: &list{
Values: []value{
{
String: ottltest.Strp("test"),
},
},
},
},
},
},
},
{
name: "floatgetter slice arg",
inv: editor{
Expand Down Expand Up @@ -971,6 +1005,28 @@ func Test_NewFunctionCall(t *testing.T) {
},
want: nil,
},
{
name: "durationgetter arg",
inv: editor{
Function: "testing_durationgetter",
Arguments: []value{
{
String: ottltest.Strp("test"),
},
},
},
},
{
name: "timegetter arg",
inv: editor{
Function: "testing_timegetter",
Arguments: []value{
{
String: ottltest.Strp("test"),
},
},
},
},
{
name: "functiongetter arg (Uppercase)",
inv: editor{
Expand Down Expand Up @@ -1274,6 +1330,26 @@ func functionWithStringGetterSlice(getters []StringGetter[interface{}]) (ExprFun
}, nil
}

type durationGetterSliceArguments struct {
DurationGetters []DurationGetter[any] `ottlarg:"0"`
}

func functionWithDurationGetterSlice(getters []DurationGetter[interface{}]) (ExprFunc[interface{}], error) {
return func(context.Context, interface{}) (interface{}, error) {
return nil, nil
}, nil
}

type timeGetterSliceArguments struct {
TimeGetters []TimeGetter[any] `ottlarg:"0"`
}

func functionWithTimeGetterSlice(getters []TimeGetter[interface{}]) (ExprFunc[interface{}], error) {
return func(context.Context, interface{}) (interface{}, error) {
return nil, nil
}, nil
}

type floatGetterSliceArguments struct {
FloatGetters []FloatGetter[any] `ottlarg:"0"`
}
Expand Down Expand Up @@ -1374,6 +1450,26 @@ func functionWithStringGetter(StringGetter[interface{}]) (ExprFunc[interface{}],
}, nil
}

type durationGetterArguments struct {
DurationGetterArg DurationGetter[any] `ottlarg:"0"`
}

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

type timeGetterArguments struct {
TimeGetterArg TimeGetter[any] `ottlarg:"0"`
}

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

type functionGetterArguments struct {
FunctionGetterArg FunctionGetter[any] `ottlarg:"0"`
}
Expand Down Expand Up @@ -1608,6 +1704,16 @@ func defaultFunctionsForTests() map[string]Factory[any] {
&stringGetterSliceArguments{},
functionWithStringGetterSlice,
),
createFactory[any](
"testing_durationgetter_slice",
&durationGetterSliceArguments{},
functionWithDurationGetterSlice,
),
createFactory[any](
"testing_timegetter_slice",
&timeGetterSliceArguments{},
functionWithTimeGetterSlice,
),
createFactory[any](
"testing_stringlikegetter_slice",
&stringLikeGetterSliceArguments{},
Expand Down Expand Up @@ -1653,6 +1759,16 @@ func defaultFunctionsForTests() map[string]Factory[any] {
&getterArguments{},
functionWithGetter,
),
createFactory[any](
"testing_durationgetter",
&durationGetterArguments{},
functionWithDurationGetter,
),
createFactory[any](
"testing_timegetter",
&timeGetterArguments{},
functionWithTimeGetter,
),
createFactory[any](
"testing_stringgetter",
&stringGetterArguments{},
Expand Down

0 comments on commit b5666a7

Please sign in to comment.