forked from mirror/redis
feat: add WriteArg and Scan net.IP(#2062)
* feat: add WriteArg net.IP * feat: add Scan net.IP
This commit is contained in:
parent
3961b9577f
commit
7d5167e862
|
@ -3,6 +3,7 @@ package proto
|
|||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
|
@ -115,6 +116,9 @@ func Scan(b []byte, v interface{}) error {
|
|||
return nil
|
||||
case encoding.BinaryUnmarshaler:
|
||||
return v.UnmarshalBinary(b)
|
||||
case *net.IP:
|
||||
*v = b
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf(
|
||||
"redis: can't unmarshal %T (consider implementing BinaryUnmarshaler)", v)
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -106,6 +107,8 @@ func (w *Writer) WriteArg(v interface{}) error {
|
|||
return err
|
||||
}
|
||||
return w.bytes(b)
|
||||
case net.IP:
|
||||
return w.bytes(v)
|
||||
default:
|
||||
return fmt.Errorf(
|
||||
"redis: can't marshal %T (implement encoding.BinaryMarshaler)", v)
|
||||
|
|
|
@ -3,6 +3,8 @@ package proto_test
|
|||
import (
|
||||
"bytes"
|
||||
"encoding"
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -64,6 +66,13 @@ var _ = Describe("WriteBuffer", func() {
|
|||
|
||||
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{}
|
||||
|
|
|
@ -316,6 +316,18 @@ var _ = Describe("Client", func() {
|
|||
err := client.Conn(ctx).Get(ctx, "this-key-does-not-exist").Err()
|
||||
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() {
|
||||
|
|
Loading…
Reference in New Issue