diff --git a/README.md b/README.md index 7f9133f..92cefa3 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ with optional values: ```go // Create an optional with a value. -opt := SomeOptionalString("hello") +opt := option.SomeString("hello") // Check if a value is present. if opt.IsSome() { @@ -79,6 +79,30 @@ value := opt.UnwrapOr("default") err := opt.EncodeMsgpack(encoder) ``` +### Using generic approach + +```go +type SomeType struct { + name string + number int +} + +// Create an optional with a value. +opt := option.Some(SomeType{"hello", 42}) + +// Check if a value is present. +if opt.IsSome() { + value := opt.Unwrap() + fmt.Println(value) +} + +// Use a default value if none. +value := opt.UnwrapOr(SomeType{"default", 0}) + +// Encode to MessagePack. +err := opt.EncodeMsgpack(encoder) +``` + ### Usage with go-tarantool It may be necessary to use an optional type in a structure. For example, @@ -147,7 +171,7 @@ while ensuring proper encoding and decoding when using MessagePack. - `SomeXxx(value)` - Create an optional with a value - `NoneXxx()` - Create an empty optional - `Unwrap()`, `UnwrapOr()`, `UnwrapOrElse()` - Value extraction - - `IsSome()`, `IsNone()` - Presence checking + - `IsSome()`, `IsNil()` - Presence checking - Full MessagePack `CustomEncoder` and `CustomDecoder` implementation - Type-safe operations @@ -240,12 +264,13 @@ For example, to generate an optional type for `github.com/google/uuid.UUID`: ### Using Generated Types -Generated types follow the pattern Optional and provide methods for working -with optional values: +Generated types provide methods for working with optional values and 2 constructors for every single type: +`Some` creates option with some value and `None` creates the empty one +( is the original type name started with upper case): ```go // Create an optional with a value. -opt := SomeOptionalString("hello") +opt := option.SomeString("hello") // Check if a value is present. if opt.IsSome() { @@ -329,7 +354,7 @@ At this point we can see already that the alternatives (based on pointer and sli Now let's check encoding and decoding. -## Encode + Decode +#### Encode + Decode ``` # int diff --git a/bool_gen.go b/bool_gen.go index ac419f7..1201fb1 100644 --- a/bool_gen.go +++ b/bool_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[bool] = (*Bool)(nil) // SomeBool creates an optional Bool with the given bool value. // The returned Bool will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeBool(true) -// if o.IsSome() { -// v := o.Unwrap() // v == true -// } func SomeBool(value bool) Bool { return Bool{ value: value, @@ -34,13 +27,6 @@ func SomeBool(value bool) Bool { // NoneBool creates an empty optional Bool value. // The returned Bool will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneBool() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneBool() Bool { return Bool{ exists: false, @@ -106,11 +92,6 @@ func (o Bool) Unwrap() bool { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneBool() -// v := o.UnwrapOr(someDefaultBool) func (o Bool) UnwrapOr(defaultValue bool) bool { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Bool) UnwrapOr(defaultValue bool) bool { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneBool() -// v := o.UnwrapOrElse(func() bool { return computeDefault() }) func (o Bool) UnwrapOrElse(defaultValue func() bool) bool { if o.exists { return o.value diff --git a/bool_gen_test.go b/bool_gen_test.go index cf374b1..3791809 100644 --- a/bool_gen_test.go +++ b/bool_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestBool_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeBool() { + opt := option.SomeBool(true) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: true +} + +func ExampleNoneBool() { + opt := option.NoneBool() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleBool_IsSome() { + some := option.SomeBool(true) + none := option.NoneBool() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleBool_IsZero() { + some := option.SomeBool(true) + none := option.NoneBool() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleBool_IsNil() { + some := option.SomeBool(true) + none := option.NoneBool() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleBool_Get() { + some := option.SomeBool(true) + none := option.NoneBool() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // true true + // false false +} + +func ExampleBool_MustGet() { + some := option.SomeBool(true) + fmt.Println(some.MustGet()) + // Output: true +} + +func ExampleBool_MustGet_panic() { + none := option.NoneBool() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleBool_Unwrap() { + some := option.SomeBool(true) + none := option.NoneBool() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // true + // false +} + +func ExampleBool_UnwrapOr() { + some := option.SomeBool(true) + none := option.NoneBool() + fmt.Println(some.UnwrapOr(false)) + fmt.Println(none.UnwrapOr(false)) + // Output: + // true + // false +} + +func ExampleBool_UnwrapOrElse() { + some := option.SomeBool(true) + none := option.NoneBool() + fmt.Println(some.UnwrapOrElse(func() bool { + return false + })) + fmt.Println(none.UnwrapOrElse(func() bool { + return false + })) + // Output: + // true + // false +} diff --git a/byte_gen.go b/byte_gen.go index 52edc11..47d5321 100644 --- a/byte_gen.go +++ b/byte_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[byte] = (*Byte)(nil) // SomeByte creates an optional Byte with the given byte value. // The returned Byte will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeByte(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeByte(value byte) Byte { return Byte{ value: value, @@ -34,13 +27,6 @@ func SomeByte(value byte) Byte { // NoneByte creates an empty optional Byte value. // The returned Byte will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneByte() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneByte() Byte { return Byte{ exists: false, @@ -106,11 +92,6 @@ func (o Byte) Unwrap() byte { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneByte() -// v := o.UnwrapOr(someDefaultByte) func (o Byte) UnwrapOr(defaultValue byte) byte { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Byte) UnwrapOr(defaultValue byte) byte { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneByte() -// v := o.UnwrapOrElse(func() byte { return computeDefault() }) func (o Byte) UnwrapOrElse(defaultValue func() byte) byte { if o.exists { return o.value diff --git a/byte_gen_test.go b/byte_gen_test.go index 4733e31..8f10531 100644 --- a/byte_gen_test.go +++ b/byte_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestByte_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeByte() { + opt := option.SomeByte(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneByte() { + opt := option.NoneByte() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleByte_IsSome() { + some := option.SomeByte(12) + none := option.NoneByte() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleByte_IsZero() { + some := option.SomeByte(12) + none := option.NoneByte() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleByte_IsNil() { + some := option.SomeByte(12) + none := option.NoneByte() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleByte_Get() { + some := option.SomeByte(12) + none := option.NoneByte() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleByte_MustGet() { + some := option.SomeByte(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleByte_MustGet_panic() { + none := option.NoneByte() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleByte_Unwrap() { + some := option.SomeByte(12) + none := option.NoneByte() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleByte_UnwrapOr() { + some := option.SomeByte(12) + none := option.NoneByte() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleByte_UnwrapOrElse() { + some := option.SomeByte(12) + none := option.NoneByte() + fmt.Println(some.UnwrapOrElse(func() byte { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() byte { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/bytes_gen.go b/bytes_gen.go index a994e8c..268f623 100644 --- a/bytes_gen.go +++ b/bytes_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[[]byte] = (*Bytes)(nil) // SomeBytes creates an optional Bytes with the given []byte value. // The returned Bytes will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeBytes([]byte("hello")) -// if o.IsSome() { -// v := o.Unwrap() // v == []byte("hello") -// } func SomeBytes(value []byte) Bytes { return Bytes{ value: value, @@ -34,13 +27,6 @@ func SomeBytes(value []byte) Bytes { // NoneBytes creates an empty optional Bytes value. // The returned Bytes will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneBytes() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneBytes() Bytes { return Bytes{ exists: false, @@ -106,11 +92,6 @@ func (o Bytes) Unwrap() []byte { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneBytes() -// v := o.UnwrapOr(someDefaultBytes) func (o Bytes) UnwrapOr(defaultValue []byte) []byte { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Bytes) UnwrapOr(defaultValue []byte) []byte { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneBytes() -// v := o.UnwrapOrElse(func() []byte { return computeDefault() }) func (o Bytes) UnwrapOrElse(defaultValue func() []byte) []byte { if o.exists { return o.value diff --git a/bytes_gen_test.go b/bytes_gen_test.go index 83c0190..8566df5 100644 --- a/bytes_gen_test.go +++ b/bytes_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -19,7 +20,7 @@ func TestBytes_IsSome(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) + someBytes := option.SomeBytes([]byte{3, 14, 15}) assert.True(t, someBytes.IsSome()) }) @@ -37,7 +38,7 @@ func TestBytes_IsZero(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) + someBytes := option.SomeBytes([]byte{3, 14, 15}) assert.False(t, someBytes.IsZero()) }) @@ -55,7 +56,7 @@ func TestBytes_IsNil(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) + someBytes := option.SomeBytes([]byte{3, 14, 15}) assert.False(t, someBytes.IsNil()) }) @@ -73,10 +74,10 @@ func TestBytes_Get(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) + someBytes := option.SomeBytes([]byte{3, 14, 15}) val, ok := someBytes.Get() require.True(t, ok) - assert.EqualValues(t, []byte("hello"), val) + assert.EqualValues(t, []byte{3, 14, 15}, val) }) t.Run("none", func(t *testing.T) { @@ -94,8 +95,8 @@ func TestBytes_MustGet(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) - assert.EqualValues(t, []byte("hello"), someBytes.MustGet()) + someBytes := option.SomeBytes([]byte{3, 14, 15}) + assert.EqualValues(t, []byte{3, 14, 15}, someBytes.MustGet()) }) t.Run("none", func(t *testing.T) { @@ -114,8 +115,8 @@ func TestBytes_Unwrap(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) - assert.EqualValues(t, []byte("hello"), someBytes.Unwrap()) + someBytes := option.SomeBytes([]byte{3, 14, 15}) + assert.EqualValues(t, []byte{3, 14, 15}, someBytes.Unwrap()) }) t.Run("none", func(t *testing.T) { @@ -134,15 +135,15 @@ func TestBytes_UnwrapOr(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) - assert.EqualValues(t, []byte("hello"), someBytes.UnwrapOr([]byte("henlo"))) + someBytes := option.SomeBytes([]byte{3, 14, 15}) + assert.EqualValues(t, []byte{3, 14, 15}, someBytes.UnwrapOr([]byte{3, 14, 15, 9, 26})) }) t.Run("none", func(t *testing.T) { t.Parallel() emptyBytes := option.NoneBytes() - assert.EqualValues(t, []byte("henlo"), emptyBytes.UnwrapOr([]byte("henlo"))) + assert.EqualValues(t, []byte{3, 14, 15, 9, 26}, emptyBytes.UnwrapOr([]byte{3, 14, 15, 9, 26})) }) } @@ -152,9 +153,9 @@ func TestBytes_UnwrapOrElse(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someBytes := option.SomeBytes([]byte("hello")) - assert.EqualValues(t, []byte("hello"), someBytes.UnwrapOrElse(func() []byte { - return []byte("henlo") + someBytes := option.SomeBytes([]byte{3, 14, 15}) + assert.EqualValues(t, []byte{3, 14, 15}, someBytes.UnwrapOrElse(func() []byte { + return []byte{3, 14, 15, 9, 26} })) }) @@ -162,8 +163,8 @@ func TestBytes_UnwrapOrElse(t *testing.T) { t.Parallel() emptyBytes := option.NoneBytes() - assert.EqualValues(t, []byte("henlo"), emptyBytes.UnwrapOrElse(func() []byte { - return []byte("henlo") + assert.EqualValues(t, []byte{3, 14, 15, 9, 26}, emptyBytes.UnwrapOrElse(func() []byte { + return []byte{3, 14, 15, 9, 26} })) }) } @@ -179,7 +180,7 @@ func TestBytes_EncodeDecodeMsgpack(t *testing.T) { enc := msgpack.NewEncoder(&buf) dec := msgpack.NewDecoder(&buf) - someBytes := option.SomeBytes([]byte("hello")) + someBytes := option.SomeBytes([]byte{3, 14, 15}) err := someBytes.EncodeMsgpack(enc) require.NoError(t, err) @@ -187,7 +188,7 @@ func TestBytes_EncodeDecodeMsgpack(t *testing.T) { err = unmarshaled.DecodeMsgpack(dec) require.NoError(t, err) assert.True(t, unmarshaled.IsSome()) - assert.EqualValues(t, []byte("hello"), unmarshaled.Unwrap()) + assert.EqualValues(t, []byte{3, 14, 15}, unmarshaled.Unwrap()) }) t.Run("none", func(t *testing.T) { @@ -209,3 +210,114 @@ func TestBytes_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeBytes() { + opt := option.SomeBytes([]byte{3, 14, 15}) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: [3 14 15] +} + +func ExampleNoneBytes() { + opt := option.NoneBytes() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleBytes_IsSome() { + some := option.SomeBytes([]byte{3, 14, 15}) + none := option.NoneBytes() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleBytes_IsZero() { + some := option.SomeBytes([]byte{3, 14, 15}) + none := option.NoneBytes() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleBytes_IsNil() { + some := option.SomeBytes([]byte{3, 14, 15}) + none := option.NoneBytes() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleBytes_Get() { + some := option.SomeBytes([]byte{3, 14, 15}) + none := option.NoneBytes() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // [3 14 15] true + // [] false +} + +func ExampleBytes_MustGet() { + some := option.SomeBytes([]byte{3, 14, 15}) + fmt.Println(some.MustGet()) + // Output: [3 14 15] +} + +func ExampleBytes_MustGet_panic() { + none := option.NoneBytes() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleBytes_Unwrap() { + some := option.SomeBytes([]byte{3, 14, 15}) + none := option.NoneBytes() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // [3 14 15] + // [] +} + +func ExampleBytes_UnwrapOr() { + some := option.SomeBytes([]byte{3, 14, 15}) + none := option.NoneBytes() + fmt.Println(some.UnwrapOr([]byte{3, 14, 15, 9, 26})) + fmt.Println(none.UnwrapOr([]byte{3, 14, 15, 9, 26})) + // Output: + // [3 14 15] + // [3 14 15 9 26] +} + +func ExampleBytes_UnwrapOrElse() { + some := option.SomeBytes([]byte{3, 14, 15}) + none := option.NoneBytes() + fmt.Println(some.UnwrapOrElse(func() []byte { + return []byte{3, 14, 15, 9, 26} + })) + fmt.Println(none.UnwrapOrElse(func() []byte { + return []byte{3, 14, 15, 9, 26} + })) + // Output: + // [3 14 15] + // [3 14 15 9 26] +} diff --git a/cmd/generator/generator.go b/cmd/generator/generator.go index 1a47b73..1f287a1 100644 --- a/cmd/generator/generator.go +++ b/cmd/generator/generator.go @@ -1,3 +1,4 @@ +//nolint:exhaustruct package main import ( @@ -30,8 +31,11 @@ type generatorDef struct { EncoderFunc string CheckerFunc string - TestingValue string - UnexpectedTestingValue string + TestingValue string + TestingValueOutput string + UnexpectedTestingValue string + UnexpectedTestingValueOutput string + ZeroTestingValueOutput string } func structToMap(def generatorDef) map[string]any { @@ -44,17 +48,34 @@ func structToMap(def generatorDef) map[string]any { "EncoderFunc": def.EncoderFunc, "CheckerFunc": def.CheckerFunc, - "TestingValue": def.TestingValue, - "UnexpectedTestingValue": def.UnexpectedTestingValue, + "TestingValue": def.TestingValue, + "TestingValueOutput": def.TestingValue, + "UnexpectedTestingValue": def.UnexpectedTestingValue, + "UnexpectedTestingValueOutput": def.UnexpectedTestingValue, + "ZeroTestingValueOutput": def.ZeroTestingValueOutput, } if def.Type != "" { out["Type"] = def.Type } + if def.TestingValueOutput != "" { + out["TestingValueOutput"] = def.TestingValueOutput + } + + if def.UnexpectedTestingValueOutput != "" { + out["UnexpectedTestingValueOutput"] = def.UnexpectedTestingValueOutput + } + return out } +func zeroOutput[T any]() string { + var zero T + + return fmt.Sprint(zero) +} + var defaultTypes = []generatorDef{ { Name: "byte", @@ -65,6 +86,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[byte](), }, { Name: "int", @@ -75,6 +97,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[int](), }, { Name: "int8", @@ -85,6 +108,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[int8](), }, { Name: "int16", @@ -95,6 +119,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[int16](), }, { Name: "int32", @@ -105,6 +130,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[int32](), }, { Name: "int64", @@ -115,6 +141,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[int64](), }, { Name: "uint", @@ -125,6 +152,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[uint](), }, { Name: "uint8", @@ -135,6 +163,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[uint8](), }, { Name: "uint16", @@ -145,6 +174,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[uint16](), }, { Name: "uint32", @@ -155,6 +185,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[uint32](), }, { Name: "uint64", @@ -165,6 +196,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[uint64](), }, { Name: "float32", @@ -175,6 +207,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[float32](), }, { Name: "float64", @@ -185,6 +218,7 @@ var defaultTypes = []generatorDef{ TestingValue: "12", UnexpectedTestingValue: "13", + ZeroTestingValueOutput: zeroOutput[float64](), }, { Name: "string", @@ -193,8 +227,11 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeString", CheckerFunc: "checkString", - TestingValue: "\"hello\"", - UnexpectedTestingValue: "\"henlo\"", + TestingValue: "\"hello\"", + TestingValueOutput: "hello", + UnexpectedTestingValue: "\"bye\"", + UnexpectedTestingValueOutput: "bye", + ZeroTestingValueOutput: zeroOutput[string](), }, { Name: "bytes", @@ -203,8 +240,11 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeBytes", CheckerFunc: "checkBytes", - TestingValue: "[]byte(\"hello\")", - UnexpectedTestingValue: "[]byte(\"henlo\")", + TestingValue: "[]byte{3, 14, 15}", + TestingValueOutput: "[3 14 15]", + UnexpectedTestingValue: "[]byte{3, 14, 15, 9, 26}", + UnexpectedTestingValueOutput: "[3 14 15 9 26]", + ZeroTestingValueOutput: zeroOutput[[]byte](), }, { Name: "bool", @@ -215,6 +255,7 @@ var defaultTypes = []generatorDef{ TestingValue: "true", UnexpectedTestingValue: "false", + ZeroTestingValueOutput: zeroOutput[bool](), }, } @@ -243,13 +284,6 @@ var _ commonInterface[{{.Type}}] = (*{{.Name}})(nil) // Some{{.Name}} creates an optional {{.Name}} with the given {{.Type}} value. // The returned {{.Name}} will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := Some{{.Name}}({{.TestingValue}}) -// if o.IsSome() { -// v := o.Unwrap() // v == {{.TestingValue}} -// } func Some{{.Name}}(value {{.Type}}) {{.Name}} { return {{.Name}}{ value: value, @@ -259,13 +293,6 @@ func Some{{.Name}}(value {{.Type}}) {{.Name}} { // None{{.Name}} creates an empty optional {{.Name}} value. // The returned {{.Name}} will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := None{{.Name}}() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func None{{.Name}}() {{.Name}} { return {{.Name}}{ exists: false, @@ -331,11 +358,6 @@ func (o {{.Name}}) Unwrap() {{.Type}} { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := None{{.Name}}() -// v := o.UnwrapOr(someDefault{{.Name}}) func (o {{.Name}}) UnwrapOr(defaultValue {{.Type}}) {{.Type}} { if o.exists { return o.value @@ -347,11 +369,6 @@ func (o {{.Name}}) UnwrapOr(defaultValue {{.Type}}) {{.Type}} { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := None{{.Name}}() -// v := o.UnwrapOrElse(func() {{.Type}} { return computeDefault() }) func (o {{.Name}}) UnwrapOrElse(defaultValue func() {{.Type}}) {{.Type}} { if o.exists { return o.value @@ -418,6 +435,7 @@ import ( {{ end }} "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -623,6 +641,117 @@ func Test{{.Name}}_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSome{{.Name}}() { + opt := option.Some{{.Name}}({{.TestingValue}}) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: {{.TestingValueOutput}} +} + +func ExampleNone{{.Name}}() { + opt := option.None{{.Name}}() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func Example{{.Name}}_IsSome() { + some := option.Some{{.Name}}({{.TestingValue}}) + none := option.None{{.Name}}() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func Example{{.Name}}_IsZero() { + some := option.Some{{.Name}}({{.TestingValue}}) + none := option.None{{.Name}}() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func Example{{.Name}}_IsNil() { + some := option.Some{{.Name}}({{.TestingValue}}) + none := option.None{{.Name}}() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func Example{{.Name}}_Get() { + some := option.Some{{.Name}}({{.TestingValue}}) + none := option.None{{.Name}}() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // {{.TestingValueOutput}} true + // {{.ZeroTestingValueOutput}} false +} + +func Example{{.Name}}_MustGet() { + some := option.Some{{.Name}}({{.TestingValue}}) + fmt.Println(some.MustGet()) + // Output: {{.TestingValueOutput}} +} + +func Example{{.Name}}_MustGet_panic() { + none := option.None{{.Name}}() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func Example{{.Name}}_Unwrap() { + some := option.Some{{.Name}}({{.TestingValue}}) + none := option.None{{.Name}}() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // {{.TestingValueOutput}} + // {{.ZeroTestingValueOutput}} +} + +func Example{{.Name}}_UnwrapOr() { + some := option.Some{{.Name}}({{.TestingValue}}) + none := option.None{{.Name}}() + fmt.Println(some.UnwrapOr({{.UnexpectedTestingValue}})) + fmt.Println(none.UnwrapOr({{.UnexpectedTestingValue}})) + // Output: + // {{.TestingValueOutput}} + // {{.UnexpectedTestingValueOutput}} +} + +func Example{{.Name}}_UnwrapOrElse() { + some := option.Some{{.Name}}({{.TestingValue}}) + none := option.None{{.Name}}() + fmt.Println(some.UnwrapOrElse(func() {{.Type}} { + return {{.UnexpectedTestingValue}} + })) + fmt.Println(none.UnwrapOrElse(func() {{.Type}} { + return {{.UnexpectedTestingValue}} + })) + // Output: + // {{.TestingValueOutput}} + // {{.UnexpectedTestingValueOutput}} +} ` func printFile(prefix string, data []byte) { diff --git a/float32_gen.go b/float32_gen.go index 262b394..d34a1a7 100644 --- a/float32_gen.go +++ b/float32_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[float32] = (*Float32)(nil) // SomeFloat32 creates an optional Float32 with the given float32 value. // The returned Float32 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeFloat32(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeFloat32(value float32) Float32 { return Float32{ value: value, @@ -34,13 +27,6 @@ func SomeFloat32(value float32) Float32 { // NoneFloat32 creates an empty optional Float32 value. // The returned Float32 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneFloat32() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneFloat32() Float32 { return Float32{ exists: false, @@ -106,11 +92,6 @@ func (o Float32) Unwrap() float32 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneFloat32() -// v := o.UnwrapOr(someDefaultFloat32) func (o Float32) UnwrapOr(defaultValue float32) float32 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Float32) UnwrapOr(defaultValue float32) float32 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneFloat32() -// v := o.UnwrapOrElse(func() float32 { return computeDefault() }) func (o Float32) UnwrapOrElse(defaultValue func() float32) float32 { if o.exists { return o.value diff --git a/float32_gen_test.go b/float32_gen_test.go index a623449..eba4c74 100644 --- a/float32_gen_test.go +++ b/float32_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestFloat32_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeFloat32() { + opt := option.SomeFloat32(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneFloat32() { + opt := option.NoneFloat32() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleFloat32_IsSome() { + some := option.SomeFloat32(12) + none := option.NoneFloat32() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleFloat32_IsZero() { + some := option.SomeFloat32(12) + none := option.NoneFloat32() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleFloat32_IsNil() { + some := option.SomeFloat32(12) + none := option.NoneFloat32() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleFloat32_Get() { + some := option.SomeFloat32(12) + none := option.NoneFloat32() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleFloat32_MustGet() { + some := option.SomeFloat32(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleFloat32_MustGet_panic() { + none := option.NoneFloat32() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleFloat32_Unwrap() { + some := option.SomeFloat32(12) + none := option.NoneFloat32() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleFloat32_UnwrapOr() { + some := option.SomeFloat32(12) + none := option.NoneFloat32() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleFloat32_UnwrapOrElse() { + some := option.SomeFloat32(12) + none := option.NoneFloat32() + fmt.Println(some.UnwrapOrElse(func() float32 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() float32 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/float64_gen.go b/float64_gen.go index 50a0a16..0c4f17d 100644 --- a/float64_gen.go +++ b/float64_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[float64] = (*Float64)(nil) // SomeFloat64 creates an optional Float64 with the given float64 value. // The returned Float64 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeFloat64(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeFloat64(value float64) Float64 { return Float64{ value: value, @@ -34,13 +27,6 @@ func SomeFloat64(value float64) Float64 { // NoneFloat64 creates an empty optional Float64 value. // The returned Float64 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneFloat64() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneFloat64() Float64 { return Float64{ exists: false, @@ -106,11 +92,6 @@ func (o Float64) Unwrap() float64 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneFloat64() -// v := o.UnwrapOr(someDefaultFloat64) func (o Float64) UnwrapOr(defaultValue float64) float64 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Float64) UnwrapOr(defaultValue float64) float64 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneFloat64() -// v := o.UnwrapOrElse(func() float64 { return computeDefault() }) func (o Float64) UnwrapOrElse(defaultValue func() float64) float64 { if o.exists { return o.value diff --git a/float64_gen_test.go b/float64_gen_test.go index bd3c13a..0445742 100644 --- a/float64_gen_test.go +++ b/float64_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestFloat64_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeFloat64() { + opt := option.SomeFloat64(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneFloat64() { + opt := option.NoneFloat64() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleFloat64_IsSome() { + some := option.SomeFloat64(12) + none := option.NoneFloat64() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleFloat64_IsZero() { + some := option.SomeFloat64(12) + none := option.NoneFloat64() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleFloat64_IsNil() { + some := option.SomeFloat64(12) + none := option.NoneFloat64() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleFloat64_Get() { + some := option.SomeFloat64(12) + none := option.NoneFloat64() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleFloat64_MustGet() { + some := option.SomeFloat64(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleFloat64_MustGet_panic() { + none := option.NoneFloat64() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleFloat64_Unwrap() { + some := option.SomeFloat64(12) + none := option.NoneFloat64() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleFloat64_UnwrapOr() { + some := option.SomeFloat64(12) + none := option.NoneFloat64() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleFloat64_UnwrapOrElse() { + some := option.SomeFloat64(12) + none := option.NoneFloat64() + fmt.Println(some.UnwrapOrElse(func() float64 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() float64 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/generic.go b/generic.go index b329ef8..a8af69d 100644 --- a/generic.go +++ b/generic.go @@ -30,11 +30,6 @@ var _ commonInterface[any] = (*Generic[any])(nil) // Some creates a Generic[T] containing the given value. // // The returned Generic is in the "some" state, meaning IsSome() will return true. -// -// Example: -// -// opt := option.Some("hello") -// fmt.Println(opt.IsSome()) // true func Some[T any](value T) Generic[T] { return Generic[T]{ value: value, @@ -45,33 +40,16 @@ func Some[T any](value T) Generic[T] { // None creates an Generic[T] that does not contain a value. // // The returned Generic is in the "none" state, meaning IsZero() will return true. -// -// Example: -// -// opt := option.None[int]() -// fmt.Println(opt.IsZero()) // true func None[T any]() Generic[T] { return Generic[T]{exists: false} //nolint:exhaustruct } // IsSome returns true if the optional contains a value. -// -// Example: -// -// if opt.IsSome() { -// // safely access value -// } func (o Generic[T]) IsSome() bool { return o.exists } // IsZero returns true if the optional does not contain a value. -// -// Example: -// -// if opt.IsZero() { -// log.Println("value is missing") -// } func (o Generic[T]) IsZero() bool { return !o.exists } @@ -87,15 +65,6 @@ func (o Generic[T]) IsNil() bool { // // This is the safest way to extract the value. The second return value is true if a value // is present, false otherwise. The first return value is the zero value of T when no value exists. -// -// Example: -// -// value, ok := opt.Get() -// if ok { -// process(value) -// } else { -// fmt.Println("no value available") -// } func (o Generic[T]) Get() (T, bool) { return o.value, o.exists } @@ -106,10 +75,6 @@ func (o Generic[T]) Get() (T, bool) { // // Only use this method when you are certain the value exists. // For safer access, use Get() instead. -// -// Example: -// -// value := opt.MustGet() // panics if value not set func (o Generic[T]) MustGet() T { if !o.exists { panic("optional value is not set") @@ -130,10 +95,6 @@ func (o Generic[T]) Unwrap() T { // UnwrapOr returns the contained value if present, otherwise returns the provided default value. // // This is useful when you want to provide a simple fallback value. -// -// Example: -// -// name := opt.UnwrapOr("default") func (o Generic[T]) UnwrapOr(defaultValue T) T { if o.exists { return o.value @@ -146,10 +107,6 @@ func (o Generic[T]) UnwrapOr(defaultValue T) T { // to compute a default value. // // This is useful when the default value is expensive to compute, or requires dynamic logic. -// -// Example: -// -// value := opt.UnwrapOrElse(fetchFromDatabase) func (o Generic[T]) UnwrapOrElse(defaultValueFunc func() T) T { if o.exists { return o.value diff --git a/generic_test.go b/generic_test.go index 24e1d46..08f7a5a 100644 --- a/generic_test.go +++ b/generic_test.go @@ -1,6 +1,7 @@ package option_test import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -10,128 +11,140 @@ import ( "github.com/tarantool/go-option" ) -// TestSomeAndIsSome verifies that Some() creates a valid optional and IsSome returns true. -func TestSomeAndIsSome(t *testing.T) { - t.Parallel() +func ExampleSome() { + opt := option.Some("hello") - opt := option.Some(42) - assert.True(t, opt.IsSome()) - assert.False(t, opt.IsZero()) - assert.False(t, opt.IsNil()) + fmt.Println(opt.IsSome()) + fmt.Println(opt.Unwrap()) + // Output: + // true + // hello } -// TestNoneAndIsZero verifies that None() creates an empty optional and IsZero/IsNil returns true. -func TestNoneAndIsZero(t *testing.T) { - t.Parallel() - +func ExampleNone() { opt := option.None[int]() - assert.True(t, opt.IsZero()) - assert.True(t, opt.IsNil()) - assert.False(t, opt.IsSome()) + + fmt.Println(opt.IsSome()) + fmt.Println(opt.Unwrap()) + // Output: + // false + // 0 } -// TestZeroValueIsZero verifies that the zero value of Generic[T] behaves as None. -func TestZeroValueIsZero(t *testing.T) { - t.Parallel() +func ExampleGeneric_IsSome() { + some := option.Some("hello") + none := option.None[string]() - var opt option.Generic[string] - assert.True(t, opt.IsZero()) - assert.True(t, opt.IsNil()) - assert.False(t, opt.IsSome()) + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false } -// TestGetWithValue verifies Get returns the value and true when present. -func TestGetWithValue(t *testing.T) { - t.Parallel() +func ExampleGeneric_IsZero() { + some := option.Some("hello") + none := option.None[string]() - opt := option.Some("hello") - value, ok := opt.Get() - assert.True(t, ok) - assert.Equal(t, "hello", value) + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true } -// TestGetWithoutValue verifies Get returns zero value and false when absent. -func TestGetWithoutValue(t *testing.T) { - t.Parallel() +func ExampleGeneric_IsNil() { + some := option.Some("hello") + none := option.None[string]() - opt := option.None[float64]() - value, ok := opt.Get() - assert.False(t, ok) - assert.InDelta(t, 0.0, value, 1e-6) // Zero value of float64. + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true } -// TestMustGetWithValue verifies MustGet returns the value when present. -func TestMustGetWithValue(t *testing.T) { - t.Parallel() +func ExampleGeneric_Get() { + some := option.Some(12) + none := option.None[int]() - opt := option.Some(99) - value := opt.MustGet() - assert.Equal(t, 99, value) -} + val, ok := some.Get() + fmt.Println(val, ok) -// TestMustGetPanic verifies MustGet panics when no value is present. -func TestMustGetPanic(t *testing.T) { - t.Parallel() + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} - opt := option.None[bool]() - assert.Panics(t, func() { opt.MustGet() }) //nolint:wsl_v5 +func ExampleGeneric_MustGet() { + some := option.Some(12) + fmt.Println(some.MustGet()) + // Output: 12 } -// TestUnwrapAlias verifies Unwrap is an alias for MustGet. -func TestUnwrapAlias(t *testing.T) { - t.Parallel() +func ExampleGeneric_MustGet_panic() { + none := option.None[int]() + eof := false + + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() - opt := option.Some("test") - assert.Equal(t, "test", opt.Unwrap()) + fmt.Println(none.MustGet()) - emptyOpt := option.None[string]() - assert.Empty(t, emptyOpt.Unwrap()) + eof = true + // Output: panic! optional value is not set } -// TestUnwrapOrWithValue verifies UnwrapOr returns inner value when present. -func TestUnwrapOrWithValue(t *testing.T) { - t.Parallel() +func ExampleGeneric_Unwrap() { + some := option.Some(12) + none := option.None[int]() - opt := option.Some("actual") - assert.Equal(t, "actual", opt.UnwrapOr("default")) + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 } -// TestUnwrapOrWithoutValue verifies UnwrapOr returns default when absent. -func TestUnwrapOrWithoutValue(t *testing.T) { - t.Parallel() +func ExampleGeneric_UnwrapOr() { + some := option.Some(12) + none := option.None[int]() - opt := option.None[string]() - assert.Equal(t, "default", opt.UnwrapOr("default")) + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 } -// TestUnwrapOrElseWithValue verifies UnwrapOrElse returns inner value when present. -func TestUnwrapOrElseWithValue(t *testing.T) { - t.Parallel() - - opt := option.Some(100) - result := opt.UnwrapOrElse(func() int { - return 200 // Should not be called. - }) - assert.Equal(t, 100, result) +func ExampleGeneric_UnwrapOrElse() { + some := option.Some(12) + none := option.None[int]() + + fmt.Println(some.UnwrapOrElse(func() int { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() int { + return 13 + })) + // Output: + // 12 + // 13 } -// TestUnwrapOrElseWithoutValue verifies UnwrapOrElse calls func when absent. -func TestUnwrapOrElseWithoutValue(t *testing.T) { +// TestZeroValueIsZero verifies that the zero value of Generic[T] behaves as None. +func TestZeroValueIsZero(t *testing.T) { t.Parallel() - var ( - called bool - opt = option.None[int]() - ) - - result := opt.UnwrapOrElse(func() int { - called = true - - return 42 - }) - - assert.True(t, called) - assert.Equal(t, 42, result) + var opt option.Generic[string] + assert.True(t, opt.IsZero()) + assert.True(t, opt.IsNil()) + assert.False(t, opt.IsSome()) } // TestMsgpackEncodeSome verifies that a Some value is correctly encoded to MessagePack. diff --git a/int16_gen.go b/int16_gen.go index e578b00..d43e708 100644 --- a/int16_gen.go +++ b/int16_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[int16] = (*Int16)(nil) // SomeInt16 creates an optional Int16 with the given int16 value. // The returned Int16 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeInt16(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeInt16(value int16) Int16 { return Int16{ value: value, @@ -34,13 +27,6 @@ func SomeInt16(value int16) Int16 { // NoneInt16 creates an empty optional Int16 value. // The returned Int16 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneInt16() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneInt16() Int16 { return Int16{ exists: false, @@ -106,11 +92,6 @@ func (o Int16) Unwrap() int16 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneInt16() -// v := o.UnwrapOr(someDefaultInt16) func (o Int16) UnwrapOr(defaultValue int16) int16 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Int16) UnwrapOr(defaultValue int16) int16 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneInt16() -// v := o.UnwrapOrElse(func() int16 { return computeDefault() }) func (o Int16) UnwrapOrElse(defaultValue func() int16) int16 { if o.exists { return o.value diff --git a/int16_gen_test.go b/int16_gen_test.go index 4a391ae..638e027 100644 --- a/int16_gen_test.go +++ b/int16_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestInt16_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeInt16() { + opt := option.SomeInt16(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneInt16() { + opt := option.NoneInt16() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleInt16_IsSome() { + some := option.SomeInt16(12) + none := option.NoneInt16() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleInt16_IsZero() { + some := option.SomeInt16(12) + none := option.NoneInt16() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleInt16_IsNil() { + some := option.SomeInt16(12) + none := option.NoneInt16() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleInt16_Get() { + some := option.SomeInt16(12) + none := option.NoneInt16() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleInt16_MustGet() { + some := option.SomeInt16(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleInt16_MustGet_panic() { + none := option.NoneInt16() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleInt16_Unwrap() { + some := option.SomeInt16(12) + none := option.NoneInt16() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleInt16_UnwrapOr() { + some := option.SomeInt16(12) + none := option.NoneInt16() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleInt16_UnwrapOrElse() { + some := option.SomeInt16(12) + none := option.NoneInt16() + fmt.Println(some.UnwrapOrElse(func() int16 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() int16 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/int32_gen.go b/int32_gen.go index e2d4a11..5b1384a 100644 --- a/int32_gen.go +++ b/int32_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[int32] = (*Int32)(nil) // SomeInt32 creates an optional Int32 with the given int32 value. // The returned Int32 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeInt32(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeInt32(value int32) Int32 { return Int32{ value: value, @@ -34,13 +27,6 @@ func SomeInt32(value int32) Int32 { // NoneInt32 creates an empty optional Int32 value. // The returned Int32 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneInt32() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneInt32() Int32 { return Int32{ exists: false, @@ -106,11 +92,6 @@ func (o Int32) Unwrap() int32 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneInt32() -// v := o.UnwrapOr(someDefaultInt32) func (o Int32) UnwrapOr(defaultValue int32) int32 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Int32) UnwrapOr(defaultValue int32) int32 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneInt32() -// v := o.UnwrapOrElse(func() int32 { return computeDefault() }) func (o Int32) UnwrapOrElse(defaultValue func() int32) int32 { if o.exists { return o.value diff --git a/int32_gen_test.go b/int32_gen_test.go index 841a26c..fdf0499 100644 --- a/int32_gen_test.go +++ b/int32_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestInt32_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeInt32() { + opt := option.SomeInt32(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneInt32() { + opt := option.NoneInt32() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleInt32_IsSome() { + some := option.SomeInt32(12) + none := option.NoneInt32() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleInt32_IsZero() { + some := option.SomeInt32(12) + none := option.NoneInt32() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleInt32_IsNil() { + some := option.SomeInt32(12) + none := option.NoneInt32() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleInt32_Get() { + some := option.SomeInt32(12) + none := option.NoneInt32() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleInt32_MustGet() { + some := option.SomeInt32(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleInt32_MustGet_panic() { + none := option.NoneInt32() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleInt32_Unwrap() { + some := option.SomeInt32(12) + none := option.NoneInt32() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleInt32_UnwrapOr() { + some := option.SomeInt32(12) + none := option.NoneInt32() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleInt32_UnwrapOrElse() { + some := option.SomeInt32(12) + none := option.NoneInt32() + fmt.Println(some.UnwrapOrElse(func() int32 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() int32 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/int64_gen.go b/int64_gen.go index 7c800ef..2c01679 100644 --- a/int64_gen.go +++ b/int64_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[int64] = (*Int64)(nil) // SomeInt64 creates an optional Int64 with the given int64 value. // The returned Int64 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeInt64(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeInt64(value int64) Int64 { return Int64{ value: value, @@ -34,13 +27,6 @@ func SomeInt64(value int64) Int64 { // NoneInt64 creates an empty optional Int64 value. // The returned Int64 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneInt64() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneInt64() Int64 { return Int64{ exists: false, @@ -106,11 +92,6 @@ func (o Int64) Unwrap() int64 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneInt64() -// v := o.UnwrapOr(someDefaultInt64) func (o Int64) UnwrapOr(defaultValue int64) int64 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Int64) UnwrapOr(defaultValue int64) int64 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneInt64() -// v := o.UnwrapOrElse(func() int64 { return computeDefault() }) func (o Int64) UnwrapOrElse(defaultValue func() int64) int64 { if o.exists { return o.value diff --git a/int64_gen_test.go b/int64_gen_test.go index 8ec8228..3db515e 100644 --- a/int64_gen_test.go +++ b/int64_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestInt64_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeInt64() { + opt := option.SomeInt64(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneInt64() { + opt := option.NoneInt64() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleInt64_IsSome() { + some := option.SomeInt64(12) + none := option.NoneInt64() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleInt64_IsZero() { + some := option.SomeInt64(12) + none := option.NoneInt64() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleInt64_IsNil() { + some := option.SomeInt64(12) + none := option.NoneInt64() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleInt64_Get() { + some := option.SomeInt64(12) + none := option.NoneInt64() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleInt64_MustGet() { + some := option.SomeInt64(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleInt64_MustGet_panic() { + none := option.NoneInt64() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleInt64_Unwrap() { + some := option.SomeInt64(12) + none := option.NoneInt64() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleInt64_UnwrapOr() { + some := option.SomeInt64(12) + none := option.NoneInt64() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleInt64_UnwrapOrElse() { + some := option.SomeInt64(12) + none := option.NoneInt64() + fmt.Println(some.UnwrapOrElse(func() int64 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() int64 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/int8_gen.go b/int8_gen.go index ede0695..b695329 100644 --- a/int8_gen.go +++ b/int8_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[int8] = (*Int8)(nil) // SomeInt8 creates an optional Int8 with the given int8 value. // The returned Int8 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeInt8(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeInt8(value int8) Int8 { return Int8{ value: value, @@ -34,13 +27,6 @@ func SomeInt8(value int8) Int8 { // NoneInt8 creates an empty optional Int8 value. // The returned Int8 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneInt8() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneInt8() Int8 { return Int8{ exists: false, @@ -106,11 +92,6 @@ func (o Int8) Unwrap() int8 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneInt8() -// v := o.UnwrapOr(someDefaultInt8) func (o Int8) UnwrapOr(defaultValue int8) int8 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Int8) UnwrapOr(defaultValue int8) int8 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneInt8() -// v := o.UnwrapOrElse(func() int8 { return computeDefault() }) func (o Int8) UnwrapOrElse(defaultValue func() int8) int8 { if o.exists { return o.value diff --git a/int8_gen_test.go b/int8_gen_test.go index c242be4..242c52e 100644 --- a/int8_gen_test.go +++ b/int8_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestInt8_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeInt8() { + opt := option.SomeInt8(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneInt8() { + opt := option.NoneInt8() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleInt8_IsSome() { + some := option.SomeInt8(12) + none := option.NoneInt8() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleInt8_IsZero() { + some := option.SomeInt8(12) + none := option.NoneInt8() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleInt8_IsNil() { + some := option.SomeInt8(12) + none := option.NoneInt8() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleInt8_Get() { + some := option.SomeInt8(12) + none := option.NoneInt8() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleInt8_MustGet() { + some := option.SomeInt8(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleInt8_MustGet_panic() { + none := option.NoneInt8() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleInt8_Unwrap() { + some := option.SomeInt8(12) + none := option.NoneInt8() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleInt8_UnwrapOr() { + some := option.SomeInt8(12) + none := option.NoneInt8() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleInt8_UnwrapOrElse() { + some := option.SomeInt8(12) + none := option.NoneInt8() + fmt.Println(some.UnwrapOrElse(func() int8 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() int8 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/int_gen.go b/int_gen.go index 5fa8504..542ab58 100644 --- a/int_gen.go +++ b/int_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[int] = (*Int)(nil) // SomeInt creates an optional Int with the given int value. // The returned Int will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeInt(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeInt(value int) Int { return Int{ value: value, @@ -34,13 +27,6 @@ func SomeInt(value int) Int { // NoneInt creates an empty optional Int value. // The returned Int will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneInt() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneInt() Int { return Int{ exists: false, @@ -106,11 +92,6 @@ func (o Int) Unwrap() int { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneInt() -// v := o.UnwrapOr(someDefaultInt) func (o Int) UnwrapOr(defaultValue int) int { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Int) UnwrapOr(defaultValue int) int { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneInt() -// v := o.UnwrapOrElse(func() int { return computeDefault() }) func (o Int) UnwrapOrElse(defaultValue func() int) int { if o.exists { return o.value diff --git a/int_gen_test.go b/int_gen_test.go index 02ecc11..8bef022 100644 --- a/int_gen_test.go +++ b/int_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestInt_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeInt() { + opt := option.SomeInt(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneInt() { + opt := option.NoneInt() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleInt_IsSome() { + some := option.SomeInt(12) + none := option.NoneInt() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleInt_IsZero() { + some := option.SomeInt(12) + none := option.NoneInt() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleInt_IsNil() { + some := option.SomeInt(12) + none := option.NoneInt() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleInt_Get() { + some := option.SomeInt(12) + none := option.NoneInt() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleInt_MustGet() { + some := option.SomeInt(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleInt_MustGet_panic() { + none := option.NoneInt() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleInt_Unwrap() { + some := option.SomeInt(12) + none := option.NoneInt() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleInt_UnwrapOr() { + some := option.SomeInt(12) + none := option.NoneInt() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleInt_UnwrapOrElse() { + some := option.SomeInt(12) + none := option.NoneInt() + fmt.Println(some.UnwrapOrElse(func() int { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() int { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/string_gen.go b/string_gen.go index cd7ab14..1af7c49 100644 --- a/string_gen.go +++ b/string_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[string] = (*String)(nil) // SomeString creates an optional String with the given string value. // The returned String will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeString("hello") -// if o.IsSome() { -// v := o.Unwrap() // v == "hello" -// } func SomeString(value string) String { return String{ value: value, @@ -34,13 +27,6 @@ func SomeString(value string) String { // NoneString creates an empty optional String value. // The returned String will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneString() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneString() String { return String{ exists: false, @@ -106,11 +92,6 @@ func (o String) Unwrap() string { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneString() -// v := o.UnwrapOr(someDefaultString) func (o String) UnwrapOr(defaultValue string) string { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o String) UnwrapOr(defaultValue string) string { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneString() -// v := o.UnwrapOrElse(func() string { return computeDefault() }) func (o String) UnwrapOrElse(defaultValue func() string) string { if o.exists { return o.value diff --git a/string_gen_test.go b/string_gen_test.go index af660c6..58c396a 100644 --- a/string_gen_test.go +++ b/string_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -135,14 +136,14 @@ func TestString_UnwrapOr(t *testing.T) { t.Parallel() someString := option.SomeString("hello") - assert.EqualValues(t, "hello", someString.UnwrapOr("henlo")) + assert.EqualValues(t, "hello", someString.UnwrapOr("bye")) }) t.Run("none", func(t *testing.T) { t.Parallel() emptyString := option.NoneString() - assert.EqualValues(t, "henlo", emptyString.UnwrapOr("henlo")) + assert.EqualValues(t, "bye", emptyString.UnwrapOr("bye")) }) } @@ -154,7 +155,7 @@ func TestString_UnwrapOrElse(t *testing.T) { someString := option.SomeString("hello") assert.EqualValues(t, "hello", someString.UnwrapOrElse(func() string { - return "henlo" + return "bye" })) }) @@ -162,8 +163,8 @@ func TestString_UnwrapOrElse(t *testing.T) { t.Parallel() emptyString := option.NoneString() - assert.EqualValues(t, "henlo", emptyString.UnwrapOrElse(func() string { - return "henlo" + assert.EqualValues(t, "bye", emptyString.UnwrapOrElse(func() string { + return "bye" })) }) } @@ -209,3 +210,114 @@ func TestString_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeString() { + opt := option.SomeString("hello") + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: hello +} + +func ExampleNoneString() { + opt := option.NoneString() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleString_IsSome() { + some := option.SomeString("hello") + none := option.NoneString() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleString_IsZero() { + some := option.SomeString("hello") + none := option.NoneString() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleString_IsNil() { + some := option.SomeString("hello") + none := option.NoneString() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleString_Get() { + some := option.SomeString("hello") + none := option.NoneString() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // hello true + // false +} + +func ExampleString_MustGet() { + some := option.SomeString("hello") + fmt.Println(some.MustGet()) + // Output: hello +} + +func ExampleString_MustGet_panic() { + none := option.NoneString() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleString_Unwrap() { + some := option.SomeString("hello") + none := option.NoneString() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // hello + // +} + +func ExampleString_UnwrapOr() { + some := option.SomeString("hello") + none := option.NoneString() + fmt.Println(some.UnwrapOr("bye")) + fmt.Println(none.UnwrapOr("bye")) + // Output: + // hello + // bye +} + +func ExampleString_UnwrapOrElse() { + some := option.SomeString("hello") + none := option.NoneString() + fmt.Println(some.UnwrapOrElse(func() string { + return "bye" + })) + fmt.Println(none.UnwrapOrElse(func() string { + return "bye" + })) + // Output: + // hello + // bye +} diff --git a/uint16_gen.go b/uint16_gen.go index 3e99de9..bf3e27c 100644 --- a/uint16_gen.go +++ b/uint16_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[uint16] = (*Uint16)(nil) // SomeUint16 creates an optional Uint16 with the given uint16 value. // The returned Uint16 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeUint16(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeUint16(value uint16) Uint16 { return Uint16{ value: value, @@ -34,13 +27,6 @@ func SomeUint16(value uint16) Uint16 { // NoneUint16 creates an empty optional Uint16 value. // The returned Uint16 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneUint16() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneUint16() Uint16 { return Uint16{ exists: false, @@ -106,11 +92,6 @@ func (o Uint16) Unwrap() uint16 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneUint16() -// v := o.UnwrapOr(someDefaultUint16) func (o Uint16) UnwrapOr(defaultValue uint16) uint16 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Uint16) UnwrapOr(defaultValue uint16) uint16 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneUint16() -// v := o.UnwrapOrElse(func() uint16 { return computeDefault() }) func (o Uint16) UnwrapOrElse(defaultValue func() uint16) uint16 { if o.exists { return o.value diff --git a/uint16_gen_test.go b/uint16_gen_test.go index 74115ee..8fe8fda 100644 --- a/uint16_gen_test.go +++ b/uint16_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestUint16_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeUint16() { + opt := option.SomeUint16(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneUint16() { + opt := option.NoneUint16() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleUint16_IsSome() { + some := option.SomeUint16(12) + none := option.NoneUint16() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleUint16_IsZero() { + some := option.SomeUint16(12) + none := option.NoneUint16() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleUint16_IsNil() { + some := option.SomeUint16(12) + none := option.NoneUint16() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleUint16_Get() { + some := option.SomeUint16(12) + none := option.NoneUint16() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleUint16_MustGet() { + some := option.SomeUint16(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleUint16_MustGet_panic() { + none := option.NoneUint16() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleUint16_Unwrap() { + some := option.SomeUint16(12) + none := option.NoneUint16() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleUint16_UnwrapOr() { + some := option.SomeUint16(12) + none := option.NoneUint16() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleUint16_UnwrapOrElse() { + some := option.SomeUint16(12) + none := option.NoneUint16() + fmt.Println(some.UnwrapOrElse(func() uint16 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() uint16 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/uint32_gen.go b/uint32_gen.go index 3fdee12..6e05acc 100644 --- a/uint32_gen.go +++ b/uint32_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[uint32] = (*Uint32)(nil) // SomeUint32 creates an optional Uint32 with the given uint32 value. // The returned Uint32 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeUint32(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeUint32(value uint32) Uint32 { return Uint32{ value: value, @@ -34,13 +27,6 @@ func SomeUint32(value uint32) Uint32 { // NoneUint32 creates an empty optional Uint32 value. // The returned Uint32 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneUint32() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneUint32() Uint32 { return Uint32{ exists: false, @@ -106,11 +92,6 @@ func (o Uint32) Unwrap() uint32 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneUint32() -// v := o.UnwrapOr(someDefaultUint32) func (o Uint32) UnwrapOr(defaultValue uint32) uint32 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Uint32) UnwrapOr(defaultValue uint32) uint32 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneUint32() -// v := o.UnwrapOrElse(func() uint32 { return computeDefault() }) func (o Uint32) UnwrapOrElse(defaultValue func() uint32) uint32 { if o.exists { return o.value diff --git a/uint32_gen_test.go b/uint32_gen_test.go index 1acb84d..ed1a8e9 100644 --- a/uint32_gen_test.go +++ b/uint32_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestUint32_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeUint32() { + opt := option.SomeUint32(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneUint32() { + opt := option.NoneUint32() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleUint32_IsSome() { + some := option.SomeUint32(12) + none := option.NoneUint32() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleUint32_IsZero() { + some := option.SomeUint32(12) + none := option.NoneUint32() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleUint32_IsNil() { + some := option.SomeUint32(12) + none := option.NoneUint32() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleUint32_Get() { + some := option.SomeUint32(12) + none := option.NoneUint32() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleUint32_MustGet() { + some := option.SomeUint32(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleUint32_MustGet_panic() { + none := option.NoneUint32() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleUint32_Unwrap() { + some := option.SomeUint32(12) + none := option.NoneUint32() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleUint32_UnwrapOr() { + some := option.SomeUint32(12) + none := option.NoneUint32() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleUint32_UnwrapOrElse() { + some := option.SomeUint32(12) + none := option.NoneUint32() + fmt.Println(some.UnwrapOrElse(func() uint32 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() uint32 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/uint64_gen.go b/uint64_gen.go index 91264da..47f0177 100644 --- a/uint64_gen.go +++ b/uint64_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[uint64] = (*Uint64)(nil) // SomeUint64 creates an optional Uint64 with the given uint64 value. // The returned Uint64 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeUint64(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeUint64(value uint64) Uint64 { return Uint64{ value: value, @@ -34,13 +27,6 @@ func SomeUint64(value uint64) Uint64 { // NoneUint64 creates an empty optional Uint64 value. // The returned Uint64 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneUint64() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneUint64() Uint64 { return Uint64{ exists: false, @@ -106,11 +92,6 @@ func (o Uint64) Unwrap() uint64 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneUint64() -// v := o.UnwrapOr(someDefaultUint64) func (o Uint64) UnwrapOr(defaultValue uint64) uint64 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Uint64) UnwrapOr(defaultValue uint64) uint64 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneUint64() -// v := o.UnwrapOrElse(func() uint64 { return computeDefault() }) func (o Uint64) UnwrapOrElse(defaultValue func() uint64) uint64 { if o.exists { return o.value diff --git a/uint64_gen_test.go b/uint64_gen_test.go index 484c842..8fdc4b4 100644 --- a/uint64_gen_test.go +++ b/uint64_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestUint64_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeUint64() { + opt := option.SomeUint64(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneUint64() { + opt := option.NoneUint64() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleUint64_IsSome() { + some := option.SomeUint64(12) + none := option.NoneUint64() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleUint64_IsZero() { + some := option.SomeUint64(12) + none := option.NoneUint64() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleUint64_IsNil() { + some := option.SomeUint64(12) + none := option.NoneUint64() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleUint64_Get() { + some := option.SomeUint64(12) + none := option.NoneUint64() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleUint64_MustGet() { + some := option.SomeUint64(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleUint64_MustGet_panic() { + none := option.NoneUint64() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleUint64_Unwrap() { + some := option.SomeUint64(12) + none := option.NoneUint64() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleUint64_UnwrapOr() { + some := option.SomeUint64(12) + none := option.NoneUint64() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleUint64_UnwrapOrElse() { + some := option.SomeUint64(12) + none := option.NoneUint64() + fmt.Println(some.UnwrapOrElse(func() uint64 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() uint64 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/uint8_gen.go b/uint8_gen.go index dfc338d..7fd66ed 100644 --- a/uint8_gen.go +++ b/uint8_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[uint8] = (*Uint8)(nil) // SomeUint8 creates an optional Uint8 with the given uint8 value. // The returned Uint8 will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeUint8(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeUint8(value uint8) Uint8 { return Uint8{ value: value, @@ -34,13 +27,6 @@ func SomeUint8(value uint8) Uint8 { // NoneUint8 creates an empty optional Uint8 value. // The returned Uint8 will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneUint8() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneUint8() Uint8 { return Uint8{ exists: false, @@ -106,11 +92,6 @@ func (o Uint8) Unwrap() uint8 { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneUint8() -// v := o.UnwrapOr(someDefaultUint8) func (o Uint8) UnwrapOr(defaultValue uint8) uint8 { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Uint8) UnwrapOr(defaultValue uint8) uint8 { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneUint8() -// v := o.UnwrapOrElse(func() uint8 { return computeDefault() }) func (o Uint8) UnwrapOrElse(defaultValue func() uint8) uint8 { if o.exists { return o.value diff --git a/uint8_gen_test.go b/uint8_gen_test.go index c909ac1..05d93f2 100644 --- a/uint8_gen_test.go +++ b/uint8_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestUint8_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeUint8() { + opt := option.SomeUint8(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneUint8() { + opt := option.NoneUint8() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleUint8_IsSome() { + some := option.SomeUint8(12) + none := option.NoneUint8() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleUint8_IsZero() { + some := option.SomeUint8(12) + none := option.NoneUint8() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleUint8_IsNil() { + some := option.SomeUint8(12) + none := option.NoneUint8() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleUint8_Get() { + some := option.SomeUint8(12) + none := option.NoneUint8() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleUint8_MustGet() { + some := option.SomeUint8(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleUint8_MustGet_panic() { + none := option.NoneUint8() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleUint8_Unwrap() { + some := option.SomeUint8(12) + none := option.NoneUint8() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleUint8_UnwrapOr() { + some := option.SomeUint8(12) + none := option.NoneUint8() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleUint8_UnwrapOrElse() { + some := option.SomeUint8(12) + none := option.NoneUint8() + fmt.Println(some.UnwrapOrElse(func() uint8 { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() uint8 { + return 13 + })) + // Output: + // 12 + // 13 +} diff --git a/uint_gen.go b/uint_gen.go index 53c95aa..409b492 100644 --- a/uint_gen.go +++ b/uint_gen.go @@ -18,13 +18,6 @@ var _ commonInterface[uint] = (*Uint)(nil) // SomeUint creates an optional Uint with the given uint value. // The returned Uint will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeUint(12) -// if o.IsSome() { -// v := o.Unwrap() // v == 12 -// } func SomeUint(value uint) Uint { return Uint{ value: value, @@ -34,13 +27,6 @@ func SomeUint(value uint) Uint { // NoneUint creates an empty optional Uint value. // The returned Uint will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneUint() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneUint() Uint { return Uint{ exists: false, @@ -106,11 +92,6 @@ func (o Uint) Unwrap() uint { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneUint() -// v := o.UnwrapOr(someDefaultUint) func (o Uint) UnwrapOr(defaultValue uint) uint { if o.exists { return o.value @@ -122,11 +103,6 @@ func (o Uint) UnwrapOr(defaultValue uint) uint { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneUint() -// v := o.UnwrapOrElse(func() uint { return computeDefault() }) func (o Uint) UnwrapOrElse(defaultValue func() uint) uint { if o.exists { return o.value diff --git a/uint_gen_test.go b/uint_gen_test.go index 0324e67..3e984f5 100644 --- a/uint_gen_test.go +++ b/uint_gen_test.go @@ -4,6 +4,7 @@ package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -209,3 +210,114 @@ func TestUint_EncodeDecodeMsgpack(t *testing.T) { assert.False(t, unmarshaled.IsSome()) }) } + +func ExampleSomeUint() { + opt := option.SomeUint(12) + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: 12 +} + +func ExampleNoneUint() { + opt := option.NoneUint() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleUint_IsSome() { + some := option.SomeUint(12) + none := option.NoneUint() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleUint_IsZero() { + some := option.SomeUint(12) + none := option.NoneUint() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleUint_IsNil() { + some := option.SomeUint(12) + none := option.NoneUint() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleUint_Get() { + some := option.SomeUint(12) + none := option.NoneUint() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // 12 true + // 0 false +} + +func ExampleUint_MustGet() { + some := option.SomeUint(12) + fmt.Println(some.MustGet()) + // Output: 12 +} + +func ExampleUint_MustGet_panic() { + none := option.NoneUint() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleUint_Unwrap() { + some := option.SomeUint(12) + none := option.NoneUint() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // 12 + // 0 +} + +func ExampleUint_UnwrapOr() { + some := option.SomeUint(12) + none := option.NoneUint() + fmt.Println(some.UnwrapOr(13)) + fmt.Println(none.UnwrapOr(13)) + // Output: + // 12 + // 13 +} + +func ExampleUint_UnwrapOrElse() { + some := option.SomeUint(12) + none := option.NoneUint() + fmt.Println(some.UnwrapOrElse(func() uint { + return 13 + })) + fmt.Println(none.UnwrapOrElse(func() uint { + return 13 + })) + // Output: + // 12 + // 13 +}