Skip to content

Commit

Permalink
Refactor every part of options to be pointers and have more dynamics …
Browse files Browse the repository at this point in the history
…when instantiating an option parameter.
  • Loading branch information
Gabriel Cataldo committed Jan 18, 2024
1 parent 0a27b06 commit bf166bb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!--suppress ALL -->
<img align="right" src="gopher-redis.png" alt="">

[![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)
Expand Down
6 changes: 3 additions & 3 deletions redis/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type testSet struct {
name string
key any
value any
opt option.Set
opt *option.Set
wantErr bool
}

Expand All @@ -37,7 +37,7 @@ type testSetGet struct {
key any
value any
dest any
opt option.Set
opt *option.Set
wantErr bool
}

Expand Down Expand Up @@ -354,7 +354,7 @@ func initMSetInputs() []MSetInput {
}
}

func initOptionSet() option.Set {
func initOptionSet() *option.Set {
return option.NewSet().
SetMode(option.SetModeDefault).
SetTTL(redisDurationDefault).
Expand Down
19 changes: 11 additions & 8 deletions redis/option/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions redis/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions redis/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit bf166bb

Please sign in to comment.