Change naming (#177)

Utilize terminology from the Go SDK rather than introducing novel terms to describe concepts.
This commit is contained in:
donutloop
2024-02-24 10:25:31 +01:00
committed by GitHub
parent 860a499f98
commit fdc93c8cc7
4 changed files with 178 additions and 188 deletions

View File

@@ -10,8 +10,8 @@ type Optional[T any] struct {
mu *sync.RWMutex mu *sync.RWMutex
} }
// Empty returns an empty Optional instance. // Default returns an default Optional instance.
func Empty[T any]() Optional[T] { func Default[T any]() Optional[T] {
return Optional[T]{mu: &sync.RWMutex{}} return Optional[T]{mu: &sync.RWMutex{}}
} }
@@ -20,29 +20,29 @@ func Of[T any](value T) Optional[T] {
return Optional[T]{value: &value, mu: &sync.RWMutex{}} return Optional[T]{value: &value, mu: &sync.RWMutex{}}
} }
// OfNullable returns an Optional for a given value, which may be nil. // FromNillable returns an Optional for a given value, which may be nil.
func OfNullable[T any](value *T) Optional[T] { func FromNillable[T any](value *T) Optional[T] {
if value == nil { if value == nil {
return Empty[T]() return Default[T]()
} }
return Optional[T]{value: value, mu: &sync.RWMutex{}} return Optional[T]{value: value, mu: &sync.RWMutex{}}
} }
// IsPresent checks if there is a value present. // IsNotNil checks if there is a value present.
func (o Optional[T]) IsPresent() bool { func (o Optional[T]) IsNotNil() bool {
o.mu.RLock() o.mu.RLock()
defer o.mu.RUnlock() defer o.mu.RUnlock()
return o.value != nil return o.value != nil
} }
// IsEmpty checks if the Optional is empty. // IsNil checks if the Optional is nil.
func (o Optional[T]) IsEmpty() bool { func (o Optional[T]) IsNil() bool {
return !o.IsPresent() return !o.IsNotNil()
} }
// IfPresent performs the given action with the value if a value is present. // IfNotNil performs the given action with the value if a value is not nil.
func (o Optional[T]) IfPresent(action func(value T)) { func (o Optional[T]) IfNotNil(action func(value T)) {
o.mu.RLock() o.mu.RLock()
defer o.mu.RUnlock() defer o.mu.RUnlock()
@@ -51,20 +51,20 @@ func (o Optional[T]) IfPresent(action func(value T)) {
} }
} }
// IfPresentOrElse performs the action with the value if present, otherwise performs the empty-based action. // IfNotNilOrElse performs the action with the value if present, otherwise performs the fallback action.
func (o Optional[T]) IfPresentOrElse(action func(value T), emptyAction func()) { func (o Optional[T]) IfNotNilOrElse(action func(value T), fallbackAction func()) {
o.mu.RLock() o.mu.RLock()
defer o.mu.RUnlock() defer o.mu.RUnlock()
if o.value != nil { if o.value != nil {
action(*o.value) action(*o.value)
} else { } else {
emptyAction() fallbackAction()
} }
} }
// Get returns the value if present, otherwise panics. // Unwarp returns the value if not nil, otherwise panics.
func (o Optional[T]) Get() T { func (o Optional[T]) Unwarp() T {
o.mu.RLock() o.mu.RLock()
defer o.mu.RUnlock() defer o.mu.RUnlock()
@@ -74,7 +74,7 @@ func (o Optional[T]) Get() T {
return *o.value return *o.value
} }
// OrElse returns the value if present, otherwise returns other. // OrElse returns the value if is not nil, otherwise returns other.
func (o Optional[T]) OrElse(other T) T { func (o Optional[T]) OrElse(other T) T {
o.mu.RLock() o.mu.RLock()
defer o.mu.RUnlock() defer o.mu.RUnlock()
@@ -85,24 +85,24 @@ func (o Optional[T]) OrElse(other T) T {
return other return other
} }
// OrElseGet returns the value if present, otherwise invokes supplier and returns the result. // OrElseGet returns the value if is not nil, otherwise invokes action and returns the result.
func (o Optional[T]) OrElseGet(supplier func() T) T { func (o Optional[T]) OrElseGet(action func() T) T {
o.mu.RLock() o.mu.RLock()
defer o.mu.RUnlock() defer o.mu.RUnlock()
if o.value != nil { if o.value != nil {
return *o.value return *o.value
} }
return supplier() return action()
} }
// OrElseThrow returns the value if present, otherwise returns an error. // OrElseTrigger returns the value if present, otherwise returns an error.
func (o Optional[T]) OrElseThrow(errorSupplier func() error) (T, error) { func (o Optional[T]) OrElseTrigger(errorHandler func() error) (T, error) {
o.mu.RLock() o.mu.RLock()
defer o.mu.RUnlock() defer o.mu.RUnlock()
if o.value == nil { if o.value == nil {
return *new(T), errorSupplier() return *new(T), errorHandler()
} }
return *o.value, nil return *o.value, nil
} }

View File

@@ -7,11 +7,11 @@ import (
"github.com/duke-git/lancet/v2/internal" "github.com/duke-git/lancet/v2/internal"
) )
func TestEmpty(t *testing.T) { func TestDefault(t *testing.T) {
assert := internal.NewAssert(t, "TestEmpty") assert := internal.NewAssert(t, "TestEmpty")
opt := Empty[int]() opt := Default[int]()
assert.ShouldBeTrue(opt.IsEmpty()) assert.ShouldBeTrue(opt.IsNil())
} }
func TestOf(t *testing.T) { func TestOf(t *testing.T) {
@@ -19,30 +19,30 @@ func TestOf(t *testing.T) {
value := 42 value := 42
opt := Of(value) opt := Of(value)
assert.ShouldBeTrue(opt.IsPresent()) assert.ShouldBeTrue(opt.IsNotNil())
assert.Equal(opt.Get(), value) assert.Equal(opt.Unwarp(), value)
} }
func TestOfNullable(t *testing.T) { func TestFromNillable(t *testing.T) {
assert := internal.NewAssert(t, "TestOfNullable") assert := internal.NewAssert(t, "TestOfNullable")
var value *int = nil var value *int = nil
opt := OfNullable(value) opt := FromNillable(value)
assert.ShouldBeFalse(opt.IsPresent()) assert.ShouldBeFalse(opt.IsNotNil())
value = new(int) value = new(int)
*value = 42 *value = 42
opt = OfNullable(value) opt = FromNillable(value)
assert.ShouldBeTrue(opt.IsPresent()) assert.ShouldBeTrue(opt.IsNotNil())
} }
func TestOrElse(t *testing.T) { func TestOrElse(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElse") assert := internal.NewAssert(t, "TestOrElse")
optEmpty := Empty[int]() optDefault := Default[int]()
defaultValue := 100 defaultValue := 100
val := optEmpty.OrElse(defaultValue) val := optDefault.OrElse(defaultValue)
assert.Equal(val, defaultValue) assert.Equal(val, defaultValue)
optWithValue := Of(42) optWithValue := Of(42)
@@ -53,72 +53,72 @@ func TestOrElse(t *testing.T) {
func TestOrElseGetHappyPath(t *testing.T) { func TestOrElseGetHappyPath(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElseGetHappyPath") assert := internal.NewAssert(t, "TestOrElseGetHappyPath")
optWithValue := Of(42) optWithValue := Of(42)
supplier := func() int { return 100 } action := func() int { return 100 }
val := optWithValue.OrElseGet(supplier) val := optWithValue.OrElseGet(action)
assert.Equal(val, 42) assert.Equal(val, 42)
} }
func TestOrElseGet(t *testing.T) { func TestOrElseGet(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElseGet") assert := internal.NewAssert(t, "TestOrElseGet")
optEmpty := Empty[int]() optDefault := Default[int]()
supplier := func() int { return 100 } action := func() int { return 100 }
val := optEmpty.OrElseGet(supplier) val := optDefault.OrElseGet(action)
assert.Equal(val, supplier()) assert.Equal(val, action())
} }
func TestOrElseThrow(t *testing.T) { func TestOrElseTrigger(t *testing.T) {
assert := internal.NewAssert(t, "TestOrElseThrow") assert := internal.NewAssert(t, "OrElseTrigger")
optEmpty := Empty[int]() optDefault := Default[int]()
_, err := optEmpty.OrElseThrow(func() error { return errors.New("no value") }) _, err := optDefault.OrElseTrigger(func() error { return errors.New("no value") })
assert.Equal(err.Error(), "no value") assert.Equal(err.Error(), "no value")
optWithValue := Of(42) optWithValue := Of(42)
val, err := optWithValue.OrElseThrow(func() error { return errors.New("no value") }) val, err := optWithValue.OrElseTrigger(func() error { return errors.New("no value") })
assert.IsNil(err) assert.IsNil(err)
assert.Equal(val, 42) assert.Equal(val, 42)
} }
func TestIfPresent(t *testing.T) { func TestIfNotNil(t *testing.T) {
assert := internal.NewAssert(t, "TestIfPresent") assert := internal.NewAssert(t, "IfNotNil")
called := false called := false
action := func(value int) { called = true } action := func(value int) { called = true }
optEmpty := Empty[int]() optDefault := Default[int]()
optEmpty.IfPresent(action) optDefault.IfNotNil(action)
assert.ShouldBeFalse(called) assert.ShouldBeFalse(called)
called = false // Reset for next test called = false // Reset for next test
optWithValue := Of(42) optWithValue := Of(42)
optWithValue.IfPresent(action) optWithValue.IfNotNil(action)
assert.ShouldBeTrue(called) assert.ShouldBeTrue(called)
} }
func TestIfPresentOrElse(t *testing.T) { func TestIfNotNilOrElse(t *testing.T) {
assert := internal.NewAssert(t, "TestIfPresentOrElse") assert := internal.NewAssert(t, "TestIfNotNilOrElse")
// Test when value is present // Test when value is present
calledWithValue := false calledWithValue := false
valueAction := func(value int) { calledWithValue = true } valueAction := func(value int) { calledWithValue = true }
emptyAction := func() { t.Errorf("Empty action should not be called when value is present") } fallbackAction := func() { t.Errorf("Empty action should not be called when value is present") }
optWithValue := Of(42) optWithValue := Of(42)
optWithValue.IfPresentOrElse(valueAction, emptyAction) optWithValue.IfNotNilOrElse(valueAction, fallbackAction)
assert.ShouldBeTrue(calledWithValue) assert.ShouldBeTrue(calledWithValue)
// Test when value is not present // Test when value is not present
calledWithEmpty := false calledWithEmpty := false
valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") } valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") }
emptyAction = func() { calledWithEmpty = true } fallbackAction = func() { calledWithEmpty = true }
optEmpty := Empty[int]() optDefault := Default[int]()
optEmpty.IfPresentOrElse(valueAction, emptyAction) optDefault.IfNotNilOrElse(valueAction, fallbackAction)
assert.ShouldBeTrue(calledWithEmpty) assert.ShouldBeTrue(calledWithEmpty)
} }
@@ -133,19 +133,19 @@ func TestGetWithPanicStandard(t *testing.T) {
r := recover() r := recover()
assert.IsNil(r) assert.IsNil(r)
}() }()
val := optWithValue.Get() val := optWithValue.Unwarp()
if val != 42 { if val != 42 {
t.Errorf("Expected Get to return 42, got %v", val) t.Errorf("Expected Unwarp to return 42, got %v", val)
} }
}() }()
// Test when value is not present // Test when value is not present
optEmpty := Empty[int]() optDefault := Default[int]()
func() { func() {
defer func() { defer func() {
r := recover() r := recover()
assert.IsNotNil(r) assert.IsNotNil(r)
}() }()
_ = optEmpty.Get() _ = optDefault.Unwarp()
}() }()
} }

View File

@@ -22,17 +22,16 @@ import (
## 目录 ## 目录
- [Of](#Of) - [Of](#Of)
- [OfNullable](#OfNullable) - [FromNillable](#FromNillable)
- [Empty](#Empty) - [Default](#Default)
- [IsPresent](#IsPresent) - [IsNotNil](#IsNotNil)
- [IsEmpty](#IsEmpty) - [IsNil](#IsNil)
- [IfPresent](#IfPresent) - [IsNotNil](#IsNotNil)
- [IfPresentOrElse](#IfPresentOrElse) - [IfNotNilOrElse](#IfPresentOrElse)
- [Get](#Get) - [Umwarp](#Umwarp)
- [OrElse](#OrElse) - [OrElse](#OrElse)
- [OrElseGet](#OrElseGet) - [OrElseGet](#OrElseGet)
- [OrElseThrow](#OrElseThrow) - [OrElseTrigger](#OrElseTrigger)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -68,14 +67,13 @@ func main() {
} }
``` ```
### <span id="OfNullable">OfNullable</span> ### <span id="FromNillable">FromNillable</span>
<p>返回一个包含给定值的Optional该值可能为空 (nil)。</p> <p>返回一个包含给定值的Optional该值可能为空 (nil)。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func OfNullable[T any](value *T) Optional[T] func FromNillable[T any](value *T) Optional[T]
``` ```
<b>示例:</b> <b>示例:</b>
@@ -89,15 +87,15 @@ import (
func main() { func main() {
var value *int = nil var value *int = nil
opt := optional.OfNullable(value) opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
value = new(int) value = new(int)
*value = 42 *value = 42
opt = optional.OfNullable(value) opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
// Output: // Output:
@@ -107,14 +105,13 @@ func main() {
``` ```
### <span id="Empty">Empty</span> ### <span id="Default">Default</span>
<p>返回一个空Optional实例。</p> <p>返回一个空Optional实例。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func Empty[T any]() Optional[T] func Default[T any]() Optional[T]
``` ```
<b>示例:</b> <b>示例:</b>
@@ -127,8 +124,8 @@ import (
) )
func main() { func main() {
optEmpty := OfNullable.Empty[int]() optDefault := optional.Default[int]()
fmt.Println(optEmpty.IsEmpty()) fmt.Println(optDefault.IsNil())
// Output: // Output:
// true // true
@@ -136,14 +133,13 @@ func main() {
``` ```
### <span id="IsEmpty">IsEmpty</span> ### <span id="IsNil">IsNil</span>
<p>验证Optional是否为空。</p> <p>验证Optional是否为空。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (o Optional[T]) IsEmpty() bool func (o Optional[T]) IsNil() bool
``` ```
<b>示例:</b> <b>示例:</b>
@@ -156,23 +152,21 @@ import (
) )
func main() { func main() {
optEmpty := OfNullable.Empty[int]() optDefault := optional.Default[int]()
fmt.Println(optEmpty.IsEmpty()) fmt.Println(optDefault.IsNil())
// Output: // Output:
// true // true
} }
``` ```
### <span id="IsNotNil">IsNotNil</span>
### <span id="IsPresent">IsPresent</span>
<p>检查当前Optional内是否存在值。</p> <p>检查当前Optional内是否存在值。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (o Optional[T]) IsPresent() bool func (o Optional[T]) IsNotNil() bool
``` ```
<b>示例:</b> <b>示例:</b>
@@ -181,20 +175,20 @@ package main
import ( import (
"fmt" "fmt"
optional "github.com/duke-git/lancet/v2/datastructure/optional" "github.com/duke-git/lancet/v2/datastructure/optional"
) )
func main() { func main() {
var value *int = nil var value *int = nil
opt := optional.OfNullable(value) opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
value = new(int) value = new(int)
*value = 42 *value = 42
opt = optional.OfNullable(value) opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
// Output: // Output:
@@ -203,15 +197,13 @@ func main() {
} }
``` ```
### <span id="IfNotNil">IfNotNil</span>
### <span id="IfPresent">IfPresent</span>
<p>如果值存在则使用action方法执行给定的操作。</p> <p>如果值存在则使用action方法执行给定的操作。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (o Optional[T]) IfPresent(action func(value T)) func (o Optional[T]) IfNotNil(action func(value T))
``` ```
<b>示例:</b> <b>示例:</b>
@@ -220,23 +212,23 @@ package main
import ( import (
"fmt" "fmt"
optional "github.com/duke-git/lancet/v2/datastructure/optional" "github.com/duke-git/lancet/v2/datastructure/optional"
) )
func main() { func main() {
called := false called := false
action := func(value int) { called = true } action := func(value int) { called = true }
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
optEmpty.IfPresent(action) optDefault.IfNotNil(action)
fmt.Println(called) fmt.Println(called)
called = false // Reset for next test called = false // Reset for next test
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
optWithValue.IfPresent(action) optWithValue.IfNotNil(action)
fmt.Println(optWithValue.IsPresent()) fmt.Println(optWithValue.IsNotNil())
// Output: // Output:
// false // false
@@ -245,14 +237,13 @@ func main() {
``` ```
### <span id="IfPresentOrElse">IfPresentOrElse</span> ### <span id="IfNotNilOrElse">IfNotNilOrElse</span>
<p>根据是否存在值执行相应的操作:有值则执行指定操作,没有值则执行默认操作。</p> <p>根据是否存在值执行相应的操作:有值则执行指定操作,没有值则执行默认操作。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (o Optional[T]) IfPresentOrElse(action func(value T), emptyAction func()) func (o Optional[T]) IfNotNilOrElse(action func(value T), fallbackAction func())
``` ```
<b>示例:</b> <b>示例:</b>
@@ -270,7 +261,7 @@ func main() {
emptyAction := func() { t.Errorf("Empty action should not be called when value is present") } emptyAction := func() { t.Errorf("Empty action should not be called when value is present") }
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
optWithValue.IfPresentOrElse(valueAction, emptyAction) optWithValue.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithValue) fmt.Println(calledWithValue)
@@ -278,8 +269,8 @@ func main() {
valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") } valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") }
emptyAction = func() { calledWithEmpty = true } emptyAction = func() { calledWithEmpty = true }
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
optEmpty.IfPresentOrElse(valueAction, emptyAction) optDefault.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithEmpty) fmt.Println(calledWithEmpty)
@@ -289,13 +280,13 @@ func main() {
} }
``` ```
### <span id="Get">Get</span> ### <span id="Unwrap">Unwrap</span>
<p>如果存在返回该值否则引发panic。</p> <p>如果存在返回该值否则引发panic。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (o Optional[T]) Get() T func (o Optional[T]) Unwrap() T
``` ```
<b>示例:</b> <b>示例:</b>
@@ -311,7 +302,7 @@ func main() {
value := 42 value := 42
opt := optional.Of(value) opt := optional.Of(value)
fmt.Println(opt.Get()) fmt.Println(opt.Unwrap())
// Output: // Output:
// 42 // 42
@@ -338,8 +329,8 @@ import (
) )
func main() { func main() {
optEmpty := optional.Empty[int]() optDefault := optional.Empty[int]()
val := optEmpty.OrElse(100) val := optDefault.OrElse(100)
fmt.Println(val) fmt.Println(val)
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
@@ -359,7 +350,7 @@ func main() {
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (o Optional[T]) OrElseGet(supplier func() T) T func (o Optional[T]) OrElseGet(action func() T) T
``` ```
<b>示例:</b> <b>示例:</b>
@@ -372,10 +363,10 @@ import (
) )
func main() { func main() {
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
supplier := func() int { return 100 } action := func() int { return 100 }
val := optEmpty.OrElseGet(supplier) val := optDefault.OrElseGet(action)
fmt.Println(val) fmt.Println(val)
// Output: // Output:
@@ -383,14 +374,13 @@ func main() {
} }
``` ```
### <span id="OrElseTrigger">OrElseTrigger</span>
### <span id="OrElseThrow">OrElseThrow</span>
<p>检查Optional值是否存在如果存在则直接返回该值否则返回错误。</p> <p>检查Optional值是否存在如果存在则直接返回该值否则返回错误。</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (o Optional[T]) OrElseThrow(errorSupplier func() error) (T, error) OrElseTrigger(errorHandler func() error) (T, error)
``` ```
<b>示例:</b> <b>示例:</b>
@@ -403,13 +393,13 @@ import (
) )
func main() { func main() {
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
_, err := optEmpty.OrElseThrow(func() error { return errors.New("no value") }) _, err := optDefault.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(err.Error()) fmt.Println(err.Error())
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
val, err := optWithValue.OrElseThrow(func() error { return errors.New("no value") }) val, err := optWithValue.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(val) fmt.Println(val)
fmt.Println(err) fmt.Println(err)

View File

@@ -22,16 +22,16 @@ import (
## Index ## Index
- [Of](#Of) - [Of](#Of)
- [OfNullable](#OfNullable) - [FromNillable](#FromNillable)
- [Empty](#Empty) - [Default](#Default)
- [IsPresent](#IsPresent) - [IsNotNil](#IsNotNil)
- [IsEmpty](#IsEmpty) - [IsNil](#IsNil)
- [IfPresent](#IfPresent) - [IsNotNil](#IsNotNil)
- [IfPresentOrElse](#IfPresentOrElse) - [IfNotNilOrElse](#IfPresentOrElse)
- [Get](#Get) - [Umwarp](#Umwarp)
- [OrElse](#OrElse) - [OrElse](#OrElse)
- [OrElseGet](#OrElseGet) - [OrElseGet](#OrElseGet)
- [OrElseThrow](#OrElseThrow) - [OrElseTrigger](#OrElseTrigger)
@@ -68,13 +68,13 @@ func main() {
} }
``` ```
### <span id="OfNullable">OfNullable</span> ### <span id="FromNillable">FromNillable</span>
<p>Returns an Optional for a given value, which may be nil.</p> <p>Returns an Optional for a given value, which may be nil.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func OfNullable[T any](value *T) Optional[T] func FromNillable[T any](value *T) Optional[T]
``` ```
<b>Example:</b> <b>Example:</b>
@@ -88,15 +88,15 @@ import (
func main() { func main() {
var value *int = nil var value *int = nil
opt := optional.OfNullable(value) opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
value = new(int) value = new(int)
*value = 42 *value = 42
opt = optional.OfNullable(value) opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
// Output: // Output:
@@ -106,13 +106,13 @@ func main() {
``` ```
### <span id="Empty">Empty</span> ### <span id="Default">Default</span>
<p>Returns an empty Optional instance.</p> <p>Returns an default Optional instance.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func Empty[T any]() Optional[T] func Default[T any]() Optional[T]
``` ```
<b>Example:</b> <b>Example:</b>
@@ -125,8 +125,8 @@ import (
) )
func main() { func main() {
optEmpty := OfNullable.Empty[int]() optDefault := optional.Default[int]()
fmt.Println(optEmpty.IsEmpty()) fmt.Println(optDefault.IsNil())
// Output: // Output:
// true // true
@@ -134,13 +134,13 @@ func main() {
``` ```
### <span id="IsEmpty">IsEmpty</span> ### <span id="IsNil">IsNil</span>
<p>Checks if the Optional is empty.</p> <p>Checks if the Optional is nil.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (o Optional[T]) IsEmpty() bool func (o Optional[T]) IsNil() bool
``` ```
<b>Example:</b> <b>Example:</b>
@@ -153,8 +153,8 @@ import (
) )
func main() { func main() {
optEmpty := OfNullable.Empty[int]() optDefault := optional.Default[int]()
fmt.Println(optEmpty.IsEmpty()) fmt.Println(optDefault.IsNil())
// Output: // Output:
// true // true
@@ -162,13 +162,13 @@ func main() {
``` ```
### <span id="IsPresent">IsPresent</span> ### <span id="IsNotNil">IsNotNil</span>
<p>Checks if there is a value present.</p> <p>Checks if there is a value not nil.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (o Optional[T]) IsPresent() bool func (o Optional[T]) IsNotNil() bool
``` ```
<b>Example:</b> <b>Example:</b>
@@ -182,15 +182,15 @@ import (
func main() { func main() {
var value *int = nil var value *int = nil
opt := optional.OfNullable(value) opt := optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
value = new(int) value = new(int)
*value = 42 *value = 42
opt = optional.OfNullable(value) opt = optional.FromNillable(value)
fmt.Println(opt.IsPresent()) fmt.Println(opt.IsNotNil())
// Output: // Output:
@@ -200,13 +200,13 @@ func main() {
``` ```
### <span id="IfPresent">IfPresent</span> ### <span id="IfNotNil">IfNotNil</span>
<p>Performs the given action with the value if a value is present.</p> <p>Performs the given action with the value if a value is present.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (o Optional[T]) IfPresent(action func(value T)) func (o Optional[T]) IfNotNil(action func(value T))
``` ```
<b>Example:</b> <b>Example:</b>
@@ -222,16 +222,16 @@ func main() {
called := false called := false
action := func(value int) { called = true } action := func(value int) { called = true }
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
optEmpty.IfPresent(action) optDefault.IfNotNil(action)
fmt.Println(called) fmt.Println(called)
called = false // Reset for next test called = false // Reset for next test
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
optWithValue.IfPresent(action) optWithValue.IfNotNil(action)
fmt.Println(optWithValue.IsPresent()) fmt.Println(optWithValue.IsNotNil())
// Output: // Output:
// false // false
@@ -240,13 +240,13 @@ func main() {
``` ```
### <span id="IfPresentOrElse">IfPresentOrElse</span> ### <span id="IfNotNilOrElse">IfNotNilOrElse</span>
<p>Performs the action with the value if present, otherwise performs the empty-based action.</p> <p>Performs the action with the value if not nil, otherwise performs the fallback action.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (o Optional[T]) IfPresentOrElse(action func(value T), emptyAction func()) func (o Optional[T]) IfNotNilOrElse(action func(value T), fallbackAction func())
``` ```
<b>Example:</b> <b>Example:</b>
@@ -264,7 +264,7 @@ func main() {
emptyAction := func() { t.Errorf("Empty action should not be called when value is present") } emptyAction := func() { t.Errorf("Empty action should not be called when value is present") }
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
optWithValue.IfPresentOrElse(valueAction, emptyAction) optWithValue.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithValue) fmt.Println(calledWithValue)
@@ -272,8 +272,8 @@ func main() {
valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") } valueAction = func(value int) { t.Errorf("Value action should not be called when value is not present") }
emptyAction = func() { calledWithEmpty = true } emptyAction = func() { calledWithEmpty = true }
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
optEmpty.IfPresentOrElse(valueAction, emptyAction) optDefault.IfNotNilOrElse(valueAction, emptyAction)
fmt.Println(calledWithEmpty) fmt.Println(calledWithEmpty)
@@ -283,13 +283,13 @@ func main() {
} }
``` ```
### <span id="Get">Get</span> ### <span id="Unwrap">Unwrap</span>
<p>Returns the value if present, otherwise panics.</p> <p>Returns the value if not nil, otherwise panics.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (o Optional[T]) Get() T func (o Optional[T]) Unwrap() T
``` ```
<b>Example:</b> <b>Example:</b>
@@ -305,7 +305,7 @@ func main() {
value := 42 value := 42
opt := optional.Of(value) opt := optional.Of(value)
fmt.Println(opt.Get()) fmt.Println(opt.Unwrap())
// Output: // Output:
// 42 // 42
@@ -314,7 +314,7 @@ func main() {
### <span id="OrElse">OrElse</span> ### <span id="OrElse">OrElse</span>
<p>Returns the value if present, otherwise returns other.</p> <p>Returns the value if not nill, otherwise returns other.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -332,8 +332,8 @@ import (
) )
func main() { func main() {
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
val := optEmpty.OrElse(100) val := optDefault.OrElse(100)
fmt.Println(val) fmt.Println(val)
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
@@ -348,12 +348,12 @@ func main() {
### <span id="OrElseGet">OrElseGet</span> ### <span id="OrElseGet">OrElseGet</span>
<p>Returns the value if present, otherwise invokes supplier and returns the result.</p> <p>Returns the value if not nil, otherwise invokes action and returns the result.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (o Optional[T]) OrElseGet(supplier func() T) T func (o Optional[T]) OrElseGet(action func() T) T
``` ```
<b>Example:</b> <b>Example:</b>
@@ -366,10 +366,10 @@ import (
) )
func main() { func main() {
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
supplier := func() int { return 100 } action := func() int { return 100 }
val := optEmpty.OrElseGet(supplier) val := optDefault.OrElseGet(action)
fmt.Println(val) fmt.Println(val)
// Output: // Output:
@@ -378,13 +378,13 @@ func main() {
``` ```
### <span id="OrElseThrow">OrElseThrow</span> ### <span id="OrElseTrigger">OrElseTrigger</span>
<p>Returns the value if present, otherwise returns an error.</p> <p>Returns the value if present, otherwise returns an error.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (o Optional[T]) OrElseThrow(errorSupplier func() error) (T, error) OrElseTrigger(errorHandler func() error) (T, error)
``` ```
<b>Example:</b> <b>Example:</b>
@@ -397,13 +397,13 @@ import (
) )
func main() { func main() {
optEmpty := optional.Empty[int]() optDefault := optional.Default[int]()
_, err := optEmpty.OrElseThrow(func() error { return errors.New("no value") }) _, err := optDefault.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(err.Error()) fmt.Println(err.Error())
optWithValue := optional.Of(42) optWithValue := optional.Of(42)
val, err := optWithValue.OrElseThrow(func() error { return errors.New("no value") }) val, err := optWithValue.OrElseTrigger(func() error { return errors.New("no value") })
fmt.Println(val) fmt.Println(val)
fmt.Println(err) fmt.Println(err)