-
-
Notifications
You must be signed in to change notification settings - Fork 9
fix: panic in extractKey #25
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -204,27 +204,50 @@ func calculateJSONDiffs(expectedJSON, actualJSON []byte) (string, error) { | |||
| return strings.Join(diffs, "\n"), nil | ||||
| } | ||||
|
|
||||
| // unquoteKey trims surrounding quotes and spaces from a JSON key safely. | ||||
| func unquoteKey(k string) string { | ||||
| k = strings.TrimSpace(k) | ||||
| // Trim both double/single quotes if present first. | ||||
| k = strings.Trim(k, `"'`) | ||||
| // Then, trim any remaining whitespace that was inside the quotes. | ||||
| k = strings.TrimSpace(k) | ||||
| return k | ||||
| } | ||||
|
|
||||
| // extractKey extracts the keys from the diff string. | ||||
| // diffString: The input string representing the differences. | ||||
| // Returns a string containing all the keys separated by a pipe character. | ||||
| // Handles empty lines and lines that don't start with +/- safely. | ||||
| func extractKey(diffString string) string { | ||||
| diffLines := strings.Split(diffString, "\n") // Split the diff string into lines. | ||||
| diffLines := strings.Split(diffString, "\n") | ||||
| var keys []string | ||||
|
|
||||
| // Iterate over each line in the diff string. | ||||
| for _, line := range diffLines { | ||||
| // Remove the leading '-' or '+' and any surrounding spaces | ||||
| line = strings.TrimSpace(line[1:]) | ||||
| for _, raw := range diffLines { | ||||
| line := strings.TrimSpace(raw) | ||||
| if line == "" { | ||||
| continue | ||||
| } | ||||
|
|
||||
| // Remove a single leading +/- if present. | ||||
| if line[0] == '-' || line[0] == '+' { | ||||
| // If it's exactly "-" or "+", skip. | ||||
| if len(line) == 1 { | ||||
| continue | ||||
| } | ||||
| line = strings.TrimSpace(line[1:]) | ||||
| if line == "" { | ||||
| continue | ||||
| } | ||||
| } | ||||
|
|
||||
| if colonIndex := strings.Index(line, ":"); colonIndex != -1 { | ||||
| // Extract and clean up the key | ||||
| key := strings.Trim(line[:colonIndex], `"'`) | ||||
| // Expect `<key>: <value>`; split once. | ||||
| parts := strings.SplitN(line, ":", 2) | ||||
| if len(parts) != 2 { | ||||
| continue | ||||
| } | ||||
| key := unquoteKey(parts[0]) | ||||
| if key != "" { | ||||
| keys = append(keys, key) | ||||
| } | ||||
| // Add the key to the list of keys. | ||||
| } | ||||
|
|
||||
| // Join the keys into a single string separated by a pipe character. | ||||
| return strings.Join(keys, "|") | ||||
| } | ||||
|
|
||||
|
|
@@ -484,30 +507,33 @@ func separateAndColorize(diffStr string, noise map[string][]string) (string, str | |||
| actualTrimmedLine := nextLine[3:] // Trim the '+ ' prefix from the next line. | ||||
| actualKeyValue := strings.SplitN(actualTrimmedLine, ":", 2) | ||||
| actualKey = strings.TrimSpace(actualKeyValue[0]) | ||||
| cleanActualKey := unquoteKey(actualKey) | ||||
| // Process the value | ||||
| value := strings.TrimSpace(actualKeyValue[1]) | ||||
| var jsonObj map[string]interface{} | ||||
| switch { | ||||
| case json.Unmarshal([]byte(value), &jsonObj) == nil: | ||||
| isActualMap = true | ||||
| actualMap = map[string]interface{}{actualKey[:len(actualKey)-1]: jsonObj} | ||||
| actualMap = map[string]interface{}{cleanActualKey: jsonObj} | ||||
| case json.Unmarshal([]byte(value), &actualsArray) == nil: | ||||
| default: | ||||
| actualValue = value | ||||
| } | ||||
| } | ||||
|
|
||||
| if len(strings.SplitN(line[3:], ":", 2)) == 2 { | ||||
| if len(line) > 3 && len(strings.SplitN(line[3:], ":", 2)) == 2 { | ||||
| expectTrimmedLine := line[3:] // Trim the '- ' prefix from the current line. | ||||
| expectkeyValue := strings.SplitN(expectTrimmedLine, ":", 2) | ||||
| expectKey = strings.TrimSpace(expectkeyValue[0]) | ||||
| cleanExpectKey := unquoteKey(expectKey) | ||||
| // Process the value | ||||
| value := strings.TrimSpace(expectkeyValue[1]) | ||||
| var jsonObj map[string]interface{} | ||||
| switch { | ||||
| case json.Unmarshal([]byte(value), &jsonObj) == nil: | ||||
| isExpectMap = true | ||||
| expectMap = map[string]interface{}{expectKey[:len(expectKey)-1]: jsonObj} | ||||
| expectMap = map[string]interface{}{cleanExpectKey: jsonObj} | ||||
|
|
||||
|
Comment on lines
+535
to
+536
|
||||
| expectMap = map[string]interface{}{cleanExpectKey: jsonObj} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can still cause a panic if
lineis an empty string after trimming. The empty string check should be performed before accessingline[0].There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good to have but this will never happen since line 223 handles that