File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,6 +38,16 @@ func (r Result[T]) Eh() T {
3838 return r .Ok
3939}
4040
41+ // IsOk returns true when result has no error and otherwise false
42+ func (r Result [T ]) IsOk () bool {
43+ return r .Err == nil
44+ }
45+
46+ // IsErr returns true when result has error and otherwise false
47+ func (r Result [T ]) IsErr () bool {
48+ return r .Err != nil
49+ }
50+
4151// MustUnwrap returns the Ok value or panics if there is an error.
4252func (r Result [T ]) MustUnwrap () T {
4353 if r .Err != nil {
@@ -54,6 +64,11 @@ func (r Result[T]) MustUnwrapErr() error {
5464 return r .Err
5565}
5666
67+ // Unwrap returns a value and an error
68+ func (r Result [T ]) Unwrap () (T , error ) {
69+ return r .Ok , r .Err
70+ }
71+
5772// ehError is used to wrap any errors that are raised because of calling
5873// ReturnIfErr on a Result.
5974type ehError struct {
Original file line number Diff line number Diff line change @@ -133,14 +133,14 @@ func example(aFile string) (res Result[[]byte]) {
133133
134134func TestExample (t * testing.T ) {
135135 res := example ("README.md" )
136- if res .Err != nil {
136+ if res .IsErr () {
137137 t .Fatalf ("Err is not nil %+v" , res )
138138 }
139139}
140140
141141func TestExampleFail (t * testing.T ) {
142142 res := example ("non-existing-file" )
143- if res .Err == nil {
143+ if res .IsOk () {
144144 t .Fatalf ("Err should be nil %+v" , res )
145145 }
146146}
Original file line number Diff line number Diff line change 11module github.com/olevski/eh
22
3- go 1.20
3+ go 1.21
You can’t perform that action at this time.
0 commit comments