diff options
author | Keuin <[email protected]> | 2023-07-01 21:52:08 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-07-01 21:52:27 +0800 |
commit | 40e0b3172af1e8deab80a7bf2bf95337ef9201c6 (patch) | |
tree | 2b99c5028a1b799d0887f028351bde3560f72d5c /common/orelse.go | |
parent | 6c3c988bae6f8a78afd3503743106395792ee2b0 (diff) |
Refactor: replace homemade result handling with library.
Diffstat (limited to 'common/orelse.go')
-rw-r--r-- | common/orelse.go | 61 |
1 files changed, 0 insertions, 61 deletions
diff --git a/common/orelse.go b/common/orelse.go deleted file mode 100644 index 29e3dc7..0000000 --- a/common/orelse.go +++ /dev/null @@ -1,61 +0,0 @@ -package common - -import "reflect" - -type Opt[T any] interface { - OrElse(thing T) T -} - -type OptError[T any] struct { - thing T - err error -} - -type OptNull[T any] struct { - ptr *T -} - -type OptZero[T any] struct { - thing T -} - -func (o OptNull[T]) OrElse(thing T) T { - if o.ptr != nil { - return *o.ptr - } - return thing -} - -func Errorable[T any](thing T, err error) Opt[T] { - return OptError[T]{ - thing: thing, - err: err, - } -} - -func (o OptError[T]) OrElse(thing T) T { - if o.err != nil { - return thing - } - return o.thing -} - -func Nullable[T any](ptr *T) Opt[T] { - return OptNull[T]{ - ptr: ptr, - } -} - -func Zeroable[T any](thing T) Opt[T] { - return OptZero[T]{ - thing: thing, - } -} - -func (o OptZero[T]) OrElse(thing T) T { - var zero T - if reflect.DeepEqual(zero, o.thing) { - return thing - } - return o.thing -} |