-
-
Notifications
You must be signed in to change notification settings - Fork 194
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 JSON string comparison. #475
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package strings | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
"unicode/utf8" | ||
) | ||
|
||
// The implementation is taken from TiDB | ||
// https://github.com/pingcap/tidb/blob/a594287e9f402037b06930026906547000006bb6/types/json/binary_functions.go#L89 | ||
func Unquote(s string) (string, error) { | ||
ret := new(bytes.Buffer) | ||
for i := 0; i < len(s); i++ { | ||
if s[i] == '\\' { | ||
i++ | ||
if i == len(s) { | ||
return "", fmt.Errorf("Missing a closing quotation mark in string") | ||
} | ||
switch s[i] { | ||
case '"': | ||
ret.WriteByte('"') | ||
case 'b': | ||
ret.WriteByte('\b') | ||
case 'f': | ||
ret.WriteByte('\f') | ||
case 'n': | ||
ret.WriteByte('\n') | ||
case 'r': | ||
ret.WriteByte('\r') | ||
case 't': | ||
ret.WriteByte('\t') | ||
case '\\': | ||
ret.WriteByte('\\') | ||
case 'u': | ||
if i+4 > len(s) { | ||
return "", fmt.Errorf("Invalid unicode: %s", s[i+1:]) | ||
} | ||
char, size, err := decodeEscapedUnicode([]byte(s[i+1 : i+5])) | ||
if err != nil { | ||
return "", err | ||
} | ||
ret.Write(char[0:size]) | ||
i += 4 | ||
default: | ||
// For all other escape sequences, backslash is ignored. | ||
ret.WriteByte(s[i]) | ||
} | ||
} else { | ||
ret.WriteByte(s[i]) | ||
} | ||
} | ||
|
||
str := ret.String() | ||
strlen := len(str) | ||
// Remove prefix and suffix '"'. | ||
if strlen > 1 { | ||
head, tail := str[0], str[strlen-1] | ||
if head == '"' && tail == '"' { | ||
return str[1 : strlen-1], nil | ||
} | ||
} | ||
return str, nil | ||
} | ||
|
||
// decodeEscapedUnicode decodes unicode into utf8 bytes specified in RFC 3629. | ||
// According RFC 3629, the max length of utf8 characters is 4 bytes. | ||
// And MySQL use 4 bytes to represent the unicode which must be in [0, 65536). | ||
// The implementation is taken from TiDB: | ||
// https://github.com/pingcap/tidb/blob/a594287e9f402037b06930026906547000006bb6/types/json/binary_functions.go#L136 | ||
func decodeEscapedUnicode(s []byte) (char [4]byte, size int, err error) { | ||
size, err = hex.Decode(char[0:2], s) | ||
if err != nil || size != 2 { | ||
// The unicode must can be represented in 2 bytes. | ||
return char, 0, err | ||
} | ||
var unicode uint16 | ||
err = binary.Read(bytes.NewReader(char[0:2]), binary.BigEndian, &unicode) | ||
if err != nil { | ||
return char, 0, err | ||
} | ||
size = utf8.RuneLen(rune(unicode)) | ||
utf8.EncodeRune(char[0:size], rune(unicode)) | ||
return | ||
} |
This file contains 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 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 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
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.
Should this case be handling all JSONValues, not just JSONDocuments?
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.
I just leave as it was, but makes sense. If we use
JSONValue
then we need to remove the following piece of code:tests still pass without it.
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.
If we don't handle arbitrary JSONValues somehow in this function then JSONValues that aren't JSONDocuments won't be supported, right? Are users supposed to be able to use their own implementations of JSONValue?
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.
Changed! Also, it fixes another bug related to JSON null values. They were translated to "" instead of "null"