Skip to content

Commit 5c8aab1

Browse files
authored
Add MustGet function (#2)
In the cases where a user has explicitly checked `IsSpecified()` and `!IsNull()`, calling `Get` only to discard the `err` can be a bit awkward, so instead we can introduce a `MustGet` method to retrieve the value and panic if there is an error, as is common to do with Go libraries.
1 parent 6fb6cb7 commit 5c8aab1

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

internal/test/nullable_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestNullable(t *testing.T) {
2626
value, err := myObj.Foo.Get()
2727
require.NoError(t, err)
2828
require.Equal(t, "bar", value)
29+
require.Equal(t, "bar", myObj.Foo.MustGet())
2930
// serialize back to json: leads to the same data
3031
require.Equal(t, data, serialize(myObj, t))
3132

@@ -50,6 +51,7 @@ func TestNullable(t *testing.T) {
5051
require.True(t, myObj.Foo.IsSpecified())
5152
_, err = myObj.Foo.Get()
5253
require.ErrorContains(t, err, "value is null")
54+
require.Panics(t, func() { myObj.Foo.MustGet() })
5355
// serialize back to json: leads to the same data
5456
require.Equal(t, data, serialize(myObj, t))
5557

nullable.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func (t Nullable[T]) Get() (T, error) {
5151
return t[true], nil
5252
}
5353

54+
// MustGet retrieves the underlying value, if present, and panics if the value was not present
55+
func (t Nullable[T]) MustGet() T {
56+
v, err := t.Get()
57+
if err != nil {
58+
panic(err)
59+
}
60+
return v
61+
}
62+
5463
// Set sets the underlying value to a given value
5564
func (t *Nullable[T]) Set(value T) {
5665
*t = map[bool]T{true: value}

nullable_example_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func ExampleNullable_unmarshalRequired() {
252252
return
253253
}
254254
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
255+
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
255256
fmt.Println("---")
256257

257258
// when it's set explicitly to a specific value
@@ -274,6 +275,7 @@ func ExampleNullable_unmarshalRequired() {
274275
return
275276
}
276277
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
278+
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
277279
fmt.Println("---")
278280

279281
// Output:
@@ -289,11 +291,13 @@ func ExampleNullable_unmarshalRequired() {
289291
// obj.Name.IsSpecified(): true
290292
// obj.Name.IsNull(): false
291293
// obj.Name.Get(): "" <nil>
294+
// obj.Name.MustGet(): ""
292295
// ---
293296
// Value:
294297
// obj.Name.IsSpecified(): true
295298
// obj.Name.IsNull(): false
296299
// obj.Name.Get(): "foo" <nil>
300+
// obj.Name.MustGet(): "foo"
297301
// ---
298302
}
299303

@@ -351,6 +355,7 @@ func ExampleNullable_unmarshalOptional() {
351355
return
352356
}
353357
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
358+
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
354359
fmt.Println("---")
355360

356361
// when it's set explicitly to a specific value
@@ -373,6 +378,7 @@ func ExampleNullable_unmarshalOptional() {
373378
return
374379
}
375380
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
381+
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
376382
fmt.Println("---")
377383

378384
// Output:
@@ -388,10 +394,12 @@ func ExampleNullable_unmarshalOptional() {
388394
// obj.Name.IsSpecified(): true
389395
// obj.Name.IsNull(): false
390396
// obj.Name.Get(): "" <nil>
397+
// obj.Name.MustGet(): ""
391398
// ---
392399
// Value:
393400
// obj.Name.IsSpecified(): true
394401
// obj.Name.IsNull(): false
395402
// obj.Name.Get(): "foo" <nil>
403+
// obj.Name.MustGet(): "foo"
396404
// ---
397405
}

0 commit comments

Comments
 (0)