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