Skip to content

Commit e578825

Browse files
authored
Merge pull request #13 from lzap/dig1
Add a `GetOrEmpty` method on Nullable which will return the wrapped value, or a zerovalue if one hasn't been wrapped.
2 parents 55520a6 + 6fde78b commit e578825

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

nullable.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +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
54+
// Dig retrieves the underlying value or returns empty value if not present or was `null`. Use Get to distinguish between these cases.
55+
func (t Nullable[T]) GetOrEmpty() T {
56+
var empty T
57+
if !t.IsSpecified() || t.IsNull() {
58+
return empty
59+
}
60+
return t[true]
61+
}
62+
5563
func (t Nullable[T]) MustGet() T {
5664
v, err := t.Get()
5765
if err != nil {

nullable_example_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func ExampleNullable_unmarshalRequired() {
252252
return
253253
}
254254
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
255+
fmt.Printf("obj.Name.GetOrEmpty(): %#v\n", obj.Name.GetOrEmpty())
255256
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
256257
fmt.Println("---")
257258

@@ -275,6 +276,7 @@ func ExampleNullable_unmarshalRequired() {
275276
return
276277
}
277278
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
279+
fmt.Printf("obj.Name.GetOrEmpty(): %#v\n", obj.Name.GetOrEmpty())
278280
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
279281
fmt.Println("---")
280282

@@ -291,12 +293,14 @@ func ExampleNullable_unmarshalRequired() {
291293
// obj.Name.IsSpecified(): true
292294
// obj.Name.IsNull(): false
293295
// obj.Name.Get(): "" <nil>
296+
// obj.Name.GetOrEmpty(): ""
294297
// obj.Name.MustGet(): ""
295298
// ---
296299
// Value:
297300
// obj.Name.IsSpecified(): true
298301
// obj.Name.IsNull(): false
299302
// obj.Name.Get(): "foo" <nil>
303+
// obj.Name.GetOrEmpty(): "foo"
300304
// obj.Name.MustGet(): "foo"
301305
// ---
302306
}
@@ -355,6 +359,7 @@ func ExampleNullable_unmarshalOptional() {
355359
return
356360
}
357361
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
362+
fmt.Printf("obj.Name.GetOrEmpty(): %#v\n", obj.Name.GetOrEmpty())
358363
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
359364
fmt.Println("---")
360365

@@ -378,6 +383,7 @@ func ExampleNullable_unmarshalOptional() {
378383
return
379384
}
380385
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
386+
fmt.Printf("obj.Name.GetOrEmpty(): %#v\n", obj.Name.GetOrEmpty())
381387
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
382388
fmt.Println("---")
383389

@@ -394,12 +400,14 @@ func ExampleNullable_unmarshalOptional() {
394400
// obj.Name.IsSpecified(): true
395401
// obj.Name.IsNull(): false
396402
// obj.Name.Get(): "" <nil>
403+
// obj.Name.GetOrEmpty(): ""
397404
// obj.Name.MustGet(): ""
398405
// ---
399406
// Value:
400407
// obj.Name.IsSpecified(): true
401408
// obj.Name.IsNull(): false
402409
// obj.Name.Get(): "foo" <nil>
410+
// obj.Name.GetOrEmpty(): "foo"
403411
// obj.Name.MustGet(): "foo"
404412
// ---
405413
}

0 commit comments

Comments
 (0)