Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Dec 18, 2024
1 parent 37c21a9 commit 1577fcb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 99 deletions.
40 changes: 20 additions & 20 deletions pkg/ottl/contexts/internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,11 @@ func GetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
return nil, err
}
if s == nil {
p, err := keys[0].PathGetter(ctx, tCtx)
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[0])
if err != nil {
return nil, err
}
res, err := p.Get(ctx, tCtx)
if err != nil {
return nil, err
}
resString, ok := res.(string)
if !ok {
return nil, fmt.Errorf("non-string indexing is not supported")
}
s = &resString
s = resString
}

val, ok := m.Get(*s)
Expand All @@ -55,19 +47,11 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
return err
}
if s == nil {
p, err := keys[0].PathGetter(ctx, tCtx)
if err != nil {
return err
}
res, err := p.Get(ctx, tCtx)
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[0])
if err != nil {
return err
}
resString, ok := res.(string)
if !ok {
return fmt.Errorf("non-string indexing is not supported")
}
s = &resString
s = resString
}

currentValue, ok := m.Get(*s)
Expand All @@ -76,3 +60,19 @@ 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)
if err != nil {
return nil, err
}
res, err := p.Get(ctx, tCtx)
if err != nil {
return nil, err
}
resVal, ok := res.(T)
if !ok {
return nil, fmt.Errorf("casting not successfull")

Check failure on line 75 in pkg/ottl/contexts/internal/map.go

View workflow job for this annotation

GitHub Actions / lint-matrix (windows, pkg)

"successfull" is a misspelling of "successful"

Check failure on line 75 in pkg/ottl/contexts/internal/map.go

View workflow job for this annotation

GitHub Actions / lint-matrix (windows, pkg)

"successfull" is a misspelling of "successful"

Check failure on line 75 in pkg/ottl/contexts/internal/map.go

View workflow job for this annotation

GitHub Actions / lint-matrix (linux, pkg)

"successfull" is a misspelling of "successful"

Check failure on line 75 in pkg/ottl/contexts/internal/map.go

View workflow job for this annotation

GitHub Actions / lint-matrix (linux, pkg)

"successfull" is a misspelling of "successful"
}
return &resVal, nil
}
24 changes: 4 additions & 20 deletions pkg/ottl/contexts/internal/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,11 @@ func GetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o
return nil, err
}
if i == nil {
p, err := keys[0].PathGetter(ctx, tCtx)
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[0])
if err != nil {
return nil, err
}
res, err := p.Get(ctx, tCtx)
if err != nil {
return nil, err
}
resInt, ok := res.(int64)
if !ok {
return nil, fmt.Errorf("non-integer indexing is not supported")
}
i = &resInt
i = resInt
}

idx := int(*i)
Expand All @@ -56,19 +48,11 @@ func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o
return err
}
if i == nil {
p, err := keys[0].PathGetter(ctx, tCtx)
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[0])
if err != nil {
return err
}
res, err := p.Get(ctx, tCtx)
if err != nil {
return err
}
resInt, ok := res.(int64)
if !ok {
return fmt.Errorf("non-integer indexing is not supported")
}
i = &resInt
i = resInt
}

idx := int(*i)
Expand Down
79 changes: 20 additions & 59 deletions pkg/ottl/contexts/internal/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,11 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value,
return nil, err
}
if s == nil {
p, err := keys[count].PathGetter(ctx, tCtx)
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[count])
if err != nil {
return nil, err
}
res, err := p.Get(ctx, tCtx)
if err != nil {
return nil, err
}
resString, okk := res.(string)
if !okk {
return nil, errors.New("map must be indexed by a string")
}
s = &resString
s = resString
}
val, ok = val.Map().Get(*s)
if !ok {
Expand All @@ -103,19 +95,11 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value,
return nil, err
}
if i == nil {
p, err := keys[count].PathGetter(ctx, tCtx)
if err != nil {
return nil, err
}
res, err := p.Get(ctx, tCtx)
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[count])
if err != nil {
return nil, err
}
resInt, ok := res.(int64)
if !ok {
return nil, errors.New("slice must be indexed by an int")
}
i = &resInt
i = resInt
}
if int(*i) >= val.Slice().Len() || int(*i) < 0 {
return nil, fmt.Errorf("index %v out of bounds", *i)
Expand Down Expand Up @@ -149,19 +133,11 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.
return err
}
if s == nil {
p, err := keys[count].PathGetter(ctx, tCtx)
if err != nil {
return err
}
res, err := p.Get(ctx, tCtx)
resString, err := FetchValueFromPath[K, string](ctx, tCtx, keys[count])
if err != nil {
return err
}
resString, ok := res.(string)
if !ok {
return errors.New("map must be indexed by a string")
}
s = &resString
s = resString
}
potentialValue, ok := currentValue.Map().Get(*s)
if !ok {
Expand All @@ -175,19 +151,11 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.
return err
}
if i == nil {
p, err := keys[count].PathGetter(ctx, tCtx)
if err != nil {
return err
}
res, err := p.Get(ctx, tCtx)
resInt, err := FetchValueFromPath[K, int64](ctx, tCtx, keys[count])
if err != nil {
return err
}
resInt, ok := res.(int64)
if !ok {
return errors.New("slice must be indexed by an int")
}
i = &resInt
i = resInt
}
if int(*i) >= currentValue.Slice().Len() || int(*i) < 0 {
return fmt.Errorf("index %v out of bounds", *i)
Expand All @@ -212,27 +180,20 @@ func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.
}
currentValue = currentValue.Slice().AppendEmpty()
default:
p, err := keys[count].PathGetter(ctx, tCtx)
if err != nil {
return err
}
res, err := p.Get(ctx, tCtx)
if err != nil {
return err
}
resInt, ok := res.(int64)
if !ok {
resString, ok := res.(string)
if !ok {
return errors.New("neither a string nor an int index was given, this is an error in the OTTL")
resString, errString := FetchValueFromPath[K, string](ctx, tCtx, keys[count])
resInt, errInt := FetchValueFromPath[K, int64](ctx, tCtx, keys[count])
switch {
case errInt == nil:
currentValue.SetEmptySlice()
for k := 0; k < int(*resInt); k++ {
currentValue.Slice().AppendEmpty()
}
currentValue = currentValue.SetEmptyMap().PutEmpty(resString)
currentValue = currentValue.Slice().AppendEmpty()
case errString == nil:
currentValue = currentValue.SetEmptyMap().PutEmpty(*resString)
default:
return errors.New("neither a string nor an int index was given, this is an error in the OTTL")
}
currentValue.SetEmptySlice()
for k := 0; k < int(resInt); k++ {
currentValue.Slice().AppendEmpty()
}
currentValue = currentValue.Slice().AppendEmpty()
}
default:
return fmt.Errorf("type %v does not support string indexing", currentValue.Type())
Expand Down

0 comments on commit 1577fcb

Please sign in to comment.