@@ -2595,6 +2595,63 @@ var _ = Describe("Commands", func() {
25952595 "val2" ,
25962596 "val" ,
25972597 }))
2598+
2599+ type setOmitEmpty struct {
2600+ Set1 string `redis:"set1"`
2601+ Set2 int `redis:"set2,omitempty"`
2602+ Set3 time.Duration `redis:"set3,omitempty"`
2603+ Set4 string `redis:"set4,omitempty"`
2604+ Set5 time.Time `redis:"set5,omitempty"`
2605+ Set6 * numberStruct `redis:"set6,omitempty"`
2606+ Set7 numberStruct `redis:"set7,omitempty"`
2607+ }
2608+
2609+ hSet = client .HSet (ctx , "hash3" , & setOmitEmpty {
2610+ Set1 : "val" ,
2611+ })
2612+ Expect (hSet .Err ()).NotTo (HaveOccurred ())
2613+ // both set1 and set7 are set
2614+ // custom struct is not omitted
2615+ Expect (hSet .Val ()).To (Equal (int64 (2 )))
2616+
2617+ hGetAll := client .HGetAll (ctx , "hash3" )
2618+ Expect (hGetAll .Err ()).NotTo (HaveOccurred ())
2619+ Expect (hGetAll .Val ()).To (Equal (map [string ]string {
2620+ "set1" : "val" ,
2621+ "set7" : `{"Number":0}` ,
2622+ }))
2623+ var hash3 setOmitEmpty
2624+ Expect (hGetAll .Scan (& hash3 )).NotTo (HaveOccurred ())
2625+ Expect (hash3 .Set1 ).To (Equal ("val" ))
2626+ Expect (hash3 .Set2 ).To (Equal (0 ))
2627+ Expect (hash3 .Set3 ).To (Equal (time .Duration (0 )))
2628+ Expect (hash3 .Set4 ).To (Equal ("" ))
2629+ Expect (hash3 .Set5 ).To (Equal (time.Time {}))
2630+ Expect (hash3 .Set6 ).To (BeNil ())
2631+ Expect (hash3 .Set7 ).To (Equal (numberStruct {}))
2632+
2633+ now := time .Now ()
2634+ hSet = client .HSet (ctx , "hash4" , setOmitEmpty {
2635+ Set1 : "val" ,
2636+ Set5 : now ,
2637+ Set6 : & numberStruct {
2638+ Number : 5 ,
2639+ },
2640+ Set7 : numberStruct {
2641+ Number : 3 ,
2642+ },
2643+ })
2644+ Expect (hSet .Err ()).NotTo (HaveOccurred ())
2645+ Expect (hSet .Val ()).To (Equal (int64 (4 )))
2646+
2647+ hGetAll = client .HGetAll (ctx , "hash4" )
2648+ Expect (hGetAll .Err ()).NotTo (HaveOccurred ())
2649+ Expect (hGetAll .Val ()).To (Equal (map [string ]string {
2650+ "set1" : "val" ,
2651+ "set5" : now .Format (time .RFC3339Nano ),
2652+ "set6" : `{"Number":5}` ,
2653+ "set7" : `{"Number":3}` ,
2654+ }))
25982655 })
25992656
26002657 It ("should HSetNX" , func () {
@@ -7636,12 +7693,16 @@ type numberStruct struct {
76367693 Number int
76377694}
76387695
7639- func (s * numberStruct ) MarshalBinary () ([]byte , error ) {
7640- return json .Marshal (s )
7696+ func (n numberStruct ) MarshalBinary () ([]byte , error ) {
7697+ return json .Marshal (n )
7698+ }
7699+
7700+ func (n * numberStruct ) UnmarshalBinary (b []byte ) error {
7701+ return json .Unmarshal (b , n )
76417702}
76427703
7643- func (s * numberStruct ) UnmarshalBinary ( b [] byte ) error {
7644- return json .Unmarshal (b , s )
7704+ func (n * numberStruct ) ScanRedis ( str string ) error {
7705+ return json .Unmarshal ([] byte ( str ), n )
76457706}
76467707
76477708func deref (viface interface {}) interface {} {
0 commit comments