From 15682e3227597d12d7f76862b712a75fbb3d02d3 Mon Sep 17 00:00:00 2001 From: Tong Zhaoqi <1661610563@qq.com> Date: Tue, 31 Oct 2023 13:17:22 +0800 Subject: [PATCH] feat: support write the types of pointer of simple data types (#2745) (#2753) Co-authored-by: ofekshenawa <104765379+ofekshenawa@users.noreply.github.com> --- internal/proto/writer.go | 31 +++++++++++++++++++++ internal/proto/writer_test.go | 51 +++++++++++++++++++++++++++++++++++ internal/util/type.go | 5 ++++ 3 files changed, 87 insertions(+) create mode 100644 internal/util/type.go diff --git a/internal/proto/writer.go b/internal/proto/writer.go index 8eaada19..78595cc4 100644 --- a/internal/proto/writer.go +++ b/internal/proto/writer.go @@ -65,37 +65,68 @@ func (w *Writer) WriteArg(v interface{}) error { return w.string("") case string: return w.string(v) + case *string: + return w.string(*v) case []byte: return w.bytes(v) case int: return w.int(int64(v)) + case *int: + return w.int(int64(*v)) case int8: return w.int(int64(v)) + case *int8: + return w.int(int64(*v)) case int16: return w.int(int64(v)) + case *int16: + return w.int(int64(*v)) case int32: return w.int(int64(v)) + case *int32: + return w.int(int64(*v)) case int64: return w.int(v) + case *int64: + return w.int(*v) case uint: return w.uint(uint64(v)) + case *uint: + return w.uint(uint64(*v)) case uint8: return w.uint(uint64(v)) + case *uint8: + return w.uint(uint64(*v)) case uint16: return w.uint(uint64(v)) + case *uint16: + return w.uint(uint64(*v)) case uint32: return w.uint(uint64(v)) + case *uint32: + return w.uint(uint64(*v)) case uint64: return w.uint(v) + case *uint64: + return w.uint(*v) case float32: return w.float(float64(v)) + case *float32: + return w.float(float64(*v)) case float64: return w.float(v) + case *float64: + return w.float(*v) case bool: if v { return w.int(1) } return w.int(0) + case *bool: + if *v { + return w.int(1) + } + return w.int(0) case time.Time: w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano) return w.bytes(w.numBuf) diff --git a/internal/proto/writer_test.go b/internal/proto/writer_test.go index c801f87d..55f3d663 100644 --- a/internal/proto/writer_test.go +++ b/internal/proto/writer_test.go @@ -12,6 +12,7 @@ import ( . "github.com/bsm/gomega" "github.com/redis/go-redis/v9/internal/proto" + "github.com/redis/go-redis/v9/internal/util" ) 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)) + }) + } +}) diff --git a/internal/util/type.go b/internal/util/type.go new file mode 100644 index 00000000..a7ea712e --- /dev/null +++ b/internal/util/type.go @@ -0,0 +1,5 @@ +package util + +func ToPtr[T any](v T) *T { + return &v +}