feat: add WriteArg and Scan net.IP(#2062)

* feat: add WriteArg net.IP

* feat: add Scan net.IP
This commit is contained in:
Sown 2022-03-28 09:16:12 +08:00 committed by GitHub
parent 3961b9577f
commit 7d5167e862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package proto
import ( import (
"encoding" "encoding"
"fmt" "fmt"
"net"
"reflect" "reflect"
"time" "time"
@ -115,6 +116,9 @@ func Scan(b []byte, v interface{}) error {
return nil return nil
case encoding.BinaryUnmarshaler: case encoding.BinaryUnmarshaler:
return v.UnmarshalBinary(b) return v.UnmarshalBinary(b)
case *net.IP:
*v = b
return nil
default: default:
return fmt.Errorf( return fmt.Errorf(
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v) "redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)

View File

@ -4,6 +4,7 @@ import (
"encoding" "encoding"
"fmt" "fmt"
"io" "io"
"net"
"strconv" "strconv"
"time" "time"
@ -106,6 +107,8 @@ func (w *Writer) WriteArg(v interface{}) error {
return err return err
} }
return w.bytes(b) return w.bytes(b)
case net.IP:
return w.bytes(v)
default: default:
return fmt.Errorf( return fmt.Errorf(
"redis: can't marshal %T (implement encoding.BinaryMarshaler)", v) "redis: can't marshal %T (implement encoding.BinaryMarshaler)", v)

View File

@ -3,6 +3,8 @@ package proto_test
import ( import (
"bytes" "bytes"
"encoding" "encoding"
"fmt"
"net"
"testing" "testing"
"time" "time"
@ -64,6 +66,13 @@ var _ = Describe("WriteBuffer", func() {
Expect(buf.Len()).To(Equal(15)) Expect(buf.Len()).To(Equal(15))
}) })
It("should append net.IP", func() {
ip := net.ParseIP("192.168.1.1")
err := wr.WriteArgs([]interface{}{ip})
Expect(err).NotTo(HaveOccurred())
Expect(buf.String()).To(Equal(fmt.Sprintf("*1\r\n$16\r\n%s\r\n", bytes.NewBuffer(ip))))
})
}) })
type discard struct{} type discard struct{}

View File

@ -316,6 +316,18 @@ var _ = Describe("Client", func() {
err := client.Conn(ctx).Get(ctx, "this-key-does-not-exist").Err() err := client.Conn(ctx).Get(ctx, "this-key-does-not-exist").Err()
Expect(err).To(Equal(redis.Nil)) Expect(err).To(Equal(redis.Nil))
}) })
It("should set and scan net.IP", func() {
ip := net.ParseIP("192.168.1.1")
err := client.Set(ctx, "ip", ip, 0).Err()
Expect(err).NotTo(HaveOccurred())
var ip2 net.IP
err = client.Get(ctx, "ip").Scan(&ip2)
Expect(err).NotTo(HaveOccurred())
Expect(ip2).To(Equal(ip))
})
}) })
var _ = Describe("Client timeout", func() { var _ = Describe("Client timeout", func() {