Skip to content

Commit 1559c7d

Browse files
committed
feat: add function MustGet
1 parent bd57468 commit 1559c7d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ func ExampleNullable_unmarshalOptional() {
351351
return
352352
}
353353
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
354+
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
354355
fmt.Println("---")
355356

356357
// when it's set explicitly to a specific value
@@ -373,6 +374,7 @@ func ExampleNullable_unmarshalOptional() {
373374
return
374375
}
375376
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
377+
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
376378
fmt.Println("---")
377379

378380
// Output:
@@ -388,10 +390,12 @@ func ExampleNullable_unmarshalOptional() {
388390
// obj.Name.IsSpecified(): true
389391
// obj.Name.IsNull(): false
390392
// obj.Name.Get(): "" <nil>
393+
// obj.Name.MustGet(): ""
391394
// ---
392395
// Value:
393396
// obj.Name.IsSpecified(): true
394397
// obj.Name.IsNull(): false
395398
// obj.Name.Get(): "foo" <nil>
399+
// obj.Name.MustGet(): "foo"
396400
// ---
397401
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy