mirror of https://github.com/go-redis/redis.git
Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com>
This commit is contained in:
parent
81947daa8d
commit
15682e3227
|
@ -65,37 +65,68 @@ func (w *Writer) WriteArg(v interface{}) error {
|
||||||
return w.string("")
|
return w.string("")
|
||||||
case string:
|
case string:
|
||||||
return w.string(v)
|
return w.string(v)
|
||||||
|
case *string:
|
||||||
|
return w.string(*v)
|
||||||
case []byte:
|
case []byte:
|
||||||
return w.bytes(v)
|
return w.bytes(v)
|
||||||
case int:
|
case int:
|
||||||
return w.int(int64(v))
|
return w.int(int64(v))
|
||||||
|
case *int:
|
||||||
|
return w.int(int64(*v))
|
||||||
case int8:
|
case int8:
|
||||||
return w.int(int64(v))
|
return w.int(int64(v))
|
||||||
|
case *int8:
|
||||||
|
return w.int(int64(*v))
|
||||||
case int16:
|
case int16:
|
||||||
return w.int(int64(v))
|
return w.int(int64(v))
|
||||||
|
case *int16:
|
||||||
|
return w.int(int64(*v))
|
||||||
case int32:
|
case int32:
|
||||||
return w.int(int64(v))
|
return w.int(int64(v))
|
||||||
|
case *int32:
|
||||||
|
return w.int(int64(*v))
|
||||||
case int64:
|
case int64:
|
||||||
return w.int(v)
|
return w.int(v)
|
||||||
|
case *int64:
|
||||||
|
return w.int(*v)
|
||||||
case uint:
|
case uint:
|
||||||
return w.uint(uint64(v))
|
return w.uint(uint64(v))
|
||||||
|
case *uint:
|
||||||
|
return w.uint(uint64(*v))
|
||||||
case uint8:
|
case uint8:
|
||||||
return w.uint(uint64(v))
|
return w.uint(uint64(v))
|
||||||
|
case *uint8:
|
||||||
|
return w.uint(uint64(*v))
|
||||||
case uint16:
|
case uint16:
|
||||||
return w.uint(uint64(v))
|
return w.uint(uint64(v))
|
||||||
|
case *uint16:
|
||||||
|
return w.uint(uint64(*v))
|
||||||
case uint32:
|
case uint32:
|
||||||
return w.uint(uint64(v))
|
return w.uint(uint64(v))
|
||||||
|
case *uint32:
|
||||||
|
return w.uint(uint64(*v))
|
||||||
case uint64:
|
case uint64:
|
||||||
return w.uint(v)
|
return w.uint(v)
|
||||||
|
case *uint64:
|
||||||
|
return w.uint(*v)
|
||||||
case float32:
|
case float32:
|
||||||
return w.float(float64(v))
|
return w.float(float64(v))
|
||||||
|
case *float32:
|
||||||
|
return w.float(float64(*v))
|
||||||
case float64:
|
case float64:
|
||||||
return w.float(v)
|
return w.float(v)
|
||||||
|
case *float64:
|
||||||
|
return w.float(*v)
|
||||||
case bool:
|
case bool:
|
||||||
if v {
|
if v {
|
||||||
return w.int(1)
|
return w.int(1)
|
||||||
}
|
}
|
||||||
return w.int(0)
|
return w.int(0)
|
||||||
|
case *bool:
|
||||||
|
if *v {
|
||||||
|
return w.int(1)
|
||||||
|
}
|
||||||
|
return w.int(0)
|
||||||
case time.Time:
|
case time.Time:
|
||||||
w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano)
|
w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano)
|
||||||
return w.bytes(w.numBuf)
|
return w.bytes(w.numBuf)
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
. "github.com/bsm/gomega"
|
. "github.com/bsm/gomega"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9/internal/proto"
|
"github.com/redis/go-redis/v9/internal/proto"
|
||||||
|
"github.com/redis/go-redis/v9/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MyType struct{}
|
type MyType struct{}
|
||||||
|
@ -100,3 +101,53 @@ func BenchmarkWriteBuffer_Append(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ = Describe("WriteArg", func() {
|
||||||
|
var buf *bytes.Buffer
|
||||||
|
var wr *proto.Writer
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
buf = new(bytes.Buffer)
|
||||||
|
wr = proto.NewWriter(buf)
|
||||||
|
})
|
||||||
|
|
||||||
|
args := map[any]string{
|
||||||
|
"hello": "$1\r\nhello\r\n",
|
||||||
|
int(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(int(10)): "$2\r\n10\r\n",
|
||||||
|
int8(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(int8(10)): "$2\r\n10\r\n",
|
||||||
|
int16(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(int16(10)): "$2\r\n10\r\n",
|
||||||
|
int32(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(int32(10)): "$2\r\n10\r\n",
|
||||||
|
int64(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(int64(10)): "$2\r\n10\r\n",
|
||||||
|
uint(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(uint(10)): "$2\r\n10\r\n",
|
||||||
|
uint8(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(uint8(10)): "$2\r\n10\r\n",
|
||||||
|
uint16(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(uint16(10)): "$2\r\n10\r\n",
|
||||||
|
uint32(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(uint32(10)): "$2\r\n10\r\n",
|
||||||
|
uint64(10): "$2\r\n10\r\n",
|
||||||
|
util.ToPtr(uint64(10)): "$2\r\n10\r\n",
|
||||||
|
float32(10.3): "$4\r\n10.3\r\n",
|
||||||
|
util.ToPtr(float32(10.3)): "$4\r\n10.3\r\n",
|
||||||
|
float64(10.3): "$4\r\n10.3\r\n",
|
||||||
|
util.ToPtr(float64(10.3)): "$4\r\n10.3\r\n",
|
||||||
|
bool(true): "$1\r\n1\r\n",
|
||||||
|
bool(false): "$1\r\n0\r\n",
|
||||||
|
util.ToPtr(bool(true)): "$1\r\n1\r\n",
|
||||||
|
util.ToPtr(bool(false)): "$1\r\n0\r\n",
|
||||||
|
}
|
||||||
|
|
||||||
|
for arg, expect := range args {
|
||||||
|
It(fmt.Sprintf("should write arg of type %T", arg), func() {
|
||||||
|
err := wr.WriteArg(arg)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(buf.String()).To(Equal(expect))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
func ToPtr[T any](v T) *T {
|
||||||
|
return &v
|
||||||
|
}
|
Loading…
Reference in New Issue