diff --git a/README.md b/README.md index 8a92347..4f67a66 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ -[![Project status](https://img.shields.io/badge/version-v1.0.4-vividgreen.svg)](https://github.com/GabrielHCataldo/go-redis-template/releases/tag/v1.0.4) +[![Project status](https://img.shields.io/badge/version-v1.0.5-vividgreen.svg)](https://github.com/GabrielHCataldo/go-redis-template/releases/tag/v1.0.5) [![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-redis-template)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-redis-template) [![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-redis-template/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-mongo?branch=main) [![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-redis-template/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-mongo) diff --git a/redis/main_test.go b/redis/main_test.go index 33177f4..7b223bf 100644 --- a/redis/main_test.go +++ b/redis/main_test.go @@ -23,7 +23,7 @@ type testSet struct { name string key any value any - opt option.Set + opt *option.Set wantErr bool } @@ -37,7 +37,7 @@ type testSetGet struct { key any value any dest any - opt option.Set + opt *option.Set wantErr bool } @@ -354,7 +354,7 @@ func initMSetInputs() []MSetInput { } } -func initOptionSet() option.Set { +func initOptionSet() *option.Set { return option.NewSet(). SetMode(option.SetModeDefault). SetTTL(redisDurationDefault). diff --git a/redis/option/set.go b/redis/option/set.go index 913a18e..28fe88f 100644 --- a/redis/option/set.go +++ b/redis/option/set.go @@ -18,38 +18,41 @@ type Set struct { } // NewSet creates a new Set instance. -func NewSet() Set { - return Set{} +func NewSet() *Set { + return &Set{} } // SetMode sets value for the Mode field. -func (s Set) SetMode(mode SetMode) Set { +func (s *Set) SetMode(mode SetMode) *Set { s.Mode = mode return s } // SetTTL sets value for the TTL field. -func (s Set) SetTTL(ttl time.Duration) Set { +func (s *Set) SetTTL(ttl time.Duration) *Set { s.TTL = ttl return s } // SetExpireAt sets value for the ExpireAt field. -func (s Set) SetExpireAt(expAt time.Time) Set { +func (s *Set) SetExpireAt(expAt time.Time) *Set { s.ExpireAt = expAt return s } // SetKeepTTL sets value for the KeepTTL field. -func (s Set) SetKeepTTL(keepTTL bool) Set { +func (s *Set) SetKeepTTL(keepTTL bool) *Set { s.KeepTTL = keepTTL return s } // GetOptionSetByParams assembles the Set object from optional parameters. -func GetOptionSetByParams(opts []Set) Set { - result := Set{} +func GetOptionSetByParams(opts []*Set) *Set { + result := &Set{} for _, opt := range opts { + if opt == nil { + continue + } if helper.IsNotEmpty(opt.Mode) { result.Mode = opt.Mode } diff --git a/redis/template.go b/redis/template.go index 2dcc154..c672bb2 100644 --- a/redis/template.go +++ b/redis/template.go @@ -18,7 +18,7 @@ type MSetInput struct { // Value can be of any type, but cannot be null, and must be compatible with conversion to string (helper.ConvertToString). Value any // Opt to customize the operation (not required) - Opt option.Set + Opt *option.Set } type MSetOutput struct { @@ -51,7 +51,7 @@ func NewTemplate(opts option.Client) *Template { // If the return is nil, the operation was carried out successfully, otherwise an error occurred in the operation. // // To customize the operation, use the opts parameter (option.Set). -func (t Template) Set(ctx context.Context, key, value any, opts ...option.Set) error { +func (t Template) Set(ctx context.Context, key, value any, opts ...*option.Set) error { result, err := t.set(ctx, key, value, false, opts...) if err == nil { err = result.Err() @@ -89,7 +89,7 @@ func (t Template) MSet(ctx context.Context, values ...MSetInput) []MSetOutput { // If the return is null, the operation was performed successfully, otherwise an error occurred in the operation. // // To customize the operation, use the opts parameter (option.Set). -func (t Template) SetGet(ctx context.Context, key, value, dest any, opts ...option.Set) error { +func (t Template) SetGet(ctx context.Context, key, value, dest any, opts ...*option.Set) error { result, err := t.set(ctx, key, value, true, opts...) if helper.IsNotNil(err) { return err @@ -243,7 +243,7 @@ func (t Template) SimpleDisconnect() { logger.Info("connection to redis closed.") } -func (t Template) set(ctx context.Context, key, value any, get bool, opts ...option.Set) (*redis.StatusCmd, error) { +func (t Template) set(ctx context.Context, key, value any, get bool, opts ...*option.Set) (*redis.StatusCmd, error) { opt := option.GetOptionSetByParams(opts) sKey, err := helper.ConvertToString(key) if helper.IsNotNil(err) { diff --git a/redis/template_test.go b/redis/template_test.go index 79dee51..5a9273f 100644 --- a/redis/template_test.go +++ b/redis/template_test.go @@ -13,7 +13,7 @@ func TestTemplateSet(t *testing.T) { t.Run(tt.name, func(t *testing.T) { ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) defer cancel() - err := redisTemplate.Set(ctx, tt.key, tt.value, tt.opt) + err := redisTemplate.Set(ctx, tt.key, tt.value, tt.opt, nil) if (err != nil) != tt.wantErr { logger.Errorf("Set() err = %v, wantErr = %v", err, tt.wantErr) t.Fail() @@ -40,7 +40,7 @@ func TestTemplateSetGet(t *testing.T) { t.Run(tt.name, func(t *testing.T) { ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) defer cancel() - err := redisTemplate.SetGet(ctx, tt.key, tt.value, tt.dest, tt.opt) + err := redisTemplate.SetGet(ctx, tt.key, tt.value, tt.dest, tt.opt, nil) if (err != nil) != tt.wantErr { logger.Errorf("SetGet() err = %v, wantErr = %v", err, tt.wantErr) t.Fail()