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

Fix empty array index lookup #6

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

chaudum
Copy link

@chaudum chaudum commented Oct 4, 2024

Description: What this PR does

This PR fixes a panic that occurs when the array index is an empty string, as in

{"foo": {"keys": ["a", "b", "c"]}}
// lookup foo.keys[""] instead of foo.keys[1]

Signed-off-by: Christian Haudum <[email protected]>
Copy link

@trevorwhitney trevorwhitney left a comment

Choose a reason for hiding this comment

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

looks good, just a couple questions about an array with empty string, ie [""], thanks!

{"Empty field array", args{[]byte("{\"data\": []}"), funcError, []string{"data"}}, 10, false},
{"Empty field array with space", args{[]byte("{\"data\": [ ]}"), funcError, []string{"data"}}, 11, false},
{"Empty field array with \n", args{[]byte("{\"data\": [\n]}"), funcError, []string{"data"}}, 11, false},
{"Empty field array with newline", args{[]byte("{\"data\": [\n]}"), funcError, []string{"data"}}, 11, false},

Choose a reason for hiding this comment

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

do we need a test for array with an empty string, ie `[]byte("{"data": [""]}"

Copy link
Author

Choose a reason for hiding this comment

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

As I understood, this is just a test to iterate over an array, not accessing an element by index in that array. The API of this library is rather confusing tbh.

path: []string{"arrString", "[1]"},
expected: "b",
}, {
path: []string{"arrString", "[]"}, // [] is equal to [0]

Choose a reason for hiding this comment

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

again, do we need []string{"arrString", "[\"\"]"}, or maybe that case works?

Copy link
Author

Choose a reason for hiding this comment

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

[]string{"keys", "[]"}

is what Loki produces when using this parser expression:

| json key[""]

[]string{"keys", "[\"\"]"}

would be equivalent to any other invalid, non-integer array index

@chaudum chaudum merged commit 0233299 into master Oct 4, 2024
1 check passed
@chaudum chaudum deleted the chaudum/fix-empty-array-index-lookup branch October 4, 2024 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants