Skip to content

feat: new method for fetching ignoring error #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion nullable.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +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
// Dig retrieves the underlying value or returns empty value if not present or was `null`. Use Get to distinguish between these cases.
func (t Nullable[T]) Dig() T {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetOrEmpty might be a better name

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still quite long, but definitely better than GetWithoutError. Maybe if there was also an alias method named Gety that would be great :-)

var empty T
if !t.IsSpecified() || t.IsNull() {
return empty
}
return t[true]
}

func (t Nullable[T]) MustGet() T {
v, err := t.Get()
if err != nil {
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.Dig(): %#v\n", obj.Name.Dig())
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")

Expand All @@ -275,6 +276,7 @@ func ExampleNullable_unmarshalRequired() {
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.Dig(): %#v\n", obj.Name.Dig())
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")

Expand All @@ -291,12 +293,14 @@ func ExampleNullable_unmarshalRequired() {
// obj.Name.IsSpecified(): true
// obj.Name.IsNull(): false
// obj.Name.Get(): "" <nil>
// obj.Name.Dig(): ""
// obj.Name.MustGet(): ""
// ---
// Value:
// obj.Name.IsSpecified(): true
// obj.Name.IsNull(): false
// obj.Name.Get(): "foo" <nil>
// obj.Name.Dig(): "foo"
// obj.Name.MustGet(): "foo"
// ---
}
Expand Down Expand Up @@ -355,6 +359,7 @@ func ExampleNullable_unmarshalOptional() {
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.Dig(): %#v\n", obj.Name.Dig())
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")

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

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