Skip to content

Commit

Permalink
Add MustGet function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AuroraTea committed Jan 25, 2024
1 parent bd57468 commit 342d471
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/test/nullable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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))

Expand Down
9 changes: 9 additions & 0 deletions nullable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 8 additions & 0 deletions nullable_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func ExampleNullable_unmarshalRequired() {
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")

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

// Output:
Expand All @@ -289,11 +291,13 @@ func ExampleNullable_unmarshalRequired() {
// obj.Name.IsSpecified(): true
// obj.Name.IsNull(): false
// obj.Name.Get(): "" <nil>
// obj.Name.MustGet(): ""
// ---
// Value:
// obj.Name.IsSpecified(): true
// obj.Name.IsNull(): false
// obj.Name.Get(): "foo" <nil>
// obj.Name.MustGet(): "foo"
// ---
}

Expand Down Expand Up @@ -351,6 +355,7 @@ func ExampleNullable_unmarshalOptional() {
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")

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

// Output:
Expand All @@ -388,10 +394,12 @@ func ExampleNullable_unmarshalOptional() {
// obj.Name.IsSpecified(): true
// obj.Name.IsNull(): false
// obj.Name.Get(): "" <nil>
// obj.Name.MustGet(): ""
// ---
// Value:
// obj.Name.IsSpecified(): true
// obj.Name.IsNull(): false
// obj.Name.Get(): "foo" <nil>
// obj.Name.MustGet(): "foo"
// ---
}

0 comments on commit 342d471

Please sign in to comment.