-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
#502 Added support for JSON.STRLEN
#545
#502 Added support for JSON.STRLEN
#545
Conversation
Hi @JyotinderSingh, PR is ready for review. Please share your feedback. |
if path == defaultRootPath { | ||
defaultStringResult := make([]interface{}, 0, 1) | ||
if utils.GetJSONFieldType(jsonData) == utils.StringType { | ||
defaultStringResult = append(defaultStringResult, int64(len(jsonData.(string)))) | ||
} else { | ||
defaultStringResult = append(defaultStringResult, clientio.RespNIL) | ||
} | ||
|
||
return clientio.Encode(defaultStringResult, false) | ||
} |
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.
thanks for your pr. I have a tiny comment. in redis. if the rootpath is not provided, the return msg should like below:
`
127.0.0.1:6379> JSON.SET doc $ '{"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}'
OK
127.0.0.1:6379> JSON.STRLEN doc $
1) (nil)
127.0.0.1:6379> JSON.STRLEN doc
(error) WRONGTYPE wrong type of path value - expected string but found object
`
so I think there is difference between if the path is provided
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.
Hi @jujiale , The parser we use returns different results from what is expected for paths not starting with $. I have been trying to get the same response as redis for different cases but found out that we need to change the parser if we have to be consistent. I checked the JSON.ARRLEN for the same behaviour as follows:
In dice, the result is an array hence we return array of results. In redis, if there is only one result, the logic only and only then checks for type of the path and throws the WRONGTYPE error. If the type error happens during an array it returns a nil.
Hey @JyotinderSingh, Would like to get your feedback too on this.
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.
Hi, I have tried to keep the behavior in sync with arrlen and pushed new changes with new tests
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.
yes. In redis. different path have different returns. I think we should consider it if use the same logic for return.
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.
For now, I have handled the case just like arrlen.
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.
Thanks for adding this command @apoorvyadav1111. Please address @jujiale's comments.
b05f84d
to
fa5361a
Compare
3b1611d
to
15c1a09
Compare
jsonData := obj.Value | ||
if path == defaultRootPath { | ||
defaultStringResult := make([]interface{}, 0, 1) | ||
if utils.GetJSONFieldType(jsonData) == utils.StringType { | ||
defaultStringResult = append(defaultStringResult, int64(len(jsonData.(string)))) | ||
} else { | ||
defaultStringResult = append(defaultStringResult, clientio.RespNIL) | ||
} | ||
|
||
return clientio.Encode(defaultStringResult, false) | ||
} |
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.
a tiny question. in this logic. if path == defaultRootPath . should the jsonData not be a string? because we have made judge in AssertTypeAndEncoding
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.
In AssertTypeAndEncoding, we get no error if the root value is json, which can be {a:1} or '"hello"'. If its the first case, then the defaultpath must return nil, but if its the latter it should return the length. Hence this is the reason I am doing a check here
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.
in which scenario. the rootpath value could be {a:1} or '"hello"
`
127.0.0.1:6379> JSON.SET user3 $ '{a:1}'
(error) Existing key has wrong Redis type
127.0.0.1:6379> SET user3 '{a:1}'
OK
127.0.0.1:6379> JSON.STRLEN user3
(error) Existing key has wrong Redis type
127.0.0.1:6379> JSON.STRLEN user3 $
(error) Existing key has wrong Redis type
127.0.0.1:6379> JSON.SET user3 $ "hello"
(error) Existing key has wrong Redis type
127.0.0.1:6379> set user3 "hello"
OK
127.0.0.1:6379> JSON.STRLEN user3
(error) Existing key has wrong Redis type
127.0.0.1:6379> JSON.STRLEN user3 $
(error) Existing key has wrong Redis type
`
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.
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.
got it. thanks
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.
Thanks @apoorvyadav1111 for adding support for JSON.STRLEN!
Thanks @jujiale for the comprehensive reviews.
Merging.
PR addresses #502
The changes that are included in the PR.
Added support for
JSON.STRLEN
. added unit tests and integration testsAll tests passed.