@@ -2578,6 +2578,63 @@ var _ = Describe("Commands", func() {
25782578 "val2" ,
25792579 "val" ,
25802580 }))
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+ }))
25812638 })
25822639
25832640 It ("should HSetNX" , func () {
@@ -7209,6 +7266,17 @@ var _ = Describe("Commands", func() {
72097266 Expect (err ).NotTo (HaveOccurred ())
72107267 Expect (vals ).To (Equal ([]interface {}{int64 (12 ), proto .RedisError ("error" ), "abc" }))
72117268 })
7269+
7270+ It ("returns empty values when args are nil" , func () {
7271+ vals , err := client .Eval (
7272+ ctx ,
7273+ "return {ARGV[1]}" ,
7274+ []string {},
7275+ nil ,
7276+ ).Result ()
7277+ Expect (err ).NotTo (HaveOccurred ())
7278+ Expect (vals ).To (BeEmpty ())
7279+ })
72127280 })
72137281
72147282 Describe ("EvalRO" , func () {
@@ -7232,6 +7300,17 @@ var _ = Describe("Commands", func() {
72327300 Expect (err ).NotTo (HaveOccurred ())
72337301 Expect (vals ).To (Equal ([]interface {}{int64 (12 ), proto .RedisError ("error" ), "abc" }))
72347302 })
7303+
7304+ It ("returns empty values when args are nil" , func () {
7305+ vals , err := client .EvalRO (
7306+ ctx ,
7307+ "return {ARGV[1]}" ,
7308+ []string {},
7309+ nil ,
7310+ ).Result ()
7311+ Expect (err ).NotTo (HaveOccurred ())
7312+ Expect (vals ).To (BeEmpty ())
7313+ })
72357314 })
72367315
72377316 Describe ("Functions" , func () {
@@ -7597,12 +7676,16 @@ type numberStruct struct {
75977676 Number int
75987677}
75997678
7600- func (s * numberStruct ) MarshalBinary () ([]byte , error ) {
7601- 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 )
76027685}
76037686
7604- func (s * numberStruct ) UnmarshalBinary ( b [] byte ) error {
7605- return json .Unmarshal (b , s )
7687+ func (n * numberStruct ) ScanRedis ( str string ) error {
7688+ return json .Unmarshal ([] byte ( str ), n )
76067689}
76077690
76087691func deref (viface interface {}) interface {} {
0 commit comments