From 342d4715c58508141255b239d2a3d2069fb88832 Mon Sep 17 00:00:00 2001 From: AuroraTea <1352685369@qq.com> Date: Sat, 13 Jan 2024 10:28:13 +0800 Subject: [PATCH] Add `MustGet` function 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. --- internal/test/nullable_test.go | 2 ++ nullable.go | 9 +++++++++ nullable_example_test.go | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/internal/test/nullable_test.go b/internal/test/nullable_test.go index 745e89d..c12b568 100644 --- a/internal/test/nullable_test.go +++ b/internal/test/nullable_test.go @@ -26,6 +26,7 @@ func TestNullable(t *testing.T) { value, err := myObj.Foo.Get() require.NoError(t, err) require.Equal(t, "bar", value) + require.Equal(t, "bar", myObj.Foo.MustGet()) // serialize back to json: leads to the same data require.Equal(t, data, serialize(myObj, t)) @@ -50,6 +51,7 @@ func TestNullable(t *testing.T) { require.True(t, myObj.Foo.IsSpecified()) _, err = myObj.Foo.Get() require.ErrorContains(t, err, "value is null") + require.Panics(t, func() { myObj.Foo.MustGet() }) // serialize back to json: leads to the same data require.Equal(t, data, serialize(myObj, t)) diff --git a/nullable.go b/nullable.go index bb24671..d2e3238 100644 --- a/nullable.go +++ b/nullable.go @@ -51,6 +51,15 @@ func (t Nullable[T]) Get() (T, error) { return t[true], nil } +// MustGet retrieves the underlying value, if present, and panics if the value was not present +func (t Nullable[T]) MustGet() T { + v, err := t.Get() + if err != nil { + panic(err) + } + return v +} + // Set sets the underlying value to a given value func (t *Nullable[T]) Set(value T) { *t = map[bool]T{true: value} diff --git a/nullable_example_test.go b/nullable_example_test.go index 71b596a..c391817 100644 --- a/nullable_example_test.go +++ b/nullable_example_test.go @@ -252,6 +252,7 @@ func ExampleNullable_unmarshalRequired() { return } fmt.Printf("obj.Name.Get(): %#v \n", val) + fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet()) fmt.Println("---") // when it's set explicitly to a specific value @@ -274,6 +275,7 @@ func ExampleNullable_unmarshalRequired() { return } fmt.Printf("obj.Name.Get(): %#v \n", val) + fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet()) fmt.Println("---") // Output: @@ -289,11 +291,13 @@ func ExampleNullable_unmarshalRequired() { // obj.Name.IsSpecified(): true // obj.Name.IsNull(): false // obj.Name.Get(): "" + // obj.Name.MustGet(): "" // --- // Value: // obj.Name.IsSpecified(): true // obj.Name.IsNull(): false // obj.Name.Get(): "foo" + // obj.Name.MustGet(): "foo" // --- } @@ -351,6 +355,7 @@ func ExampleNullable_unmarshalOptional() { return } fmt.Printf("obj.Name.Get(): %#v \n", val) + fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet()) fmt.Println("---") // when it's set explicitly to a specific value @@ -373,6 +378,7 @@ func ExampleNullable_unmarshalOptional() { return } fmt.Printf("obj.Name.Get(): %#v \n", val) + fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet()) fmt.Println("---") // Output: @@ -388,10 +394,12 @@ func ExampleNullable_unmarshalOptional() { // obj.Name.IsSpecified(): true // obj.Name.IsNull(): false // obj.Name.Get(): "" + // obj.Name.MustGet(): "" // --- // Value: // obj.Name.IsSpecified(): true // obj.Name.IsNull(): false // obj.Name.Get(): "foo" + // obj.Name.MustGet(): "foo" // --- }