Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
// ---
}