Skip to content

Commit

Permalink
feat: add Get() and new construction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Jan 24, 2024
1 parent 4c70fd0 commit a0bb6d1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
13 changes: 13 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shine
type Option[T any] interface {
IsSome() bool
IsNone() bool
Get() (T, bool)
Expect(panicV any) T
Unwrap() T
UnwrapOr(def T) T
Expand All @@ -25,3 +26,15 @@ func NewOption[T any](v *T) Option[T] {

return NewSome(*v)
}

func NewOptionOf[T any](v T) Option[T] {
return NewOptionFrom(v, !isNil(v))
}

func NewOptionFrom[T any](v T, ok bool) Option[T] {
if ok {
return NewSome(v)
}

return NewNone[T]()
}
5 changes: 5 additions & 0 deletions option_none.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ func (n None[T]) IsNone() bool {
return true
}

func (n None[T]) Get() (T, bool) {
var def T
return def, false
}

func (n None[T]) Expect(panicV any) T {
panic(panicV)
}
Expand Down
4 changes: 4 additions & 0 deletions option_some.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (s Some[T]) IsNone() bool {
return false
}

func (s Some[T]) Get() (T, bool) {
return s.v, true
}

func (s Some[T]) Expect(panicV any) T {
return s.v
}
Expand Down
13 changes: 3 additions & 10 deletions result.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package shine

import "reflect"

type Result[T any, E error] interface {
IsOk() bool
IsOkAnd(fn func(v T) bool) bool
IsErr() bool
IsErrAnd(fn func(v E) bool) bool
Get() (T, E)
Expect(panicV any) T
ExpectErr(panicV any) E
Unwrap() T
Expand All @@ -28,14 +27,8 @@ type Result[T any, E error] interface {
}

func NewResult[T any, E error](v T, err E) Result[T, E] {
errV := reflect.ValueOf(err)
switch errV.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map, reflect.Chan, reflect.Func:
if !errV.IsNil() {
return NewErr[T](err)
}

default:
if !isNil(err) {
return NewErr[T](err)
}

return NewOk[T, E](v)
Expand Down
5 changes: 5 additions & 0 deletions result_err.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func (e Err[T, E]) IsErrAnd(fn func(e E) bool) bool {
return fn(e.err)
}

func (e Err[T, E]) Get() (T, E) {
var def T
return def, e.err
}

func (e Err[T, E]) Expect(panicV any) T {
panic(panicV)
}
Expand Down
5 changes: 5 additions & 0 deletions result_ok.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func (o Ok[T, E]) IsErrAnd(fn func(e E) bool) bool {
return false
}

func (o Ok[T, E]) Get() (T, E) {
var def E
return o.v, def
}

func (o Ok[T, E]) Expect(panicV any) T {
return o.v
}
Expand Down
18 changes: 18 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package shine

import "reflect"

func isNil(v any) bool {
if v == nil {
return true
}

rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map, reflect.Chan, reflect.Func:
return rv.IsNil()

default:
return false
}
}

0 comments on commit a0bb6d1

Please sign in to comment.