-
Notifications
You must be signed in to change notification settings - Fork 198
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
Conversation
1126eb6
to
b38db7c
Compare
b38db7c
to
a7b4c1c
Compare
Codecov ReportAttention:
❗ 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. |
rueidiscompat/adapter.go
Outdated
_cmd := c.client.B().JsonSet().Key(key).Path(path).Value(str(value)) | ||
switch mode { | ||
case "XX": | ||
_cmd.Nx() |
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.
_cmd.Nx() | |
_cmd.Xx() |
case "XX": | ||
_cmd.Nx() | ||
case "NX": | ||
_cmd.Xx() |
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.
_cmd.Xx() | |
_cmd.Nx() |
rueidiscompat/command.go
Outdated
err error | ||
val []any | ||
baseCmd[[]any] | ||
// val []any |
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.
// val []any |
rueidiscompat/command.go
Outdated
} | ||
vals := make([]any, len(arr)) | ||
for i, v := range arr { | ||
if isJSONObjKeys { |
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.
It will be better to move if isJSONObjKeys {
to the outside of the loop.
rueidiscompat/command.go
Outdated
if isJSONObjKeys { | ||
// for JSON.OBJKEYS | ||
if v.IsNil() { | ||
vals[i] = nil |
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 is unnecessary, right?
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.
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
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 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() |
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.
arr, err := v.ToAny() | |
vals[i], err = v.ToAny() |
rueidiscompat/command.go
Outdated
msg, err := res.ToMessage() | ||
if err != nil { | ||
if err == rueidis.Nil { | ||
cmd.SetVal("") |
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.
why cmd.SetVal("") when err == rueidis.Nil ?
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'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.
And for JSON.NUMINCRBY
:
JSON.NUMINCRBY
returns a bulk string reply specified as a stringified new value for each path, ornil
, 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:
rueidiscompat/command.go
Outdated
intPtrSlice := make([]*int64, len(arr)) | ||
for i, e := range arr { | ||
if e.IsNil() { | ||
intPtrSlice[i] = nil |
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 also seems unnecessary.
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.
Do you mean intPtrSlice[i] = nil
is unnecessary because element of intPtrSlice has default value nil
?
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.
rueidiscompat/command.go
Outdated
anySlice := make([]any, len(arr)) | ||
for i, e := range arr { | ||
if e.IsNil() { | ||
anySlice[i] = nil |
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 also seems unnecessary.
4122c65
to
92c36fa
Compare
This PR implemented adapter for
go-redis
JSONCmdable
.