Skip to content

Commit

Permalink
polishing of implementation
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Dec 19, 2024
1 parent b8c4622 commit b62391b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
8 changes: 4 additions & 4 deletions pkg/ottl/contexts/internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/contexts/internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions pkg/ottl/contexts/internal/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/ottl/contexts/internal/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 16 additions & 4 deletions pkg/ottl/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("]")
}
Expand Down Expand Up @@ -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]{}
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ottl/ottlfuncs/func_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type SetArguments[K any] struct {
Target ottl.GetSetter[K]
Target ottl.Setter[K]
Value ottl.Getter[K]
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/ottlfuncs/func_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}{
Expand Down

0 comments on commit b62391b

Please sign in to comment.