Skip to content

Commit 808f3a5

Browse files
committed
Return error
1 parent dd5f8eb commit 808f3a5

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

internal/goutil/optional.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package goutil
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
)
78

9+
var ErrNotPresent = errors.New("Optional value is not present")
10+
811
// Optional represents a value that may or may not be present.
912
type Optional[T any] struct {
1013
value T
@@ -29,12 +32,13 @@ func (o Optional[T]) IsPresent() bool {
2932
return o.isSet
3033
}
3134

32-
// Get returns the value if present, or panics if not present.
33-
func (o Optional[T]) Get() T {
35+
// Get returns the value if present, or an error if not present.
36+
func (o Optional[T]) Get() (T, error) {
3437
if !o.isSet {
35-
panic("Optional value is not present")
38+
var zero T
39+
return zero, ErrNotPresent
3640
}
37-
return o.value
41+
return o.value, nil
3842
}
3943

4044
// OrElse returns the value if present, or the given default value if not present.

internal/goutil/optional_test.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ func TestOptional(t *testing.T) {
1111
t.Run("NewOptional", func(t *testing.T) {
1212
opt := NewOptional(42)
1313
assert.True(t, opt.IsPresent())
14-
assert.Equal(t, 42, opt.Get())
14+
value, err := opt.Get()
15+
assert.NoError(t, err)
16+
assert.Equal(t, 42, value)
1517
})
1618

1719
t.Run("Empty", func(t *testing.T) {
@@ -21,10 +23,13 @@ func TestOptional(t *testing.T) {
2123

2224
t.Run("Get", func(t *testing.T) {
2325
opt := NewOptional("test")
24-
assert.Equal(t, "test", opt.Get())
26+
value, err := opt.Get()
27+
assert.NoError(t, err)
28+
assert.Equal(t, "test", value)
2529

2630
emptyOpt := Empty[string]()
27-
assert.Panics(t, func() { emptyOpt.Get() })
31+
_, err = emptyOpt.Get()
32+
assert.Error(t, err)
2833
})
2934

3035
t.Run("OrElse", func(t *testing.T) {
@@ -54,7 +59,9 @@ func TestOptional(t *testing.T) {
5459
opt := NewOptional(3)
5560
mapped := opt.Map(func(v int) int { return v * 2 })
5661
assert.True(t, mapped.IsPresent())
57-
assert.Equal(t, 6, mapped.Get())
62+
value, err := mapped.Get()
63+
assert.NoError(t, err)
64+
assert.Equal(t, 6, value)
5865

5966
emptyOpt := Empty[int]()
6067
mappedEmpty := emptyOpt.Map(func(v int) int { return v * 2 })
@@ -75,7 +82,9 @@ func TestOptional(t *testing.T) {
7582
err := json.Unmarshal([]byte("42"), &opt)
7683
assert.NoError(t, err)
7784
assert.True(t, opt.IsPresent())
78-
assert.Equal(t, 42, opt.Get())
85+
value, err := opt.Get()
86+
assert.NoError(t, err)
87+
assert.Equal(t, 42, value)
7988

8089
err = json.Unmarshal([]byte("null"), &opt)
8190
assert.NoError(t, err)
@@ -106,9 +115,13 @@ func TestOptionalUnmarshalJSONInStruct(t *testing.T) {
106115
assert.NoError(t, err)
107116
assert.Equal(t, "John Doe", result.Name)
108117
assert.True(t, result.Age.IsPresent())
109-
assert.Equal(t, 30, result.Age.Get())
118+
ageValue, err := result.Age.Get()
119+
assert.NoError(t, err)
120+
assert.Equal(t, 30, ageValue)
110121
assert.True(t, result.Address.IsPresent())
111-
assert.Equal(t, "123 Main St", result.Address.Get())
122+
addressValue, err := result.Address.Get()
123+
assert.NoError(t, err)
124+
assert.Equal(t, "123 Main St", addressValue)
112125
})
113126

114127
t.Run("Missing optional values", func(t *testing.T) {

0 commit comments

Comments
 (0)