Skip to content

Feat: Align go-redis JSONCmdable #459

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 6 commits into from
Jan 28, 2024

Conversation

unknowntpo
Copy link
Contributor

This PR implemented adapter for go-redis JSONCmdable.

@unknowntpo unknowntpo force-pushed the feat-align-go-redis-json branch 2 times, most recently from 1126eb6 to b38db7c Compare January 27, 2024 11:18
@unknowntpo unknowntpo force-pushed the feat-align-go-redis-json branch from b38db7c to a7b4c1c Compare January 27, 2024 11:20
@codecov-commenter
Copy link

codecov-commenter commented Jan 27, 2024

Codecov Report

Attention: 111 lines in your changes are missing coverage. Please review.

Comparison is base (222f9ed) 95.99% compared to head (92c36fa) 95.69%.
Report is 44 commits behind head on main.

Files Patch % Lines
rueidiscompat/adapter.go 63.54% 66 Missing and 4 partials ⚠️
rueidiscompat/command.go 66.11% 33 Missing and 8 partials ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #459      +/-   ##
==========================================
- Coverage   95.99%   95.69%   -0.31%     
==========================================
  Files          77       77              
  Lines       32392    32685     +293     
==========================================
+ Hits        31095    31278     +183     
- Misses       1106     1204      +98     
- Partials      191      203      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

_cmd := c.client.B().JsonSet().Key(key).Path(path).Value(str(value))
switch mode {
case "XX":
_cmd.Nx()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
_cmd.Nx()
_cmd.Xx()

case "XX":
_cmd.Nx()
case "NX":
_cmd.Xx()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
_cmd.Xx()
_cmd.Nx()

err error
val []any
baseCmd[[]any]
// val []any
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// val []any

}
vals := make([]any, len(arr))
for i, v := range arr {
if isJSONObjKeys {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It will be better to move if isJSONObjKeys { to the outside of the loop.

if isJSONObjKeys {
// for JSON.OBJKEYS
if v.IsNil() {
vals[i] = nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is unnecessary, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's necessary because the return value might be Nil

e.g.

redis> JSON.SET doc $ '{"a":[3], "nested": {"a": {"b":2, "c": 1}}}'
OK
redis> JSON.OBJKEYS doc $..a
1) (nil)
2) 1) "b"
   2) "c"

Ref: JSON.OBJKEYS

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get your point, vals[i] is nil by default, no need to set it, I'll fix that.

continue
}
// convert to any which underlying type is []any
arr, err := v.ToAny()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
arr, err := v.ToAny()
vals[i], err = v.ToAny()

msg, err := res.ToMessage()
if err != nil {
if err == rueidis.Nil {
cmd.SetVal("")
Copy link
Collaborator

Choose a reason for hiding this comment

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

why cmd.SetVal("") when err == rueidis.Nil ?

Copy link
Contributor Author

@unknowntpo unknowntpo Jan 28, 2024

Choose a reason for hiding this comment

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

I'm wrong.
If we delete everything from this key, and call JSON.GET with path $, we would get nil.
So, for JSON.GET, if the error is rueidis.Nil, we should just return it.

json.set del1 $ '{"a": 1, "nested": {"a": 2, "b": 3}}'
OK
127.0.0.1:6378> json.get del1
"{\"a\":1,\"nested\":{\"a\":2,\"b\":3}}"
127.0.0.1:6378> json.del del1 $
(integer) 1
127.0.0.1:6378> json.get del1 $
(nil)

the tests in go-redis is wrong.

https://github.com/redis/rueidis/pull/459/files#diff-8e5958b5f75f88e93f6235ef6011f699baa79bb7f32da3c9f9bf2f3507c4ce1aR10818

And for JSON.NUMINCRBY:

JSON.NUMINCRBY returns a bulk string reply specified as a stringified new value for each path, or nil, if the matching JSON value is not a number.

According to the documentation, the array element in reply of JSON.NUMINCRBY will be rueidis.Nil if matching JSON value is not a number, and when it's nil, we should set it to nil.

e.g.

redis> JSON.SET doc . '{"a":"b","b":[{"a":2}, {"a":5}, {"a":"c"}]}'
OK
redis> JSON.NUMINCRBY doc $..a 2
"[null,4,7,null]"

Ref:

intPtrSlice := make([]*int64, len(arr))
for i, e := range arr {
if e.IsNil() {
intPtrSlice[i] = nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

This also seems unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean intPtrSlice[i] = nil is unnecessary because element of intPtrSlice has default value nil ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes.

anySlice := make([]any, len(arr))
for i, e := range arr {
if e.IsNil() {
anySlice[i] = nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

This also seems unnecessary.

@rueian rueian added the feature label Jan 27, 2024
@unknowntpo unknowntpo force-pushed the feat-align-go-redis-json branch from 4122c65 to 92c36fa Compare January 28, 2024 09:48
@rueian rueian merged commit ffeb7c7 into redis:main Jan 28, 2024
@unknowntpo unknowntpo deleted the feat-align-go-redis-json branch January 29, 2024 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants