Skip to content

Commit 56bf905

Browse files
committed
feat: make Message fields be private, move cache/proto/queue pkg to root
1 parent 92007d6 commit 56bf905

35 files changed

+1008
-1456
lines changed

client.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"time"
66

77
"github.com/rueian/rueidis/internal/cmds"
8-
"github.com/rueian/rueidis/internal/proto"
98
)
109

1110
type singleClient struct {
@@ -33,13 +32,13 @@ func (c *singleClient) B() *cmds.Builder {
3332
return c.cmd
3433
}
3534

36-
func (c *singleClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
35+
func (c *singleClient) Do(ctx context.Context, cmd cmds.Completed) (resp RedisResult) {
3736
resp = c.conn.Do(cmd)
3837
c.cmd.Put(cmd.CommandSlice())
3938
return resp
4039
}
4140

42-
func (c *singleClient) DoCache(ctx context.Context, cmd cmds.Cacheable, ttl time.Duration) (resp proto.Result) {
41+
func (c *singleClient) DoCache(ctx context.Context, cmd cmds.Cacheable, ttl time.Duration) (resp RedisResult) {
4342
resp = c.conn.DoCache(cmd, ttl)
4443
c.cmd.Put(cmd.CommandSlice())
4544
return resp
@@ -65,13 +64,13 @@ func (c *dedicatedSingleClient) B() *cmds.Builder {
6564
return c.cmd
6665
}
6766

68-
func (c *dedicatedSingleClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
67+
func (c *dedicatedSingleClient) Do(ctx context.Context, cmd cmds.Completed) (resp RedisResult) {
6968
resp = c.wire.Do(cmd)
7069
c.cmd.Put(cmd.CommandSlice())
7170
return resp
7271
}
7372

74-
func (c *dedicatedSingleClient) DoMulti(ctx context.Context, multi ...cmds.Completed) (resp []proto.Result) {
73+
func (c *dedicatedSingleClient) DoMulti(ctx context.Context, multi ...cmds.Completed) (resp []RedisResult) {
7574
if len(multi) == 0 {
7675
return nil
7776
}

client_test.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import (
88
"time"
99

1010
"github.com/rueian/rueidis/internal/cmds"
11-
"github.com/rueian/rueidis/internal/mock"
12-
"github.com/rueian/rueidis/internal/proto"
1311
)
1412

1513
type mockConn struct {
16-
DoFn func(cmd cmds.Completed) proto.Result
17-
DoCacheFn func(cmd cmds.Cacheable, ttl time.Duration) proto.Result
18-
DoMultiFn func(multi ...cmds.Completed) []proto.Result
19-
InfoFn func() map[string]proto.Message
14+
DoFn func(cmd cmds.Completed) RedisResult
15+
DoCacheFn func(cmd cmds.Cacheable, ttl time.Duration) RedisResult
16+
DoMultiFn func(multi ...cmds.Completed) []RedisResult
17+
InfoFn func() map[string]RedisMessage
2018
ErrorFn func() error
2119
CloseFn func()
2220
DialFn func() error
@@ -46,28 +44,28 @@ func (m *mockConn) Store(w wire) {
4644
}
4745
}
4846

49-
func (m *mockConn) Do(cmd cmds.Completed) proto.Result {
47+
func (m *mockConn) Do(cmd cmds.Completed) RedisResult {
5048
if m.DoFn != nil {
5149
return m.DoFn(cmd)
5250
}
53-
return proto.Result{}
51+
return RedisResult{}
5452
}
5553

56-
func (m *mockConn) DoCache(cmd cmds.Cacheable, ttl time.Duration) proto.Result {
54+
func (m *mockConn) DoCache(cmd cmds.Cacheable, ttl time.Duration) RedisResult {
5755
if m.DoCacheFn != nil {
5856
return m.DoCacheFn(cmd, ttl)
5957
}
60-
return proto.Result{}
58+
return RedisResult{}
6159
}
6260

63-
func (m *mockConn) DoMulti(multi ...cmds.Completed) []proto.Result {
61+
func (m *mockConn) DoMulti(multi ...cmds.Completed) []RedisResult {
6462
if m.DoMultiFn != nil {
6563
return m.DoMultiFn(multi...)
6664
}
6765
return nil
6866
}
6967

70-
func (m *mockConn) Info() map[string]proto.Message {
68+
func (m *mockConn) Info() map[string]RedisMessage {
7169
if m.InfoFn != nil {
7270
return m.InfoFn()
7371
}
@@ -125,11 +123,11 @@ func TestSingleClient(t *testing.T) {
125123

126124
t.Run("Delegate Do", func(t *testing.T) {
127125
c := client.B().Get().Key("Do").Build()
128-
m.DoFn = func(cmd cmds.Completed) proto.Result {
126+
m.DoFn = func(cmd cmds.Completed) RedisResult {
129127
if !reflect.DeepEqual(cmd.Commands(), c.Commands()) {
130128
t.Fatalf("unexpected command %v", cmd)
131129
}
132-
return proto.NewResult(proto.Message{Type: '+', String: "Do"}, nil)
130+
return newResult(RedisMessage{typ: '+', string: "Do"}, nil)
133131
}
134132
if v, err := client.Do(context.Background(), c).ToString(); err != nil || v != "Do" {
135133
t.Fatalf("unexpected response %v %v", v, err)
@@ -138,11 +136,11 @@ func TestSingleClient(t *testing.T) {
138136

139137
t.Run("Delegate DoCache", func(t *testing.T) {
140138
c := client.B().Get().Key("DoCache").Cache()
141-
m.DoCacheFn = func(cmd cmds.Cacheable, ttl time.Duration) proto.Result {
139+
m.DoCacheFn = func(cmd cmds.Cacheable, ttl time.Duration) RedisResult {
142140
if !reflect.DeepEqual(cmd.Commands(), c.Commands()) || ttl != 100 {
143141
t.Fatalf("unexpected command %v, %v", cmd, ttl)
144142
}
145-
return proto.NewResult(proto.Message{Type: '+', String: "DoCache"}, nil)
143+
return newResult(RedisMessage{typ: '+', string: "DoCache"}, nil)
146144
}
147145
if v, err := client.DoCache(context.Background(), c, 100).ToString(); err != nil || v != "DoCache" {
148146
t.Fatalf("unexpected response %v %v", v, err)
@@ -168,12 +166,12 @@ func TestSingleClient(t *testing.T) {
168166
})
169167

170168
t.Run("Dedicated Delegate", func(t *testing.T) {
171-
w := &mock.Wire{
172-
DoFn: func(cmd cmds.Completed) proto.Result {
173-
return proto.NewResult(proto.Message{Type: '+', String: "Delegate"}, nil)
169+
w := &mockWire{
170+
DoFn: func(cmd cmds.Completed) RedisResult {
171+
return newResult(RedisMessage{typ: '+', string: "Delegate"}, nil)
174172
},
175-
DoMultiFn: func(cmd ...cmds.Completed) []proto.Result {
176-
return []proto.Result{proto.NewResult(proto.Message{Type: '+', String: "Delegate"}, nil)}
173+
DoMultiFn: func(cmd ...cmds.Completed) []RedisResult {
174+
return []RedisResult{newResult(RedisMessage{typ: '+', string: "Delegate"}, nil)}
177175
},
178176
}
179177
m.AcquireFn = func() wire {

cluster.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/rueian/rueidis/internal/cmds"
13-
"github.com/rueian/rueidis/internal/proto"
1413
)
1514

1615
var ErrNoSlot = errors.New("the slot has no redis node")
@@ -84,7 +83,7 @@ func (c *clusterClient) refresh() (err error) {
8483
}
8584

8685
func (c *clusterClient) _refresh() (err error) {
87-
var reply proto.Message
86+
var reply RedisMessage
8887
var dead []string
8988

9089
retry:
@@ -114,7 +113,7 @@ retry:
114113
return err
115114
}
116115

117-
if len(reply.Values) == 0 {
116+
if len(reply.values) == 0 {
118117
if _, err = c.init(); err != nil {
119118
return err
120119
}
@@ -177,20 +176,20 @@ type group struct {
177176
slots [][2]int64
178177
}
179178

180-
func parseSlots(slots proto.Message) map[string]group {
181-
groups := make(map[string]group, len(slots.Values))
182-
for _, v := range slots.Values {
183-
master := fmt.Sprintf("%s:%d", v.Values[2].Values[0].String, v.Values[2].Values[1].Integer)
179+
func parseSlots(slots RedisMessage) map[string]group {
180+
groups := make(map[string]group, len(slots.values))
181+
for _, v := range slots.values {
182+
master := fmt.Sprintf("%s:%d", v.values[2].values[0].string, v.values[2].values[1].integer)
184183
g, ok := groups[master]
185184
if !ok {
186185
g.slots = make([][2]int64, 0)
187-
g.nodes = make([]string, 0, len(v.Values)-2)
188-
for i := 2; i < len(v.Values); i++ {
189-
dst := fmt.Sprintf("%s:%d", v.Values[i].Values[0].String, v.Values[i].Values[1].Integer)
186+
g.nodes = make([]string, 0, len(v.values)-2)
187+
for i := 2; i < len(v.values); i++ {
188+
dst := fmt.Sprintf("%s:%d", v.values[i].values[0].string, v.values[i].values[1].integer)
190189
g.nodes = append(g.nodes, dst)
191190
}
192191
}
193-
g.slots = append(g.slots, [2]int64{v.Values[0].Integer, v.Values[1].Integer})
192+
g.slots = append(g.slots, [2]int64{v.values[0].integer, v.values[1].integer})
194193
groups[master] = g
195194
}
196195
return groups
@@ -242,11 +241,11 @@ func (c *clusterClient) B() *cmds.Builder {
242241
return c.cmd
243242
}
244243

245-
func (c *clusterClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
244+
func (c *clusterClient) Do(ctx context.Context, cmd cmds.Completed) (resp RedisResult) {
246245
retry:
247246
cc, err := c.pick(cmd.Slot())
248247
if err != nil {
249-
resp = proto.NewErrResult(err)
248+
resp = newErrResult(err)
250249
goto ret
251250
}
252251
resp = cc.Do(cmd)
@@ -269,11 +268,11 @@ ret:
269268
return resp
270269
}
271270

272-
func (c *clusterClient) DoCache(ctx context.Context, cmd cmds.Cacheable, ttl time.Duration) (resp proto.Result) {
271+
func (c *clusterClient) DoCache(ctx context.Context, cmd cmds.Cacheable, ttl time.Duration) (resp RedisResult) {
273272
retry:
274273
cc, err := c.pick(cmd.Slot())
275274
if err != nil {
276-
resp = proto.NewErrResult(err)
275+
resp = newErrResult(err)
277276
goto ret
278277
}
279278
resp = cc.DoCache(cmd, ttl)
@@ -355,18 +354,18 @@ func (c *dedicatedClusterClient) B() *cmds.Builder {
355354
return c.cmd
356355
}
357356

358-
func (c *dedicatedClusterClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
357+
func (c *dedicatedClusterClient) Do(ctx context.Context, cmd cmds.Completed) (resp RedisResult) {
359358
c.check(cmd.Slot())
360359
if err := c.acquire(); err != nil {
361-
return proto.NewErrResult(err)
360+
return newErrResult(err)
362361
} else {
363362
resp = c.wire.Do(cmd)
364363
}
365364
c.cmd.Put(cmd.CommandSlice())
366365
return resp
367366
}
368367

369-
func (c *dedicatedClusterClient) DoMulti(ctx context.Context, multi ...cmds.Completed) (resp []proto.Result) {
368+
func (c *dedicatedClusterClient) DoMulti(ctx context.Context, multi ...cmds.Completed) (resp []RedisResult) {
370369
if len(multi) == 0 {
371370
return nil
372371
}
@@ -376,9 +375,9 @@ func (c *dedicatedClusterClient) DoMulti(ctx context.Context, multi ...cmds.Comp
376375
if err := c.acquire(); err == nil {
377376
resp = c.wire.DoMulti(multi...)
378377
} else {
379-
resp = make([]proto.Result, len(multi))
378+
resp = make([]RedisResult, len(multi))
380379
for i := range resp {
381-
resp[i] = proto.NewErrResult(err)
380+
resp[i] = newErrResult(err)
382381
}
383382
}
384383
for _, cmd := range multi {

0 commit comments

Comments
 (0)