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] Cleanup OTTL Time/Duration #26364

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,13 @@ func (p *Parser[K]) newGetterFromConverter(c converter) (Getter[K], error) {
}, nil
}

// TimeGetter is a Getter that must return an time.Time.
// TimeGetter is a Getter that must return a time.Time.
type TimeGetter[K any] interface {
// Get retrieves an time.Time value.
// Get retrieves a time.Time value.
Get(ctx context.Context, tCtx K) (time.Time, error)
}

// StandardTimeGetter is a basic implementation of IntGetter
// StandardTimeGetter is a basic implementation of TimeGetter
type StandardTimeGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (interface{}, error)
}
Expand All @@ -613,9 +613,9 @@ func (g StandardTimeGetter[K]) Get(ctx context.Context, tCtx K) (time.Time, erro
}
}

// DurationGetter is a Getter that must return an time.Duration.
// DurationGetter is a Getter that must return a time.Duration.
type DurationGetter[K any] interface {
// Get retrieves an int64 value.
// Get retrieves a time.Duration value.
Get(ctx context.Context, tCtx K) (time.Duration, error)
}

Expand Down
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(_ []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(_ []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
Loading