Skip to content

Fix empty array index lookup - #6

Merged
chaudum merged 1 commit into
masterfrom
chaudum/fix-empty-array-index-lookup
Oct 4, 2024
Merged

Fix empty array index lookup#6
chaudum merged 1 commit into
masterfrom
chaudum/fix-empty-array-index-lookup

Conversation

@chaudum

@chaudum chaudum commented Oct 4, 2024

Copy link
Copy Markdown

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 <christian.haudum@gmail.com>

@trevorwhitney trevorwhitney left a comment

Copy link
Copy Markdown

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!

Comment thread parser_test.go
{"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},

Copy link
Copy Markdown

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
Copy Markdown
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.

Comment thread parser_test.go
path: []string{"arrString", "[1]"},
expected: "b",
}, {
path: []string{"arrString", "[]"}, // [] is equal to [0]

Copy link
Copy Markdown

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
Copy Markdown
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
@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

2 participants