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] Support dynamic indexing #36719

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

odubajDT
Copy link
Contributor

@odubajDT odubajDT commented Dec 9, 2024

Description

Link to tracking issue

Fixes #36644

@odubajDT odubajDT force-pushed the indexing-pkg-ottl branch 5 times, most recently from 1577fcb to 87f2d33 Compare December 18, 2024 10:06
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Signed-off-by: odubajDT <[email protected]>
Copy link
Contributor

@evan-bradley evan-bradley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this one on @odubajDT. I have a few questions on the implementation, but based on the tests passing, I think we're definitely on the right track.

}

var _ Key[any] = &baseKey[any]{}

type baseKey[K any] struct {
s *string
i *int64
p Getter[K]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is minor, but could we make the field name g or something more representative of its type?

@@ -8,12 +8,12 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we revert this change? I think your editor may have done this automatically.

@@ -46,6 +46,7 @@ var _ ottl.Key[any] = &TestKey[any]{}
type TestKey[K any] struct {
S *string
I *int64
P ottl.Getter[K]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
P ottl.Getter[K]
G ottl.Getter[K]

Same here, I think a G for Getter makes the most sense.

}
resVal, ok := res.(T)
if !ok {
return nil, fmt.Errorf("casting not successful")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("casting not successful")
return nil, fmt.Errorf("could not resolve key for map/slice, expecting `int64` or `string` but got %T", res)

It would be nice to make this more explicit what's happening here, if a user ever hits this error we should try to give them all the relevant information we have. If possible, it would also be nice to print the type of T here so we can make it clear what type of key was expected.

return nil, fmt.Errorf("non-integer indexing is not supported")
resInt, err := FetchValueFromExpression[K, int64](ctx, tCtx, keys[0])
if err != nil {
return nil, fmt.Errorf("non-integer indexing is not supported")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("non-integer indexing is not supported")
return nil, fmt.Errorf("unable to resolve an integer index: %w", err)

I think at a minimum we should wrap the underlying error, but we should also consider rewording this message, since I'm not sure a failure in FetchValueFromExpression necessarily means the user tried to use a non-integer value to index a slice. For example, they could have tried to retrieve attributes["my_int"] and the attribute was nil.

@@ -71,27 +71,35 @@ func SetValue(value pcommon.Value, val any) error {
func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value, keys []ottl.Key[K]) (any, error) {
val := value
var ok bool
for i := 0; i < len(keys); i++ {
for count := 0; count < len(keys); count++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does count represent here? To me the conventional i makes more sense, but I could be missing your intent. count strikes me as more of an accumulated/computed value.

Comment on lines +183 to +184
resString, errString := FetchValueFromExpression[K, string](ctx, tCtx, keys[count])
resInt, errInt := FetchValueFromExpression[K, int64](ctx, tCtx, keys[count])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to take these out of this default case and consider all four cases inside a single switch statement?

Comment on lines +236 to +241
if f := keys[i].Expression.Float; f != nil {
par = literal[K]{value: *f}
}
if i := keys[i].Expression.Int; i != nil {
par = literal[K]{value: *i}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will these two cases be hit? Would it simplify things if we refactored the grammar to just have keys be a mathExprLiteral type instead of a key type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pkg/ottl] Allow indexing maps and slices with dynamic values
3 participants