refactor(typeutils)!: refactor to pointer receivers
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package typeutils
|
package typeutils
|
||||||
|
|
||||||
|
import "github.com/lbatuska/goutils/assert"
|
||||||
|
|
||||||
// CTORS BEGIN
|
// CTORS BEGIN
|
||||||
func Some[T any](value T) Optional[T] {
|
func Some[T any](value T) Optional[T] {
|
||||||
return Optional[T]{value, true}
|
return Optional[T]{value, true}
|
||||||
@@ -16,44 +18,68 @@ func None_t[T any](T) Optional[T] {
|
|||||||
|
|
||||||
// CTORS END
|
// CTORS END
|
||||||
|
|
||||||
func (opt Optional[T]) IsSome() bool { return opt.present }
|
func (opt *Optional[T]) IsSome() bool {
|
||||||
func (opt Optional[T]) IsNone() bool { return !opt.present }
|
if opt == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return opt.present
|
||||||
|
}
|
||||||
|
|
||||||
func (opt Optional[T]) HasValue() bool { return opt.IsSome() }
|
func (opt *Optional[T]) IsNone() bool {
|
||||||
|
if opt == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return !opt.present
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opt *Optional[T]) HasValue() bool {
|
||||||
|
if opt == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return opt.IsSome()
|
||||||
|
}
|
||||||
|
|
||||||
// UNWRAPPABLE INTERFACE BEGIN
|
// UNWRAPPABLE INTERFACE BEGIN
|
||||||
func (opt Optional[T]) Expect(msg string) T {
|
func (opt *Optional[T]) Expect(msg string) T {
|
||||||
|
assert.NotNil(opt)
|
||||||
if opt.present {
|
if opt.present {
|
||||||
return opt.value
|
return opt.value
|
||||||
}
|
}
|
||||||
panic(msg)
|
panic(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt Optional[T]) Unwrap() T {
|
func (opt *Optional[T]) Unwrap() T {
|
||||||
|
assert.NotNil(opt)
|
||||||
if opt.present {
|
if opt.present {
|
||||||
return opt.value
|
return opt.value
|
||||||
}
|
}
|
||||||
panic("Tried unwrapping an Optional that did not have a value!")
|
panic("Tried unwrapping an Optional that did not have a value!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt Optional[T]) UnwrapOr(val T) T {
|
func (opt *Optional[T]) UnwrapOr(val T) T {
|
||||||
if opt.present {
|
if opt != nil {
|
||||||
return opt.value
|
if opt.present {
|
||||||
|
return opt.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt Optional[T]) UnwrapOrDefault() T {
|
func (opt *Optional[T]) UnwrapOrDefault() T {
|
||||||
if opt.present {
|
if opt != nil {
|
||||||
return opt.value
|
if opt.present {
|
||||||
|
return opt.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var res T
|
var res T
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt Optional[T]) UnwrapOrElse(f func() T) T {
|
func (opt *Optional[T]) UnwrapOrElse(f func() T) T {
|
||||||
if opt.present {
|
if opt != nil {
|
||||||
return opt.value
|
if opt.present {
|
||||||
|
return opt.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return f()
|
return f()
|
||||||
}
|
}
|
||||||
@@ -61,17 +87,22 @@ func (opt Optional[T]) UnwrapOrElse(f func() T) T {
|
|||||||
// UNWRAPPABLE INTERFACE END
|
// UNWRAPPABLE INTERFACE END
|
||||||
|
|
||||||
// transforms Some(v) to Ok(v), and None to Err(err)
|
// transforms Some(v) to Ok(v), and None to Err(err)
|
||||||
func (opt Optional[T]) OkOr(err error) Result[T] {
|
func (opt *Optional[T]) OkOr(err error) Result[T] {
|
||||||
if opt.present {
|
if opt != nil {
|
||||||
return Ok(opt.value)
|
if opt.present {
|
||||||
|
return Ok(opt.value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Err[T](err)
|
return Err[T](err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// transforms Some(v) to Ok(v), and None to a value of Err using the provided function
|
// transforms Some(v) to Ok(v), and None to a value of Err using the provided function
|
||||||
func (opt Optional[T]) OkOrElse(f func() error) Result[T] {
|
func (opt *Optional[T]) OkOrElse(f func() error) Result[T] {
|
||||||
if opt.present {
|
if opt != nil {
|
||||||
return Ok(opt.value)
|
if opt.present {
|
||||||
|
return Ok(opt.value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Err[T](f())
|
return Err[T](f())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
package typeutils
|
package typeutils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/lbatuska/goutils/assert"
|
||||||
|
)
|
||||||
|
|
||||||
// CTORS BEGIN
|
// CTORS BEGIN
|
||||||
func Ok[T any](value T) Result[T] {
|
func Ok[T any](value T) Result[T] {
|
||||||
return Result[T]{value: value, err: nil}
|
return Result[T]{value: value, err: nil}
|
||||||
@@ -15,44 +21,68 @@ func Err_t[T any](err error, x T) Result[T] {
|
|||||||
|
|
||||||
// CTORS END
|
// CTORS END
|
||||||
|
|
||||||
func (res Result[T]) IsOk() bool { return res.err == nil }
|
func (res *Result[T]) IsOk() bool {
|
||||||
func (res Result[T]) IsErr() bool { return res.err != nil }
|
if res == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return res.err == nil
|
||||||
|
}
|
||||||
|
|
||||||
func (res Result[T]) HasValue() bool { return res.IsOk() }
|
func (res *Result[T]) IsErr() bool {
|
||||||
|
if res == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return res.err != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (res *Result[T]) HasValue() bool {
|
||||||
|
if res == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return res.IsOk()
|
||||||
|
}
|
||||||
|
|
||||||
// UNWRAPPABLE INTERFACE
|
// UNWRAPPABLE INTERFACE
|
||||||
func (res Result[T]) Expect(msg string) T {
|
func (res *Result[T]) Expect(msg string) T {
|
||||||
|
assert.NotNil(res)
|
||||||
if res.err == nil {
|
if res.err == nil {
|
||||||
return res.value
|
return res.value
|
||||||
}
|
}
|
||||||
panic(msg)
|
panic(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (res Result[T]) Unwrap() T {
|
func (res *Result[T]) Unwrap() T {
|
||||||
|
assert.NotNil(res)
|
||||||
if res.err == nil {
|
if res.err == nil {
|
||||||
return res.value
|
return res.value
|
||||||
}
|
}
|
||||||
panic("Tried unwrapping a Result that had an error value!")
|
panic("Tried unwrapping a Result that had an error value!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (res Result[T]) UnwrapOr(val T) T {
|
func (res *Result[T]) UnwrapOr(val T) T {
|
||||||
if res.err == nil {
|
if res != nil {
|
||||||
return res.value
|
if res.err == nil {
|
||||||
|
return res.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (res Result[T]) UnwrapOrDefault() T {
|
func (res *Result[T]) UnwrapOrDefault() T {
|
||||||
if res.err == nil {
|
if res != nil {
|
||||||
return res.value
|
if res.err == nil {
|
||||||
|
return res.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var ret T
|
var ret T
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (res Result[T]) UnwrapOrElse(f func() T) T {
|
func (res *Result[T]) UnwrapOrElse(f func() T) T {
|
||||||
if res.err == nil {
|
if res != nil {
|
||||||
return res.value
|
if res.err == nil {
|
||||||
|
return res.value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return f()
|
return f()
|
||||||
}
|
}
|
||||||
@@ -60,7 +90,11 @@ func (res Result[T]) UnwrapOrElse(f func() T) T {
|
|||||||
// UNWRAPPABLE INTERFACE
|
// UNWRAPPABLE INTERFACE
|
||||||
|
|
||||||
// This function panic on Ok instead of Err
|
// This function panic on Ok instead of Err
|
||||||
func (res Result[T]) ExpectErr(msg string) error {
|
func (res *Result[T]) ExpectErr(msg string) error {
|
||||||
|
if res == nil {
|
||||||
|
return errors.New("ExpectErr was called on a nil Result.")
|
||||||
|
}
|
||||||
|
|
||||||
if res.err != nil {
|
if res.err != nil {
|
||||||
return res.err
|
return res.err
|
||||||
}
|
}
|
||||||
@@ -68,7 +102,10 @@ func (res Result[T]) ExpectErr(msg string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This function panic on Ok instead of Err
|
// This function panic on Ok instead of Err
|
||||||
func (res Result[T]) UnwrapErr() error {
|
func (res *Result[T]) UnwrapErr() error {
|
||||||
|
if res == nil {
|
||||||
|
return errors.New("UnwrapErr was called on a nil Result.")
|
||||||
|
}
|
||||||
if res.err != nil {
|
if res.err != nil {
|
||||||
return res.err
|
return res.err
|
||||||
}
|
}
|
||||||
@@ -76,15 +113,22 @@ func (res Result[T]) UnwrapErr() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// transforms Result into Option, mapping Ok(v) to Some(v) and Err(e) to None
|
// transforms Result into Option, mapping Ok(v) to Some(v) and Err(e) to None
|
||||||
func (res Result[T]) Ok() Optional[T] {
|
func (res *Result[T]) Ok() Optional[T] {
|
||||||
if res.err == nil {
|
if res != nil {
|
||||||
return Optional[T]{value: res.value, present: true}
|
if res.err == nil {
|
||||||
|
return Optional[T]{value: res.value, present: true}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Optional[T]{present: false}
|
return Optional[T]{present: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
// transforms Result into Option, mapping Err(e) to Some(e) and Ok(v) to None
|
// transforms Result into Option, mapping Err(e) to Some(e) and Ok(v) to None
|
||||||
func (res Result[T]) Err() Optional[error] {
|
func (res *Result[T]) Err() Optional[error] {
|
||||||
|
if res == nil {
|
||||||
|
return Optional[error]{
|
||||||
|
value: errors.New("Err was called on a nil Result."), present: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
if res.err != nil {
|
if res.err != nil {
|
||||||
return Optional[error]{value: res.err, present: true}
|
return Optional[error]{value: res.err, present: true}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user