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

#502 Added support for JSON.STRLEN #545

Merged
merged 4 commits into from
Sep 13, 2024

Conversation

apoorvyadav1111
Copy link
Contributor

PR addresses #502

The changes that are included in the PR.

Added support for JSON.STRLEN. added unit tests and integration tests

All tests passed.

@apoorvyadav1111
Copy link
Contributor Author

Hi @JyotinderSingh, PR is ready for review. Please share your feedback.

Comment on lines +2633 to +2724
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)
}
Copy link
Contributor

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

Copy link
Contributor Author

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:
image
in redis:
image

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

@JyotinderSingh JyotinderSingh left a 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.

Comment on lines +2714 to +2724
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)
}
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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

`

Copy link
Contributor Author

@apoorvyadav1111 apoorvyadav1111 Sep 13, 2024

Choose a reason for hiding this comment

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

Hi, please check these scenarios. My bad, I did not format the cases correctly in the comment.
image
image

Copy link
Contributor

Choose a reason for hiding this comment

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

got it. thanks

Copy link
Collaborator

@JyotinderSingh JyotinderSingh left a 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.

@JyotinderSingh JyotinderSingh merged commit f362799 into DiceDB:master Sep 13, 2024
2 checks passed
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.

4 participants