Skip to content

Commit d39f561

Browse files
committed
polishing of implementation
Signed-off-by: odubajDT <[email protected]>
1 parent cfc0282 commit d39f561

File tree

7 files changed

+32
-20
lines changed

7 files changed

+32
-20
lines changed

pkg/ottl/contexts/internal/map.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
2222
return nil, err
2323
}
2424
if s == nil {
25-
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[0])
25+
resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[0])
2626
if err != nil {
2727
return nil, fmt.Errorf("non-string indexing is not supported")
2828
}
@@ -47,7 +47,7 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
4747
return err
4848
}
4949
if s == nil {
50-
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[0])
50+
resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[0])
5151
if err != nil {
5252
return fmt.Errorf("non-string indexing is not supported")
5353
}
@@ -61,8 +61,8 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
6161
return setIndexableValue[K](ctx, tCtx, currentValue, val, keys[1:])
6262
}
6363

64-
func FetchValueFromPath[K any, T int64 | string](ctx context.Context, tCtx K, key ottl.Key[K]) (*T, error) {
65-
p, err := key.PathGetter(ctx, tCtx)
64+
func FetchValueFromExpression[K any, T int64 | string](ctx context.Context, tCtx K, key ottl.Key[K]) (*T, error) {
65+
p, err := key.ExpressionGetter(ctx, tCtx)
6666
if err != nil {
6767
return nil, err
6868
}

pkg/ottl/contexts/internal/path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ func (k *TestKey[K]) Int(_ context.Context, _ K) (*int64, error) {
5757
return k.I, nil
5858
}
5959

60-
func (k *TestKey[K]) PathGetter(_ context.Context, _ K) (ottl.Getter[K], error) {
60+
func (k *TestKey[K]) ExpressionGetter(_ context.Context, _ K) (ottl.Getter[K], error) {
6161
return k.P, nil
6262
}

pkg/ottl/contexts/internal/slice.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func GetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o
2222
return nil, err
2323
}
2424
if i == nil {
25-
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[0])
25+
resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[0])
2626
if err != nil {
2727
return nil, fmt.Errorf("non-integer indexing is not supported")
2828
}
@@ -48,7 +48,7 @@ func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o
4848
return err
4949
}
5050
if i == nil {
51-
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[0])
51+
resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[0])
5252
if err != nil {
5353
return fmt.Errorf("non-integer indexing is not supported")
5454
}

pkg/ottl/contexts/internal/value.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value,
7979
return nil, err
8080
}
8181
if s == nil {
82-
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[count])
82+
resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[count])
8383
if err != nil {
8484
return nil, errors.New("map must be indexed by a string")
8585
}
@@ -95,7 +95,7 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value,
9595
return nil, err
9696
}
9797
if i == nil {
98-
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[count])
98+
resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[count])
9999
if err != nil {
100100
return nil, errors.New("slice must be indexed by an int")
101101
}
@@ -133,7 +133,7 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.
133133
return err
134134
}
135135
if s == nil {
136-
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[count])
136+
resString, err := FetchValueFromExpression[K, string](ctx, tCtx, keys[count])
137137
if err != nil {
138138
return errors.New("map must be indexed by a string")
139139
}
@@ -151,7 +151,7 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.
151151
return err
152152
}
153153
if i == nil {
154-
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[count])
154+
resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[count])
155155
if err != nil {
156156
return errors.New("slice must be indexed by an int")
157157
}
@@ -180,8 +180,8 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.
180180
}
181181
currentValue = currentValue.Slice().AppendEmpty()
182182
default:
183-
resString, errString := FetchValueFromPath[K, string](ctx, tCtx, keys[count])
184-
resInt, errInt := FetchValueFromPath[K, int64](ctx, tCtx, keys[count])
183+
resString, errString := FetchValueFromExpression[K, string](ctx, tCtx, keys[count])
184+
resInt, errInt := FetchValueFromExpression[K, int64](ctx, tCtx, keys[count])
185185
switch {
186186
case errInt == nil:
187187
currentValue.SetEmptySlice()

pkg/ottl/functions.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ func buildOriginalKeysText(keys []key) string {
5353
if k.String != nil {
5454
builder.WriteString(*k.String)
5555
}
56-
if k.Expression != nil && k.Expression.Path != nil {
57-
builder.WriteString(buildOriginalText(k.Expression.Path))
56+
if k.Expression != nil {
57+
if k.Expression.Path != nil {
58+
builder.WriteString(buildOriginalText(k.Expression.Path))
59+
}
60+
if k.Expression.Float != nil {
61+
builder.WriteString(strconv.FormatFloat(*k.Expression.Float, 'f', 10, 64))
62+
}
63+
if k.Expression.Int != nil {
64+
builder.WriteString(strconv.FormatInt(*k.Expression.Int, 10))
65+
}
5866
}
5967
builder.WriteString("]")
6068
}
@@ -262,7 +270,11 @@ type Key[K any] interface {
262270
// If Key experiences an error retrieving the value it is returned.
263271
Int(context.Context, K) (*int64, error)
264272

265-
PathGetter(context.Context, K) (Getter[K], error)
273+
// ExpressionGetter returns a Getter to the expression, that can be
274+
// part of the path.
275+
// If the Key does not have an expression the returned value is nil.
276+
// If Key experiences an error retrieving the value it is returned.
277+
ExpressionGetter(context.Context, K) (Getter[K], error)
266278
}
267279

268280
var _ Key[any] = &baseKey[any]{}
@@ -281,7 +293,7 @@ func (k *baseKey[K]) Int(_ context.Context, _ K) (*int64, error) {
281293
return k.i, nil
282294
}
283295

284-
func (k *baseKey[K]) PathGetter(_ context.Context, _ K) (Getter[K], error) {
296+
func (k *baseKey[K]) ExpressionGetter(_ context.Context, _ K) (Getter[K], error) {
285297
return k.p, nil
286298
}
287299

pkg/ottl/ottlfuncs/func_set.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
type SetArguments[K any] struct {
14-
Target ottl.GetSetter[K]
14+
Target ottl.Setter[K]
1515
Value ottl.Getter[K]
1616
}
1717

@@ -29,7 +29,7 @@ func createSetFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ott
2929
return set(args.Target, args.Value), nil
3030
}
3131

32-
func set[K any](target ottl.GetSetter[K], value ottl.Getter[K]) ottl.ExprFunc[K] {
32+
func set[K any](target ottl.Setter[K], value ottl.Getter[K]) ottl.ExprFunc[K] {
3333
return func(ctx context.Context, tCtx K) (any, error) {
3434
val, err := value.Get(ctx, tCtx)
3535
if err != nil {

pkg/ottl/ottlfuncs/func_set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Test_set(t *testing.T) {
2525

2626
tests := []struct {
2727
name string
28-
setter ottl.GetSetter[pcommon.Value]
28+
setter ottl.Setter[pcommon.Value]
2929
getter ottl.Getter[pcommon.Value]
3030
want func(pcommon.Value)
3131
}{

0 commit comments

Comments
 (0)