refactor(Type)!: harmonize naming 1
This commit is contained in:
46
type/interfaces.go
Normal file
46
type/interfaces.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package Type
|
||||
|
||||
// Created to abstract over Is_some and Is_ok
|
||||
type ValueContainer interface {
|
||||
HasValue() bool
|
||||
}
|
||||
|
||||
type Unwrappable[T any] interface {
|
||||
Expect(string) T // panics with a provided custom message
|
||||
Unwrap() T // panics with a generic message
|
||||
UnwrapOr(T) T // returns the provided default value
|
||||
UnwrapOrDefault() T // returns the default value of the type T
|
||||
UnwrapOrElse(func() T) T // returns the result of evaluating the provided function
|
||||
}
|
||||
|
||||
// Both an Optional and Result is an Optioner
|
||||
type Optioner[T any] interface {
|
||||
ValueContainer
|
||||
Unwrappable[T]
|
||||
}
|
||||
|
||||
type Optionaler[T any] interface {
|
||||
IsSome() bool
|
||||
IsNone() bool
|
||||
OkOr(error) Result[T]
|
||||
OkOrElse(func() error) Result[T]
|
||||
Unwrappable[T]
|
||||
}
|
||||
|
||||
type Resulter[T any] interface {
|
||||
IsOk() bool
|
||||
IsErr() bool
|
||||
Ok() Optional[T]
|
||||
Err() Optional[error]
|
||||
Unwrappable[T]
|
||||
}
|
||||
|
||||
// Ensure compile time the interfaces are implemented
|
||||
var (
|
||||
_ Optioner[any] = (*Optional[any])(nil)
|
||||
_ Optioner[any] = (*Result[any])(nil)
|
||||
_ Optionaler[any] = (*Optional[any])(nil)
|
||||
_ Resulter[any] = (*Result[any])(nil)
|
||||
_ ValueContainer = (*Optional[any])(nil)
|
||||
_ ValueContainer = (*Result[any])(nil)
|
||||
)
|
||||
108
type/optional.go
Normal file
108
type/optional.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package Type
|
||||
|
||||
import "github.com/lbatuska/goutils/assert"
|
||||
|
||||
// CTORS BEGIN
|
||||
func Some[T any](value T) Optional[T] {
|
||||
return Optional[T]{value, true}
|
||||
}
|
||||
|
||||
func None[T any]() Optional[T] {
|
||||
return Optional[T]{present: false}
|
||||
}
|
||||
|
||||
// Because None has no type passing a value of desired type might be preferred syntax over providing type on the function call
|
||||
func None_t[T any](T) Optional[T] {
|
||||
return Optional[T]{present: false}
|
||||
}
|
||||
|
||||
// CTORS END
|
||||
|
||||
func (opt *Optional[T]) IsSome() bool {
|
||||
if opt == nil {
|
||||
return false
|
||||
}
|
||||
return opt.present
|
||||
}
|
||||
|
||||
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
|
||||
func (opt *Optional[T]) Expect(msg string) T {
|
||||
assert.NotNil(opt)
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
panic(msg)
|
||||
}
|
||||
|
||||
func (opt *Optional[T]) Unwrap() T {
|
||||
assert.NotNil(opt)
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
panic("Tried unwrapping an Optional that did not have a value!")
|
||||
}
|
||||
|
||||
func (opt *Optional[T]) UnwrapOr(val T) T {
|
||||
if opt != nil {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (opt *Optional[T]) UnwrapOrDefault() T {
|
||||
if opt != nil {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
}
|
||||
var res T
|
||||
return res
|
||||
}
|
||||
|
||||
func (opt *Optional[T]) UnwrapOrElse(f func() T) T {
|
||||
if opt != nil {
|
||||
if opt.present {
|
||||
return opt.value
|
||||
}
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
// UNWRAPPABLE INTERFACE END
|
||||
|
||||
// transforms Some(v) to Ok(v), and None to Err(err)
|
||||
func (opt *Optional[T]) OkOr(err error) Result[T] {
|
||||
if opt != nil {
|
||||
if opt.present {
|
||||
return Ok(opt.value)
|
||||
}
|
||||
}
|
||||
|
||||
return Err[T](err)
|
||||
}
|
||||
|
||||
// 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] {
|
||||
if opt != nil {
|
||||
if opt.present {
|
||||
return Ok(opt.value)
|
||||
}
|
||||
}
|
||||
return Err[T](f())
|
||||
}
|
||||
136
type/result.go
Normal file
136
type/result.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package Type
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/lbatuska/goutils/assert"
|
||||
)
|
||||
|
||||
// CTORS BEGIN
|
||||
func Ok[T any](value T) Result[T] {
|
||||
return Result[T]{value: value, err: nil}
|
||||
}
|
||||
|
||||
func Err[T any](err error) Result[T] {
|
||||
return Result[T]{err: err}
|
||||
}
|
||||
|
||||
func Err_t[T any](err error, x T) Result[T] {
|
||||
return Result[T]{err: err}
|
||||
}
|
||||
|
||||
// CTORS END
|
||||
|
||||
func (res *Result[T]) IsOk() bool {
|
||||
if res == nil {
|
||||
return false
|
||||
}
|
||||
return res.err == nil
|
||||
}
|
||||
|
||||
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
|
||||
func (res *Result[T]) Expect(msg string) T {
|
||||
assert.NotNil(res)
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
panic(msg)
|
||||
}
|
||||
|
||||
func (res *Result[T]) Unwrap() T {
|
||||
assert.NotNil(res)
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
panic("Tried unwrapping a Result that had an error value!")
|
||||
}
|
||||
|
||||
func (res *Result[T]) UnwrapOr(val T) T {
|
||||
if res != nil {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (res *Result[T]) UnwrapOrDefault() T {
|
||||
if res != nil {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
}
|
||||
var ret T
|
||||
return ret
|
||||
}
|
||||
|
||||
func (res *Result[T]) UnwrapOrElse(f func() T) T {
|
||||
if res != nil {
|
||||
if res.err == nil {
|
||||
return res.value
|
||||
}
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
// UNWRAPPABLE INTERFACE
|
||||
|
||||
// This function panic on Ok instead of Err
|
||||
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 {
|
||||
return res.err
|
||||
}
|
||||
panic(msg)
|
||||
}
|
||||
|
||||
// This function panic on Ok instead of Err
|
||||
func (res *Result[T]) UnwrapErr() error {
|
||||
if res == nil {
|
||||
return errors.New("UnwrapErr was called on a nil Result.")
|
||||
}
|
||||
if res.err != nil {
|
||||
return res.err
|
||||
}
|
||||
panic("Expect_err was called with an Ok value")
|
||||
}
|
||||
|
||||
// transforms Result into Option, mapping Ok(v) to Some(v) and Err(e) to None
|
||||
func (res *Result[T]) Ok() Optional[T] {
|
||||
if res != nil {
|
||||
if res.err == nil {
|
||||
return Optional[T]{value: res.value, present: true}
|
||||
}
|
||||
}
|
||||
return Optional[T]{present: false}
|
||||
}
|
||||
|
||||
// transforms Result into Option, mapping Err(e) to Some(e) and Ok(v) to None
|
||||
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 {
|
||||
return Optional[error]{value: res.err, present: true}
|
||||
}
|
||||
return Optional[error]{present: false}
|
||||
}
|
||||
11
type/types.go
Normal file
11
type/types.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package Type
|
||||
|
||||
type Optional[T any] struct {
|
||||
value T
|
||||
present bool
|
||||
}
|
||||
|
||||
type Result[T any] struct {
|
||||
value T
|
||||
err error
|
||||
}
|
||||
44
type/utils.go
Normal file
44
type/utils.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package Type
|
||||
|
||||
func Expect[T any](val Unwrappable[T], msg string) T {
|
||||
return val.Expect(msg)
|
||||
}
|
||||
|
||||
func Unwrap[T any](val Unwrappable[T]) T {
|
||||
return val.Unwrap()
|
||||
}
|
||||
|
||||
func UnwrapOr[T any](val Unwrappable[T]) (def T) {
|
||||
return val.UnwrapOr(def)
|
||||
}
|
||||
|
||||
func UnwrapOrDefault[T any](val Unwrappable[T]) T {
|
||||
return val.UnwrapOrDefault()
|
||||
}
|
||||
|
||||
func UnwrapOrElse[T any](val Unwrappable[T], f func() T) T {
|
||||
return val.UnwrapOrElse(f)
|
||||
}
|
||||
|
||||
// Returns if the underlying data has a Value (false in case of None or Error)
|
||||
func HasValue(val ValueContainer) bool {
|
||||
return val.HasValue()
|
||||
}
|
||||
|
||||
func ResultWrap[T any](val T, err error) Result[T] {
|
||||
if err == nil {
|
||||
return Ok(val)
|
||||
}
|
||||
return Err[T](err)
|
||||
}
|
||||
|
||||
func ResultWrapb[T any](err error, val T) Result[T] {
|
||||
if err == nil {
|
||||
return Ok(val)
|
||||
}
|
||||
return Err[T](err)
|
||||
}
|
||||
|
||||
func Ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
Reference in New Issue
Block a user