Skip to content

Commit 0355d55

Browse files
committed
feat: rename ReturnIfErr to Eh
1 parent 75ce2d9 commit 0355d55

3 files changed

Lines changed: 35 additions & 37 deletions

File tree

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ func example(aFile string) []byte, error {
2626
Into this:
2727

2828
```go
29-
func example(aFile string) (res Result[[]byte]) {
30-
defer EscapeHatch(&res) // must have pointer to the named output Result
29+
func example(aFile string) (res eh.Result[[]byte]) {
30+
defer eh.EscapeHatch(&res) // must have pointer to the named output Result
3131
buff := make([]byte, 5)
32-
file := NewResult(os.Open(aFile)).ReturnIfErr().Ok
33-
NewResult(file.Read(buff)).ReturnIfErr()
34-
return Result[[]byte]{Ok: buff}
32+
file := eh.NewResult(os.Open(aFile)).Eh()
33+
_ = eh.NewResult(file.Read(buff)).Eh()
34+
return eh.Result[[]byte]{Ok: buff}
3535
}
3636
```
3737

@@ -49,8 +49,11 @@ For a successful operation the output is like this:
4949

5050
The library provides just a handful or public structs and functions:
5151
- `Result` structs to capture an outcome that can potentially result in an error
52-
- `ReturnIfErr` function (`?` was not possible) that will stop the current function from executing
53-
if there is an error and return a `Result` that contains the error.
52+
- `Eh` method (`?` was not possible) that will stop the current function from executing
53+
if there is an error and return a `Result` that contains the error. If there is no error the `Result`
54+
is unwrapped and the `Ok` value is returned. Just like Canadians add "eh" to the end of a sentence
55+
to make it a question, you can call `Eh` on a `Result` to get a similar functionality to the question
56+
mark operator.
5457
- `EscapeHatch` function that should be deferred and given access to the enclosing
5558
function named `Result` argument so that the error can be recovered and the
5659
enclosed function can be interrupted early if an error occurs.
@@ -61,5 +64,5 @@ A few simple steps to incorporate in your code:
6164
1. Return a named `Result` from your fuction
6265
2. `defer` the `EscapeHatch` function with pointer to the named `Result` argument
6366
3. Wrap functions that return `any, error` into `eh.NewResult` to get `Result`
64-
4. Call `ReturnIfErr()` on any `Result` to stop the execution if there is an error
67+
4. Call `Eh()` on any `Result` to stop the execution if there is an error
6568
and return `Result{Err: Error}` from the enclosing function.

eh.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ func NewResult[T any](val T, err error) Result[T] {
2929
return Result[T]{val, err}
3030
}
3131

32-
// ReturnIfErr checks if there is an error in the result and if so then it will
33-
// panic with the error that was encountered.
34-
func (r Result[T]) ReturnIfErr() Result[T] {
32+
// Eh checks if there is an error in the result and if so then it will
33+
// panic with the error that was encountered. If there is no error the Ok value is returned.
34+
func (r Result[T]) Eh() T {
3535
if r.Err != nil {
3636
panic(ehError{r.Err})
3737
}
38-
return r
38+
return r.Ok
3939
}
4040

41-
// Unwrap returns the Ok value or panics if there is an error.
42-
func (r Result[T]) Unwrap() T {
41+
// MustUnwrap returns the Ok value or panics if there is an error.
42+
func (r Result[T]) MustUnwrap() T {
4343
if r.Err != nil {
4444
panic(r.Err)
4545
}
4646
return r.Ok
4747
}
4848

49-
// UnwrapErr returns the Err value or panics if there is no error.
50-
func (r Result[T]) UnwrapErr() error {
49+
// MustUnwrapErr returns the Err value or panics if there is no error.
50+
func (r Result[T]) MustUnwrapErr() error {
5151
if r.Err == nil {
5252
panic("expected the result to contain error")
5353
}

eh_test.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,22 @@ func divide(x int, y int) (int, error) {
3030

3131
func doDivide(x int, y int) (res Result[int]) {
3232
defer EscapeHatch(&res)
33-
res = NewResult(divide(x, y)).ReturnIfErr()
34-
return res
33+
return NewResult(divide(x, y))
3534
}
3635

3736
func doDivideMultiple(x int, y int) (res Result[int]) {
3837
defer EscapeHatch(&res)
39-
res = NewResult(divide(x, y)).ReturnIfErr()
40-
res = NewResult(divide(x*2, y+2)).ReturnIfErr()
41-
return res
38+
val := NewResult(divide(x, y)).Eh()
39+
return NewResult(divide(val+2, y))
4240
}
4341

4442
func doDivideGoRoutine(x int, y int, res *Result[int], wg *sync.WaitGroup) {
4543
defer wg.Done()
46-
defer EscapeHatch(res)
47-
*res = NewResult(divide(x, y)).ReturnIfErr()
44+
*res = NewResult(divide(x, y))
4845
}
4946

50-
func doDividePtr(x int, y int) (res *Result[int]) {
51-
defer EscapeHatch(res)
52-
ares := NewResult(divide(x, y)).ReturnIfErr()
53-
return &ares
47+
func doDividePtr(x int, y int) (res Result[int]) {
48+
return NewResult(divide(x, y))
5449
}
5550

5651
func TestSimple(t *testing.T) {
@@ -131,8 +126,8 @@ func TestSimpleResultMultipleFail(t *testing.T) {
131126
func example(aFile string) (res Result[[]byte]) {
132127
defer EscapeHatch(&res)
133128
buff := make([]byte, 5)
134-
file := NewResult(os.Open(aFile)).ReturnIfErr().Ok
135-
NewResult(file.Read(buff)).ReturnIfErr()
129+
file := NewResult(os.Open(aFile)).Eh()
130+
_ = NewResult(file.Read(buff)).Eh()
136131
return Result[[]byte]{Ok: buff}
137132
}
138133

@@ -150,33 +145,33 @@ func TestExampleFail(t *testing.T) {
150145
}
151146
}
152147

153-
func TestUnwrap(t *testing.T) {
148+
func TestMustUnwrap(t *testing.T) {
154149
res := Result[int]{Ok: 1}
155-
ok := res.Unwrap()
150+
ok := res.MustUnwrap()
156151
if ok != 1 {
157152
t.Fatal("Unwrap should return 1")
158153
}
159154
}
160155

161-
func TestUnwrapPanic(t *testing.T) {
156+
func TestMustUnwrapPanic(t *testing.T) {
162157
res := Result[int]{Err: fmt.Errorf("error")}
163158
defer func() { recover() }()
164-
_ = res.Unwrap()
159+
_ = res.MustUnwrap()
165160
t.Fatal("code should have panicked")
166161
}
167162

168-
func TestUnwrapErr(t *testing.T) {
163+
func TestMustUnwrapErr(t *testing.T) {
169164
aErr := fmt.Errorf("error")
170165
res := Result[int]{Err: aErr}
171-
err := res.UnwrapErr()
166+
err := res.MustUnwrapErr()
172167
if err != aErr {
173168
t.Fatal("UnwrapErr should return error")
174169
}
175170
}
176171

177-
func TestUnwrapErrPanic(t *testing.T) {
172+
func TestMustUnwrapErrPanic(t *testing.T) {
178173
res := Result[int]{Ok: 1}
179174
defer func() { recover() }()
180-
_ = res.UnwrapErr()
175+
_ = res.MustUnwrapErr()
181176
t.Fatal("code should have panicked")
182177
}

0 commit comments

Comments
 (0)