Skip to content

Commit

Permalink
feat(helpers): Add more methods (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Mar 16, 2024
1 parent eb05eeb commit 9bbdf04
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 290 deletions.
49 changes: 49 additions & 0 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"encoding/json"
"time"
)

// If returns trueVal if condition is true, otherwise falseVal
Expand Down Expand Up @@ -56,6 +57,18 @@ func With[T any](value T, callbacks ...func(T) T) T {
return value
}

// Transform calls the given callback with the given value then return the result.
//
// Example:
//
// Transform(1, strconv.Itoa) // "1"
// Transform("foo", func(s string) *foo {
// return &foo{Name: s}
// }) // &foo{Name: "foo"}
func Transform[T, R any](value T, callback func(T) R) R {
return callback(value)
}

// Pipe is a function that takes a value and returns a value
//
// Pipe(m1, m2, m3)(value) => m3(m2(m1(value)))
Expand Down Expand Up @@ -148,3 +161,39 @@ func Scan(src any, dest any) error {

return json.Unmarshal(bytes, dest)
}

// Retry retries the given function until it returns nil or the attempts are exhausted.
func Retry(fn func() error, attempts int, sleeps ...time.Duration) (err error) {
var sleep time.Duration
if len(sleeps) > 0 {
sleep = sleeps[0]
}

for i := 0; i < attempts; i++ {
if err = fn(); err == nil {
return nil
}
if sleep > 0 {
time.Sleep(sleep)
}
}
return
}

// Default returns defaultValue if value is zero, otherwise value.
func Default[T comparable](value T, defaultValue T) T {
var zero T
if value == zero {
return defaultValue
}
return value
}

// DefaultWith returns defaultValue if value is zero, otherwise value.
func DefaultWith[T comparable](value T, defaultValue func() T) T {
var zero T
if value == zero {
return defaultValue()
}
return value
}
75 changes: 75 additions & 0 deletions helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"context"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -121,6 +122,15 @@ func TestWhen(t *testing.T) {
assert.Equal(t, 18, f3.Age)
}

func TestTransform(t *testing.T) {
got := Transform("foo", func(s string) *foo {
return &foo{Name: s}
})
assert.Equal(t, "foo", got.Name)

assert.Equal(t, "1", Transform(1, strconv.Itoa))
}

func TestPipe(t *testing.T) {
// pipe functions
pipe := Pipe(
Expand Down Expand Up @@ -374,3 +384,68 @@ func TestChainWithErr(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "02", got)
}

func TestRetry(t *testing.T) {
// success
var i int
err := Retry(func() error {
i++
if i < 3 {
return assert.AnError
}
return nil
}, 3)
assert.Nil(t, err)
assert.Equal(t, 3, i)

// failed
err = Retry(func() error {
return assert.AnError
}, 3)
assert.Error(t, err)
assert.Equal(t, 3, i)
}

func TestDefault(t *testing.T) {
// string
got := Default("", "foo")
assert.Equal(t, "foo", got)

// int
got2 := Default(0, 10)
assert.Equal(t, 10, got2)

// struct
got3 := Default(foo{}, foo{Name: "bar"})
assert.Equal(t, "bar", got3.Name)

// ptr
got4 := Default(nil, &foo{Name: "bar"})
assert.Equal(t, "bar", got4.Name)
}

func TestDefaultWith(t *testing.T) {
// string
got := DefaultWith("", func() string {
return "foo"
})
assert.Equal(t, "foo", got)

// int
got2 := DefaultWith(0, func() int {
return 10
})
assert.Equal(t, 10, got2)

// struct
got3 := DefaultWith(foo{}, func() foo {
return foo{Name: "bar"}
})
assert.Equal(t, "bar", got3.Name)

// ptr
got4 := DefaultWith(nil, func() *foo {
return &foo{Name: "bar"}
})
assert.Equal(t, "bar", got4.Name)
}
115 changes: 0 additions & 115 deletions utils/utils.go

This file was deleted.

Loading

0 comments on commit 9bbdf04

Please sign in to comment.