@@ -2578,6 +2578,63 @@ var _ = Describe("Commands", func() {
2578
2578
"val2" ,
2579
2579
"val" ,
2580
2580
}))
2581
+
2582
+ type setOmitEmpty struct {
2583
+ Set1 string `redis:"set1"`
2584
+ Set2 int `redis:"set2,omitempty"`
2585
+ Set3 time.Duration `redis:"set3,omitempty"`
2586
+ Set4 string `redis:"set4,omitempty"`
2587
+ Set5 time.Time `redis:"set5,omitempty"`
2588
+ Set6 * numberStruct `redis:"set6,omitempty"`
2589
+ Set7 numberStruct `redis:"set7,omitempty"`
2590
+ }
2591
+
2592
+ hSet = client .HSet (ctx , "hash3" , & setOmitEmpty {
2593
+ Set1 : "val" ,
2594
+ })
2595
+ Expect (hSet .Err ()).NotTo (HaveOccurred ())
2596
+ // both set1 and set7 are set
2597
+ // custom struct is not omitted
2598
+ Expect (hSet .Val ()).To (Equal (int64 (2 )))
2599
+
2600
+ hGetAll := client .HGetAll (ctx , "hash3" )
2601
+ Expect (hGetAll .Err ()).NotTo (HaveOccurred ())
2602
+ Expect (hGetAll .Val ()).To (Equal (map [string ]string {
2603
+ "set1" : "val" ,
2604
+ "set7" : `{"Number":0}` ,
2605
+ }))
2606
+ var hash3 setOmitEmpty
2607
+ Expect (hGetAll .Scan (& hash3 )).NotTo (HaveOccurred ())
2608
+ Expect (hash3 .Set1 ).To (Equal ("val" ))
2609
+ Expect (hash3 .Set2 ).To (Equal (0 ))
2610
+ Expect (hash3 .Set3 ).To (Equal (time .Duration (0 )))
2611
+ Expect (hash3 .Set4 ).To (Equal ("" ))
2612
+ Expect (hash3 .Set5 ).To (Equal (time.Time {}))
2613
+ Expect (hash3 .Set6 ).To (BeNil ())
2614
+ Expect (hash3 .Set7 ).To (Equal (numberStruct {}))
2615
+
2616
+ now := time .Now ()
2617
+ hSet = client .HSet (ctx , "hash4" , setOmitEmpty {
2618
+ Set1 : "val" ,
2619
+ Set5 : now ,
2620
+ Set6 : & numberStruct {
2621
+ Number : 5 ,
2622
+ },
2623
+ Set7 : numberStruct {
2624
+ Number : 3 ,
2625
+ },
2626
+ })
2627
+ Expect (hSet .Err ()).NotTo (HaveOccurred ())
2628
+ Expect (hSet .Val ()).To (Equal (int64 (4 )))
2629
+
2630
+ hGetAll = client .HGetAll (ctx , "hash4" )
2631
+ Expect (hGetAll .Err ()).NotTo (HaveOccurred ())
2632
+ Expect (hGetAll .Val ()).To (Equal (map [string ]string {
2633
+ "set1" : "val" ,
2634
+ "set5" : now .Format (time .RFC3339Nano ),
2635
+ "set6" : `{"Number":5}` ,
2636
+ "set7" : `{"Number":3}` ,
2637
+ }))
2581
2638
})
2582
2639
2583
2640
It ("should HSetNX" , func () {
@@ -7619,12 +7676,16 @@ type numberStruct struct {
7619
7676
Number int
7620
7677
}
7621
7678
7622
- func (s * numberStruct ) MarshalBinary () ([]byte , error ) {
7623
- return json .Marshal (s )
7679
+ func (n numberStruct ) MarshalBinary () ([]byte , error ) {
7680
+ return json .Marshal (n )
7681
+ }
7682
+
7683
+ func (n * numberStruct ) UnmarshalBinary (b []byte ) error {
7684
+ return json .Unmarshal (b , n )
7624
7685
}
7625
7686
7626
- func (s * numberStruct ) UnmarshalBinary ( b [] byte ) error {
7627
- return json .Unmarshal (b , s )
7687
+ func (n * numberStruct ) ScanRedis ( str string ) error {
7688
+ return json .Unmarshal ([] byte ( str ), n )
7628
7689
}
7629
7690
7630
7691
func deref (viface interface {}) interface {} {
0 commit comments