Skip to content

Commit c828764

Browse files
splundidIlia Personalofekshenawa
authored
Allow scanning redis values into pointer fields (#2787)
* Allow scanning redis values into pointer fields * Formatting --------- Co-authored-by: Ilia Personal <[email protected]> Co-authored-by: ofekshenawa <[email protected]>
1 parent 716906a commit c828764

File tree

5 files changed

+14
-22
lines changed

5 files changed

+14
-22
lines changed

command.go

-1
Original file line numberDiff line numberDiff line change
@@ -5378,7 +5378,6 @@ func (cmd *InfoCmd) readReply(rd *proto.Reader) error {
53785378
}
53795379

53805380
return nil
5381-
53825381
}
53835382

53845383
func (cmd *InfoCmd) Item(section, key string) string {

internal/hscan/hscan_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
. "github.com/bsm/ginkgo/v2"
1010
. "github.com/bsm/gomega"
11+
12+
"github.com/redis/go-redis/v9/internal/util"
1113
)
1214

1315
type data struct {
@@ -29,6 +31,7 @@ type data struct {
2931
Float float32 `redis:"float"`
3032
Float64 float64 `redis:"float64"`
3133
Bool bool `redis:"bool"`
34+
BoolRef *bool `redis:"boolRef"`
3235
}
3336

3437
type TimeRFC3339Nano struct {
@@ -117,10 +120,10 @@ var _ = Describe("Scan", func() {
117120
Expect(Scan(&d, i{"key"}, i{"value"})).NotTo(HaveOccurred())
118121
Expect(d).To(Equal(data{}))
119122

120-
keys := i{"string", "byte", "int", "int64", "uint", "uint64", "float", "float64", "bool"}
123+
keys := i{"string", "byte", "int", "int64", "uint", "uint64", "float", "float64", "bool", "boolRef"}
121124
vals := i{
122125
"str!", "bytes!", "123", "123456789123456789", "456", "987654321987654321",
123-
"123.456", "123456789123456789.987654321987654321", "1",
126+
"123.456", "123456789123456789.987654321987654321", "1", "1",
124127
}
125128
Expect(Scan(&d, keys, vals)).NotTo(HaveOccurred())
126129
Expect(d).To(Equal(data{
@@ -133,6 +136,7 @@ var _ = Describe("Scan", func() {
133136
Float: 123.456,
134137
Float64: 1.2345678912345678e+17,
135138
Bool: true,
139+
BoolRef: util.ToPtr(true),
136140
}))
137141

138142
// Scan a different type with the same values to test that
@@ -167,6 +171,7 @@ var _ = Describe("Scan", func() {
167171
Float: 1.0,
168172
Float64: 1.2345678912345678e+17,
169173
Bool: true,
174+
BoolRef: util.ToPtr(true),
170175
}))
171176
})
172177

internal/hscan/structmap.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ func newStructSpec(t reflect.Type, fieldTag string) *structSpec {
6161
}
6262

6363
// Use the built-in decoder.
64-
out.set(tag, &structField{index: i, fn: decoders[f.Type.Kind()]})
64+
kind := f.Type.Kind()
65+
if kind == reflect.Pointer {
66+
kind = f.Type.Elem().Kind()
67+
}
68+
out.set(tag, &structField{index: i, fn: decoders[kind]})
6569
}
6670

6771
return out

json.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ type JSONCmd struct {
6666
var _ Cmder = (*JSONCmd)(nil)
6767

6868
func newJSONCmd(ctx context.Context, args ...interface{}) *JSONCmd {
69-
7069
return &JSONCmd{
7170
baseCmd: baseCmd{
7271
ctx: ctx,
@@ -95,15 +94,13 @@ func (cmd *JSONCmd) Val() string {
9594
} else {
9695
return cmd.val
9796
}
98-
9997
}
10098

10199
func (cmd *JSONCmd) Result() (string, error) {
102100
return cmd.Val(), cmd.Err()
103101
}
104102

105103
func (cmd JSONCmd) Expanded() (interface{}, error) {
106-
107104
if len(cmd.val) != 0 && cmd.expanded == nil {
108105
err := json.Unmarshal([]byte(cmd.val), &cmd.expanded)
109106
if err != nil {
@@ -115,7 +112,6 @@ func (cmd JSONCmd) Expanded() (interface{}, error) {
115112
}
116113

117114
func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
118-
119115
// nil response from JSON.(M)GET (cmd.baseCmd.err will be "redis: nil")
120116
if cmd.baseCmd.Err() == Nil {
121117
cmd.val = ""
@@ -131,7 +127,7 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
131127
return err
132128
}
133129

134-
var expanded = make([]interface{}, size)
130+
expanded := make([]interface{}, size)
135131

136132
for i := 0; i < size; i++ {
137133
if expanded[i], err = rd.ReadReply(); err != nil {
@@ -186,7 +182,6 @@ func (cmd *JSONSliceCmd) Result() ([]interface{}, error) {
186182
}
187183

188184
func (cmd *JSONSliceCmd) readReply(rd *proto.Reader) error {
189-
190185
if cmd.baseCmd.Err() == Nil {
191186
cmd.val = nil
192187
return Nil
@@ -220,7 +215,6 @@ func (cmd *JSONSliceCmd) readReply(rd *proto.Reader) error {
220215
}
221216
}
222217
return nil
223-
224218
}
225219

226220
/*******************************************************************************
@@ -262,7 +256,6 @@ func (cmd *IntPointerSliceCmd) Result() ([]*int64, error) {
262256
}
263257

264258
func (cmd *IntPointerSliceCmd) readReply(rd *proto.Reader) error {
265-
266259
n, err := rd.ReadArrayLen()
267260
if err != nil {
268261
return err

json_test.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
. "github.com/bsm/ginkgo/v2"
77
. "github.com/bsm/gomega"
8+
89
"github.com/redis/go-redis/v9"
910
)
1011

@@ -13,7 +14,6 @@ type JSONGetTestStruct struct {
1314
}
1415

1516
var _ = Describe("JSON Commands", Label("json"), func() {
16-
1717
ctx := context.TODO()
1818
var client *redis.Client
1919

@@ -27,7 +27,6 @@ var _ = Describe("JSON Commands", Label("json"), func() {
2727
})
2828

2929
Describe("arrays", Label("arrays"), func() {
30-
3130
It("should JSONArrAppend", Label("json.arrappend", "json"), func() {
3231
cmd1 := client.JSONSet(ctx, "append2", "$", `{"a": [10], "b": {"a": [12, 13]}}`)
3332
Expect(cmd1.Err()).NotTo(HaveOccurred())
@@ -76,7 +75,6 @@ var _ = Describe("JSON Commands", Label("json"), func() {
7675
res, err = client.JSONArrIndexWithArgs(ctx, "index2", "$", &redis.JSONArrIndexArgs{Stop: &stop}, 4).Result()
7776
Expect(err).NotTo(HaveOccurred())
7877
Expect(res[0]).To(Equal(int64(-1)))
79-
8078
})
8179

8280
It("should JSONArrIndex and JSONArrIndexWithArgs with $", Label("json.arrindex", "json"), func() {
@@ -235,7 +233,6 @@ var _ = Describe("JSON Commands", Label("json"), func() {
235233
Expect(cmd3.Err()).NotTo(HaveOccurred())
236234
Expect(cmd3.Val()).To(Equal("[[100,200,200]]"))
237235
})
238-
239236
})
240237

241238
Describe("get/set", Label("getset"), func() {
@@ -257,7 +254,6 @@ var _ = Describe("JSON Commands", Label("json"), func() {
257254
res, err = client.JSONGetWithArgs(ctx, "get3", &redis.JSONGetArgs{Indent: "-", Newline: `~`, Space: `!`}).Result()
258255
Expect(err).NotTo(HaveOccurred())
259256
Expect(res).To(Equal(`[~-{~--"a":!1,~--"b":!2~-}~]`))
260-
261257
})
262258

263259
It("should JSONMerge", Label("json.merge", "json"), func() {
@@ -330,13 +326,10 @@ var _ = Describe("JSON Commands", Label("json"), func() {
330326
iRes, err = client.JSONMGet(ctx, "$..a", "non_existing_doc", "non_existing_doc1").Result()
331327
Expect(err).NotTo(HaveOccurred())
332328
Expect(iRes).To(Equal([]interface{}{nil, nil}))
333-
334329
})
335-
336330
})
337331

338332
Describe("Misc", Label("misc"), func() {
339-
340333
It("should JSONClear", Label("json.clear", "json"), func() {
341334
cmd1 := client.JSONSet(ctx, "clear1", "$", `[1]`)
342335
Expect(cmd1.Err()).NotTo(HaveOccurred())
@@ -460,7 +453,6 @@ var _ = Describe("JSON Commands", Label("json"), func() {
460453
cmd3 := client.JSONGet(ctx, "forget3", "$")
461454
Expect(cmd3.Err()).NotTo(HaveOccurred())
462455
Expect(cmd3.Val()).To(Equal(`[{"b":{"b":"annie"}}]`))
463-
464456
})
465457

466458
It("should JSONForget with $", Label("json.forget", "json"), func() {
@@ -622,7 +614,6 @@ var _ = Describe("JSON Commands", Label("json"), func() {
622614
cmd3, err := client.JSONGet(ctx, "strapp1", "$").Result()
623615
Expect(err).NotTo(HaveOccurred())
624616
Expect(cmd3).To(Equal(`["foobar"]`))
625-
626617
})
627618

628619
It("should JSONStrAppend and JSONStrLen with $", Label("json.strappend", "json.strlen", "json"), func() {

0 commit comments

Comments
 (0)