feat: add utils.Try
This commit is contained in:
parent
97564af275
commit
53899d58a1
@ -3,7 +3,7 @@ package metrics
|
|||||||
import (
|
import (
|
||||||
"git.0x7f.app/WOJ/woj-server/internal/e"
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
||||||
"git.0x7f.app/WOJ/woj-server/internal/misc/config"
|
"git.0x7f.app/WOJ/woj-server/internal/misc/config"
|
||||||
"git.0x7f.app/WOJ/woj-server/internal/pkg/cast"
|
"git.0x7f.app/WOJ/woj-server/pkg/cast"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/samber/do"
|
"github.com/samber/do"
|
||||||
|
27
pkg/utils/try.go
Normal file
27
pkg/utils/try.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
type TryChain[T any, V comparable] struct {
|
||||||
|
result T
|
||||||
|
error V
|
||||||
|
success V
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTry[T any, V comparable](success V) *TryChain[T, V] {
|
||||||
|
return &TryChain[T, V]{success: success}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TryChain[T, V]) Try(callback func() (T, V)) *TryChain[T, V] {
|
||||||
|
c.result, c.error = callback()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TryChain[T, V]) Or(callback func() (T, V)) *TryChain[T, V] {
|
||||||
|
if c.error == c.success {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
return c.Try(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TryChain[T, V]) Done() (T, V) {
|
||||||
|
return c.result, c.error
|
||||||
|
}
|
57
pkg/utils/try_test.go
Normal file
57
pkg/utils/try_test.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestTry(t *testing.T) {
|
||||||
|
t.Run("First Try", func(t *testing.T) {
|
||||||
|
val, err := NewTry[int, bool](true).
|
||||||
|
Try(func() (int, bool) { return 1, true }).
|
||||||
|
Or(func() (int, bool) { return 2, false }).
|
||||||
|
Or(func() (int, bool) { return 3, true }).
|
||||||
|
Or(func() (int, bool) { return 4, false }).
|
||||||
|
Done()
|
||||||
|
|
||||||
|
if val != 1 && err != true {
|
||||||
|
t.Error("Try failed")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Middle Or", func(t *testing.T) {
|
||||||
|
val, err := NewTry[int, bool](true).
|
||||||
|
Try(func() (int, bool) { return 1, false }).
|
||||||
|
Or(func() (int, bool) { return 2, false }).
|
||||||
|
Or(func() (int, bool) { return 3, true }).
|
||||||
|
Or(func() (int, bool) { return 4, false }).
|
||||||
|
Done()
|
||||||
|
|
||||||
|
if val != 3 && err != true {
|
||||||
|
t.Error("Try failed")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Last Or", func(t *testing.T) {
|
||||||
|
val, err := NewTry[int, bool](true).
|
||||||
|
Try(func() (int, bool) { return 1, false }).
|
||||||
|
Or(func() (int, bool) { return 2, false }).
|
||||||
|
Or(func() (int, bool) { return 3, false }).
|
||||||
|
Or(func() (int, bool) { return 4, true }).
|
||||||
|
Done()
|
||||||
|
|
||||||
|
if val != 4 && err != true {
|
||||||
|
t.Error("Try failed")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Nothing", func(t *testing.T) {
|
||||||
|
val, err := NewTry[int, bool](true).
|
||||||
|
Try(func() (int, bool) { return 1, false }).
|
||||||
|
Or(func() (int, bool) { return 2, false }).
|
||||||
|
Or(func() (int, bool) { return 3, false }).
|
||||||
|
Or(func() (int, bool) { return 4, false }).
|
||||||
|
Done()
|
||||||
|
|
||||||
|
if val != 4 && err != false {
|
||||||
|
t.Error("Try failed")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user