From b62391be4d7470f964b358c88f28fdcf7ad55c76 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 19 Dec 2024 09:57:12 +0100 Subject: [PATCH] polishing of implementation Signed-off-by: odubajDT --- pkg/ottl/contexts/internal/map.go | 8 ++++---- pkg/ottl/contexts/internal/path.go | 2 +- pkg/ottl/contexts/internal/slice.go | 4 ++-- pkg/ottl/contexts/internal/value.go | 12 ++++++------ pkg/ottl/functions.go | 20 ++++++++++++++++---- pkg/ottl/ottlfuncs/func_set.go | 4 ++-- pkg/ottl/ottlfuncs/func_set_test.go | 2 +- 7 files changed, 32 insertions(+), 20 deletions(-) diff --git a/pkg/ottl/contexts/internal/map.go b/pkg/ottl/contexts/internal/map.go index 439f9f71c701..52f57e95559b 100644 --- a/pkg/ottl/contexts/internal/map.go +++ b/pkg/ottl/contexts/internal/map.go @@ -22,7 +22,7 @@ func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl. return nil, err } if s == nil { - resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[0]) + resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[0]) if err != nil { return nil, fmt.Errorf("non-string indexing is not supported") } @@ -47,7 +47,7 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl. return err } if s == nil { - resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[0]) + resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[0]) if err != nil { return fmt.Errorf("non-string indexing is not supported") } @@ -61,8 +61,8 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl. return setIndexableValue[K](ctx, tCtx, currentValue, val, keys[1:]) } -func FetchValueFromPath[K any, T int64 | string](ctx context.Context, tCtx K, key ottl.Key[K]) (*T, error) { - p, err := key.PathGetter(ctx, tCtx) +func FetchValueFromExpression[K any, T int64 | string](ctx context.Context, tCtx K, key ottl.Key[K]) (*T, error) { + p, err := key.ExpressionGetter(ctx, tCtx) if err != nil { return nil, err } diff --git a/pkg/ottl/contexts/internal/path.go b/pkg/ottl/contexts/internal/path.go index b83dfefd1299..cfba45aaf291 100644 --- a/pkg/ottl/contexts/internal/path.go +++ b/pkg/ottl/contexts/internal/path.go @@ -57,6 +57,6 @@ func (k *TestKey[K]) Int(_ context.Context, _ K) (*int64, error) { return k.I, nil } -func (k *TestKey[K]) PathGetter(_ context.Context, _ K) (ottl.Getter[K], error) { +func (k *TestKey[K]) ExpressionGetter(_ context.Context, _ K) (ottl.Getter[K], error) { return k.P, nil } diff --git a/pkg/ottl/contexts/internal/slice.go b/pkg/ottl/contexts/internal/slice.go index d1d0285ffdd0..a4ba242a40bf 100644 --- a/pkg/ottl/contexts/internal/slice.go +++ b/pkg/ottl/contexts/internal/slice.go @@ -22,7 +22,7 @@ func GetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o return nil, err } if i == nil { - resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[0]) + resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[0]) if err != nil { return nil, fmt.Errorf("non-integer indexing is not supported") } @@ -48,7 +48,7 @@ func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o return err } if i == nil { - resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[0]) + resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[0]) if err != nil { return fmt.Errorf("non-integer indexing is not supported") } diff --git a/pkg/ottl/contexts/internal/value.go b/pkg/ottl/contexts/internal/value.go index 2b62f6d5066e..2136a3fbc995 100644 --- a/pkg/ottl/contexts/internal/value.go +++ b/pkg/ottl/contexts/internal/value.go @@ -79,7 +79,7 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value, return nil, err } if s == nil { - resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[count]) + resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[count]) if err != nil { return nil, errors.New("map must be indexed by a string") } @@ -95,7 +95,7 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value, return nil, err } if i == nil { - resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[count]) + resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[count]) if err != nil { return nil, errors.New("slice must be indexed by an int") } @@ -133,7 +133,7 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon. return err } if s == nil { - resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[count]) + resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[count]) if err != nil { return errors.New("map must be indexed by a string") } @@ -151,7 +151,7 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon. return err } if i == nil { - resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[count]) + resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[count]) if err != nil { return errors.New("slice must be indexed by an int") } @@ -180,8 +180,8 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon. } currentValue = currentValue.Slice().AppendEmpty() default: - resString, errString := FetchValueFromPath[K, string](ctx, tCtx, keys[count]) - resInt, errInt := FetchValueFromPath[K, int64](ctx, tCtx, keys[count]) + resString, errString := FetchValueFromExpression[K, string](ctx, tCtx, keys[count]) + resInt, errInt := FetchValueFromExpression[K, int64](ctx, tCtx, keys[count]) switch { case errInt == nil: currentValue.SetEmptySlice() diff --git a/pkg/ottl/functions.go b/pkg/ottl/functions.go index bbc7ee630225..816a2dd34302 100644 --- a/pkg/ottl/functions.go +++ b/pkg/ottl/functions.go @@ -53,8 +53,16 @@ func buildOriginalKeysText(keys []key) string { if k.String != nil { builder.WriteString(*k.String) } - if k.Expression != nil && k.Expression.Path != nil { - builder.WriteString(buildOriginalText(k.Expression.Path)) + if k.Expression != nil { + if k.Expression.Path != nil { + builder.WriteString(buildOriginalText(k.Expression.Path)) + } + if k.Expression.Float != nil { + builder.WriteString(strconv.FormatFloat(*k.Expression.Float, 'f', 10, 64)) + } + if k.Expression.Int != nil { + builder.WriteString(strconv.FormatInt(*k.Expression.Int, 10)) + } } builder.WriteString("]") } @@ -262,7 +270,11 @@ type Key[K any] interface { // If Key experiences an error retrieving the value it is returned. Int(context.Context, K) (*int64, error) - PathGetter(context.Context, K) (Getter[K], error) + // ExpressionGetter returns a Getter to the expression, that can be + // part of the path. + // If the Key does not have an expression the returned value is nil. + // If Key experiences an error retrieving the value it is returned. + ExpressionGetter(context.Context, K) (Getter[K], error) } var _ Key[any] = &baseKey[any]{} @@ -281,7 +293,7 @@ func (k *baseKey[K]) Int(_ context.Context, _ K) (*int64, error) { return k.i, nil } -func (k *baseKey[K]) PathGetter(_ context.Context, _ K) (Getter[K], error) { +func (k *baseKey[K]) ExpressionGetter(_ context.Context, _ K) (Getter[K], error) { return k.p, nil } diff --git a/pkg/ottl/ottlfuncs/func_set.go b/pkg/ottl/ottlfuncs/func_set.go index 4341fca9e034..a0ce390a4788 100644 --- a/pkg/ottl/ottlfuncs/func_set.go +++ b/pkg/ottl/ottlfuncs/func_set.go @@ -11,7 +11,7 @@ import ( ) type SetArguments[K any] struct { - Target ottl.GetSetter[K] + Target ottl.Setter[K] Value ottl.Getter[K] } @@ -29,7 +29,7 @@ func createSetFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ott return set(args.Target, args.Value), nil } -func set[K any](target ottl.GetSetter[K], value ottl.Getter[K]) ottl.ExprFunc[K] { +func set[K any](target ottl.Setter[K], value ottl.Getter[K]) ottl.ExprFunc[K] { return func(ctx context.Context, tCtx K) (any, error) { val, err := value.Get(ctx, tCtx) if err != nil { diff --git a/pkg/ottl/ottlfuncs/func_set_test.go b/pkg/ottl/ottlfuncs/func_set_test.go index 5bd910b551fe..7fbf3b2fc765 100644 --- a/pkg/ottl/ottlfuncs/func_set_test.go +++ b/pkg/ottl/ottlfuncs/func_set_test.go @@ -25,7 +25,7 @@ func Test_set(t *testing.T) { tests := []struct { name string - setter ottl.GetSetter[pcommon.Value] + setter ottl.Setter[pcommon.Value] getter ottl.Getter[pcommon.Value] want func(pcommon.Value) }{