Skip to content

Commit 749e1ca

Browse files
authored
feat: added Unwrap, IsOk, IsErr (#1)
1 parent 0355d55 commit 749e1ca

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

eh.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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.
4252
func (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.
5974
type ehError struct {

eh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ func example(aFile string) (res Result[[]byte]) {
133133

134134
func 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

141141
func 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
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/olevski/eh
22

3-
go 1.20
3+
go 1.21

0 commit comments

Comments
 (0)