diff --git a/bool.go b/bool.go index fbc6f2b..17e0d7e 100644 --- a/bool.go +++ b/bool.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:53.447681 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:55.55595 +0000 UTC package optional @@ -32,6 +32,14 @@ func (b Bool) Get() (bool, error) { return *b.value, nil } +// MustGet returns the bool value or panics if not present. +func (b Bool) MustGet() bool { + if !b.Present() { + panic("value not present") + } + return *b.value +} + // Present returns whether or not the value is present. func (b Bool) Present() bool { return b.value != nil diff --git a/byte.go b/byte.go index 39409d8..ec24386 100644 --- a/byte.go +++ b/byte.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:53.79355 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:55.886598 +0000 UTC package optional @@ -32,6 +32,14 @@ func (b Byte) Get() (byte, error) { return *b.value, nil } +// MustGet returns the byte value or panics if not present. +func (b Byte) MustGet() byte { + if !b.Present() { + panic("value not present") + } + return *b.value +} + // Present returns whether or not the value is present. func (b Byte) Present() bool { return b.value != nil diff --git a/cmd/optional/main.go b/cmd/optional/main.go index 0c8ce56..123a435 100644 --- a/cmd/optional/main.go +++ b/cmd/optional/main.go @@ -159,6 +159,14 @@ func ({{ .VariableName }} {{ .OutputName }}) Get() ({{ .TypeName }}, error) { return *{{ .VariableName }}.value, nil } +// MustGet returns the {{ .TypeName }} value or panics if not present. +func ({{ .VariableName }} {{ .OutputName }}) MustGet() {{ .TypeName }} { + if !{{ .VariableName }}.Present() { + panic("value not present") + } + return *{{ .VariableName }}.value +} + // Present returns whether or not the value is present. func ({{ .VariableName }} {{ .OutputName }}) Present() bool { return {{ .VariableName }}.value != nil diff --git a/cmd/optional/testdata/optional_bar.go b/cmd/optional/testdata/optional_bar.go index 01c9307..a00f018 100644 --- a/cmd/optional/testdata/optional_bar.go +++ b/cmd/optional/testdata/optional_bar.go @@ -32,6 +32,14 @@ func (o optionalBar) Get() (bar, error) { return *o.value, nil } +// MustGet returns the bar value or panics if not present. +func (o optionalBar) MustGet() bar { + if !o.Present() { + panic("value not present") + } + return *o.value +} + // Present returns whether or not the value is present. func (o optionalBar) Present() bool { return o.value != nil diff --git a/cmd/optional/testdata/optional_foo.go b/cmd/optional/testdata/optional_foo.go index 18a2c24..bbd5006 100644 --- a/cmd/optional/testdata/optional_foo.go +++ b/cmd/optional/testdata/optional_foo.go @@ -32,6 +32,14 @@ func (o OptionalFoo) Get() (Foo, error) { return *o.value, nil } +// MustGet returns the Foo value or panics if not present. +func (o OptionalFoo) MustGet() Foo { + if !o.Present() { + panic("value not present") + } + return *o.value +} + // Present returns whether or not the value is present. func (o OptionalFoo) Present() bool { return o.value != nil diff --git a/cmd/optional/testdata/string.go b/cmd/optional/testdata/string.go index 64074b9..a96a59b 100644 --- a/cmd/optional/testdata/string.go +++ b/cmd/optional/testdata/string.go @@ -32,6 +32,14 @@ func (s String) Get() (string, error) { return *s.value, nil } +// MustGet returns the string value or panics if not present. +func (s String) MustGet() string { + if !s.Present() { + panic("value not present") + } + return *s.value +} + // Present returns whether or not the value is present. func (s String) Present() bool { return s.value != nil diff --git a/complex128.go b/complex128.go index b85dfbe..10a493f 100644 --- a/complex128.go +++ b/complex128.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:54.128811 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:56.218496 +0000 UTC package optional @@ -32,6 +32,14 @@ func (c Complex128) Get() (complex128, error) { return *c.value, nil } +// MustGet returns the complex128 value or panics if not present. +func (c Complex128) MustGet() complex128 { + if !c.Present() { + panic("value not present") + } + return *c.value +} + // Present returns whether or not the value is present. func (c Complex128) Present() bool { return c.value != nil diff --git a/complex64.go b/complex64.go index 04d5ee5..21e9d0c 100644 --- a/complex64.go +++ b/complex64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:54.464548 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:56.545094 +0000 UTC package optional @@ -32,6 +32,14 @@ func (c Complex64) Get() (complex64, error) { return *c.value, nil } +// MustGet returns the complex64 value or panics if not present. +func (c Complex64) MustGet() complex64 { + if !c.Present() { + panic("value not present") + } + return *c.value +} + // Present returns whether or not the value is present. func (c Complex64) Present() bool { return c.value != nil diff --git a/error.go b/error.go index bc79ddc..5e337e5 100644 --- a/error.go +++ b/error.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:54.804224 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:56.869019 +0000 UTC package optional @@ -32,6 +32,14 @@ func (e Error) Get() (error, error) { return *e.value, nil } +// MustGet returns the error value or panics if not present. +func (e Error) MustGet() error { + if !e.Present() { + panic("value not present") + } + return *e.value +} + // Present returns whether or not the value is present. func (e Error) Present() bool { return e.value != nil diff --git a/example_test.go b/example_test.go index 4c320cf..793ccbe 100644 --- a/example_test.go +++ b/example_test.go @@ -31,6 +31,30 @@ func Example_get() { // value not present } +func Example_mustGet() { + values := []optional.String{ + optional.NewString("foo"), + optional.NewString(""), + optional.NewString("bar"), + {}, + } + + for _, v := range values { + if v.Present() { + value := v.MustGet() + fmt.Println(value) + } else { + fmt.Println("not present") + } + } + + // Output: + // foo + // + // bar + // not present +} + func Example_orElse() { values := []optional.String{ optional.NewString("foo"), diff --git a/float32.go b/float32.go index 9f42c2c..a2375f1 100644 --- a/float32.go +++ b/float32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:55.1819 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:57.203008 +0000 UTC package optional @@ -32,6 +32,14 @@ func (f Float32) Get() (float32, error) { return *f.value, nil } +// MustGet returns the float32 value or panics if not present. +func (f Float32) MustGet() float32 { + if !f.Present() { + panic("value not present") + } + return *f.value +} + // Present returns whether or not the value is present. func (f Float32) Present() bool { return f.value != nil diff --git a/float64.go b/float64.go index 15966b6..363e98e 100644 --- a/float64.go +++ b/float64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:55.521255 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:57.529333 +0000 UTC package optional @@ -32,6 +32,14 @@ func (f Float64) Get() (float64, error) { return *f.value, nil } +// MustGet returns the float64 value or panics if not present. +func (f Float64) MustGet() float64 { + if !f.Present() { + panic("value not present") + } + return *f.value +} + // Present returns whether or not the value is present. func (f Float64) Present() bool { return f.value != nil diff --git a/int.go b/int.go index caead99..afffab8 100644 --- a/int.go +++ b/int.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:55.863991 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:57.857318 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int) Get() (int, error) { return *i.value, nil } +// MustGet returns the int value or panics if not present. +func (i Int) MustGet() int { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int) Present() bool { return i.value != nil diff --git a/int16.go b/int16.go index 9532996..50aee82 100644 --- a/int16.go +++ b/int16.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:56.212093 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:58.192797 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int16) Get() (int16, error) { return *i.value, nil } +// MustGet returns the int16 value or panics if not present. +func (i Int16) MustGet() int16 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int16) Present() bool { return i.value != nil diff --git a/int32.go b/int32.go index 07c212d..0fce691 100644 --- a/int32.go +++ b/int32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:56.641482 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:58.524835 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int32) Get() (int32, error) { return *i.value, nil } +// MustGet returns the int32 value or panics if not present. +func (i Int32) MustGet() int32 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int32) Present() bool { return i.value != nil diff --git a/int64.go b/int64.go index 7f8f01c..ba01140 100644 --- a/int64.go +++ b/int64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:57.002648 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:58.84888 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int64) Get() (int64, error) { return *i.value, nil } +// MustGet returns the int64 value or panics if not present. +func (i Int64) MustGet() int64 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int64) Present() bool { return i.value != nil diff --git a/int8.go b/int8.go index 182f814..89744be 100644 --- a/int8.go +++ b/int8.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:57.555216 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:59.176954 +0000 UTC package optional @@ -32,6 +32,14 @@ func (i Int8) Get() (int8, error) { return *i.value, nil } +// MustGet returns the int8 value or panics if not present. +func (i Int8) MustGet() int8 { + if !i.Present() { + panic("value not present") + } + return *i.value +} + // Present returns whether or not the value is present. func (i Int8) Present() bool { return i.value != nil diff --git a/int_test.go b/int_test.go index 254406d..25c1908 100644 --- a/int_test.go +++ b/int_test.go @@ -26,6 +26,21 @@ func TestInt_Get_NotPresent(t *testing.T) { assert.Equal(t, zero, v) } +func TestInt_MustGet_Present(t *testing.T) { + o := NewInt(42) + + v := o.MustGet() + assert.True(t, o.Present()) + assert.Equal(t, 42, v) +} + +func TestInt_MustGet_NotPresent(t *testing.T) { + o := Int{} + + assert.Panics(t, func(){ _ = o.MustGet() }) + assert.False(t, o.Present()) +} + func TestInt_OrElse_Present(t *testing.T) { o := NewInt(42) diff --git a/rune.go b/rune.go index 469de67..a107e1a 100644 --- a/rune.go +++ b/rune.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:57.969673 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:59.507908 +0000 UTC package optional @@ -32,6 +32,14 @@ func (r Rune) Get() (rune, error) { return *r.value, nil } +// MustGet returns the rune value or panics if not present. +func (r Rune) MustGet() rune { + if !r.Present() { + panic("value not present") + } + return *r.value +} + // Present returns whether or not the value is present. func (r Rune) Present() bool { return r.value != nil diff --git a/string.go b/string.go index 19e67c1..667e725 100644 --- a/string.go +++ b/string.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:58.516631 +0000 UTC +// This file was generated by robots at 2021-03-14 20:38:59.838754 +0000 UTC package optional @@ -32,6 +32,14 @@ func (s String) Get() (string, error) { return *s.value, nil } +// MustGet returns the string value or panics if not present. +func (s String) MustGet() string { + if !s.Present() { + panic("value not present") + } + return *s.value +} + // Present returns whether or not the value is present. func (s String) Present() bool { return s.value != nil diff --git a/string_test.go b/string_test.go index 0289a15..7083ba4 100644 --- a/string_test.go +++ b/string_test.go @@ -26,6 +26,23 @@ func TestString_Get_NotPresent(t *testing.T) { assert.Equal(t, zero, v) } + +func TestString_MustGet_Present(t *testing.T) { + o := NewString("foo") + + v := o.MustGet() + assert.True(t, o.Present()) + assert.Equal(t, "foo", v) +} + + +func TestString_MustGet_NotPresent(t *testing.T) { + o := String{} + + assert.Panics(t, func(){ _ = o.MustGet() }) + assert.False(t, o.Present()) +} + func TestString_OrElse_Present(t *testing.T) { o := NewString("foo") diff --git a/uint.go b/uint.go index 33e93e3..e1db9e5 100644 --- a/uint.go +++ b/uint.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:58.898667 +0000 UTC +// This file was generated by robots at 2021-03-14 20:39:00.171337 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint) Get() (uint, error) { return *u.value, nil } +// MustGet returns the uint value or panics if not present. +func (u Uint) MustGet() uint { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint) Present() bool { return u.value != nil diff --git a/uint16.go b/uint16.go index 428ecbf..bdb893f 100644 --- a/uint16.go +++ b/uint16.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:59.455534 +0000 UTC +// This file was generated by robots at 2021-03-14 20:39:00.505172 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint16) Get() (uint16, error) { return *u.value, nil } +// MustGet returns the uint16 value or panics if not present. +func (u Uint16) MustGet() uint16 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint16) Present() bool { return u.value != nil diff --git a/uint32.go b/uint32.go index 5fa2ebf..5567c30 100644 --- a/uint32.go +++ b/uint32.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:29:59.856381 +0000 UTC +// This file was generated by robots at 2021-03-14 20:39:00.839205 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint32) Get() (uint32, error) { return *u.value, nil } +// MustGet returns the uint32 value or panics if not present. +func (u Uint32) MustGet() uint32 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint32) Present() bool { return u.value != nil diff --git a/uint64.go b/uint64.go index f0d79a4..33fad3a 100644 --- a/uint64.go +++ b/uint64.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:30:00.317311 +0000 UTC +// This file was generated by robots at 2021-03-14 20:39:01.176137 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint64) Get() (uint64, error) { return *u.value, nil } +// MustGet returns the uint64 value or panics if not present. +func (u Uint64) MustGet() uint64 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint64) Present() bool { return u.value != nil diff --git a/uint8.go b/uint8.go index 9b2be7f..89bdc48 100644 --- a/uint8.go +++ b/uint8.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:30:00.699077 +0000 UTC +// This file was generated by robots at 2021-03-14 20:39:01.507433 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uint8) Get() (uint8, error) { return *u.value, nil } +// MustGet returns the uint8 value or panics if not present. +func (u Uint8) MustGet() uint8 { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uint8) Present() bool { return u.value != nil diff --git a/uintptr.go b/uintptr.go index af79fef..523fcd6 100644 --- a/uintptr.go +++ b/uintptr.go @@ -1,5 +1,5 @@ // Code generated by go generate -// This file was generated by robots at 2018-12-06 14:30:01.109101 +0000 UTC +// This file was generated by robots at 2021-03-14 20:39:01.845448 +0000 UTC package optional @@ -32,6 +32,14 @@ func (u Uintptr) Get() (uintptr, error) { return *u.value, nil } +// MustGet returns the uintptr value or panics if not present. +func (u Uintptr) MustGet() uintptr { + if !u.Present() { + panic("value not present") + } + return *u.value +} + // Present returns whether or not the value is present. func (u Uintptr) Present() bool { return u.value != nil