Skip to content

Commit 2bf4298

Browse files
committed
💬 Raw methods accepts []any
1 parent 1aad069 commit 2bf4298

6 files changed

+25
-13
lines changed

‎conn.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ func NewConnAuth(remote *net.TCPAddr, authProvider AuthProvider) (*Conn, error)
174174
return conn, nil
175175
}
176176

177-
func (c *Conn) BuildSingleActionPacketRaw(segs []string) (raw string, err error) {
177+
// Allows building a packet easily like:
178+
// c.BuildSingleActionPacketRaw("SET", "X", 100)
179+
//
180+
// The arguments accept any type. The arguments are formatted internally with %v so most basic types should be supported.
181+
func (c *Conn) BuildSingleActionPacketRaw(segs []any) (raw string, err error) {
178182
c.strBuilder.Reset()
179183
_, err = fmt.Fprint(c.strBuilder, "*1\n")
180184
if err != nil {
@@ -189,14 +193,15 @@ func (c *Conn) BuildSingleActionPacketRaw(segs []string) (raw string, err error)
189193
return c.strBuilder.String(), nil
190194
}
191195

192-
func (c *Conn) appendSingleActionRaw(segs []string) (err error) {
196+
func (c *Conn) appendSingleActionRaw(segs []any) (err error) {
193197
_, err = fmt.Fprintf(c.strBuilder, "~%d\n", len(segs))
194198
if err != nil {
195199
return err
196200
}
197201

198202
for _, s := range segs {
199-
_, err = fmt.Fprintf(c.strBuilder, "%d\n%s\n", len(s), s)
203+
str := fmt.Sprintf("%v", s)
204+
_, err = fmt.Fprintf(c.strBuilder, "%d\n%s\n", len(str), str)
200205
if err != nil {
201206
return err
202207
}

‎conn_actions.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ func (c *Conn) Exec(ctx context.Context, packet *QueryPacket) ([]response.Respon
295295
return rp.resps, nil
296296
}
297297

298-
func (c *Conn) ExecSingleActionPacketRaw(segments ...string) (response.ResponseEntry, error) {
298+
// Allows executing a packet easily like:
299+
// c.ExecSingleActionPacketRaw("SET", "X", 100)
300+
//
301+
// The arguments accept any type. The arguments are formatted internally with %v so most basic types should be supported.
302+
func (c *Conn) ExecSingleActionPacketRaw(segments ...any) (response.ResponseEntry, error) {
299303
raw, err := c.BuildSingleActionPacketRaw(segments)
300304
if err != nil {
301305
return response.EmptyResponseEntry, err

‎conn_actions_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ func TestConn_ExecSingleActionPacketRaw(t *testing.T) {
1616

1717
tests := []struct {
1818
name string
19-
args []string
19+
args []any
2020
want any
2121
wantErr bool
2222
}{
23-
{ "SetX", []string { "SET", "X", "100" }, protocol.RespOkay, false },
24-
{ "SysInfoProtocol", []string { "SYS", "INFO", "PROTOCOL" }, skytable.ProtoVer, false },
25-
{ "DelX", []string { "DEL", "X" }, uint64(1), false },
23+
{ "SetX", []any { "SET", "X", 100 }, protocol.RespOkay, false },
24+
{ "SysInfoProtocol", []any { "SYS", "INFO", "PROTOCOL" }, skytable.ProtoVer, false },
25+
{ "DelX", []any { "DEL", "X" }, uint64(1), false },
2626
}
2727
for _, tt := range tests {
2828
t.Run(tt.name, func(t *testing.T) {

‎conn_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestConn_BuildSingleActionPacketRaw(t *testing.T) {
1010

1111
tests := []struct {
1212
name string
13-
args []string
13+
args []any
1414
wantRaw string
1515
wantErr bool
1616
}{
17-
{ "GetX", []string { "GET", "X" }, "*1\n~2\n3\nGET\n1\nX\n", false },
18-
{ "SysInfoProtocol", []string { "SYS", "INFO", "PROTOCOL" }, "*1\n~3\n3\nSYS\n4\nINFO\n8\nPROTOCOL\n", false },
17+
{ "SetX", []any { "SET", "X", 100 }, "*1\n~3\n3\nSET\n1\nX\n3\n100\n", false },
18+
{ "GetX", []any { "GET", "X" }, "*1\n~2\n3\nGET\n1\nX\n", false },
19+
{ "SysInfoProtocol", []any { "SYS", "INFO", "PROTOCOL" }, "*1\n~3\n3\nSYS\n4\nINFO\n8\nPROTOCOL\n", false },
1920
}
2021
for _, tt := range tests {
2122
t.Run(tt.name, func(t *testing.T) {

‎connpool.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ func NewConnPool(remote *net.TCPAddr, opts ConnPoolOptions) *ConnPool {
4141
opts.Cap = int64(runtime.NumCPU()) * 2
4242
}
4343

44-
return &ConnPool{
44+
cp := &ConnPool{
4545
opened: 0,
4646
available: make(chan *Conn, opts.Cap),
4747
remote: remote,
4848
opts: opts,
4949
}
50+
51+
return cp
5052
}
5153

5254
func (c *ConnPool) OpenedConns() int64 {

‎connpool_actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (c *ConnPool) Exec(ctx context.Context, packet *QueryPacket) ([]response.Re
158158
return conn.Exec(ctx, packet)
159159
}
160160

161-
func (c *ConnPool) ExecSingleActionPacketRaw(segments ...string) (response.ResponseEntry, error) {
161+
func (c *ConnPool) ExecSingleActionPacketRaw(segments ...any) (response.ResponseEntry, error) {
162162
conn, err := c.popConn(false)
163163
if err != nil {
164164
return response.EmptyResponseEntry, fmt.Errorf("*ConnPool.ExecSingleActionPacketRaw(): %w", err)

0 commit comments

Comments
 (0)