Skip to content

Commit

Permalink
Chaining (#8)
Browse files Browse the repository at this point in the history
Chaining
  • Loading branch information
marrow16 authored Nov 15, 2022
1 parent 02bd5ba commit 69a0aab
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
55 changes: 42 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ package main
import (
"encoding/json"
"fmt"

. "github.com/go-andiamo/gopt"
)

Expand All @@ -68,11 +69,11 @@ func main() {
err := json.Unmarshal([]byte(jdata), normal)
if err == nil {
// was property Foo set???
fmt.Printf("'Foo` was set to \"%s\"???\n", normal.Foo) // was it really?
fmt.Printf("'Foo' was set to \"%s\"???\n", normal.Foo) // was it really?
// was property Bar actually set to 0???
fmt.Printf("'Bar` was set to \"%d\"???\n", normal.Bar) // was it really?
// was property Baz actually set to 0???
fmt.Printf("'Baz` was set to \"%f\"???\n", normal.Baz) // was it really?
fmt.Printf("'Bar' was set to \"%d\"???\n", normal.Bar) // was it really?
// was property Baz actually set to 0.000???
fmt.Printf("'Baz' was set to \"%f\"???\n", normal.Baz) // was it really?
} else {
println(err.Error())
}
Expand All @@ -82,17 +83,45 @@ func main() {
opts := &OptsStruct{}
err = json.Unmarshal([]byte(jdata), opts)
if err == nil {
fmt.Printf("'Foo` was set - %t\n", opts.Foo.WasSet())
fmt.Printf("'Foo` had valid value - %t\n", opts.Foo.IsPresent())
fmt.Printf("'Bar` was set - %t\n", opts.Bar.WasSet())
fmt.Printf("'Bar` had valid value - %t\n", opts.Bar.IsPresent())
fmt.Printf("'Baz` was set - %t\n", opts.Baz.WasSet())
fmt.Printf("'Baz` had valid value - %t\n", opts.Baz.IsPresent())
opts.Foo.IfSetOtherwise(
func(v string) {
fmt.Printf("'Foo' was set to \"%s\"\n", v)
},
func() {
println("'Foo' was set but not to a valid value")
},
func() {
println("'Foo' was not set at all")
},
)
opts.Bar.IfSetOtherwise(
func(v int) {
fmt.Printf("'Bar' was set to %d\n", v)
},
func() {
println("'Bar' was set but not to a valid value")
},
func() {
println("'Bar' was not set at all")
},
)
opts.Baz.IfSetOtherwise(
func(v float64) {
fmt.Printf("'Baz' was set to %f\n", v)
},
func() {
println("'Baz' was set but not to a valid value")
},
func() {
println("'Baz' was not set at all")
},
)
} else {
println(err.Error())
}
}
```
[try on go-playground](https://go.dev/play/p/63eC1AJ3Qgn)

## Methods
<table>
Expand Down Expand Up @@ -199,7 +228,7 @@ func main() {
<code>OrElseSet(v T)</code><br>
if the value is not present it is set to the supplied value
</td>
<td><code>Optional[T]</code></td>
<td><code>*Optional[T]</code></td>
</tr>
<tr>
<td>
Expand Down Expand Up @@ -264,14 +293,14 @@ func main() {
clears the optional<br>
Clearing sets the present to false, the set flag to false and the value to an empty value
</td>
<td><code>Optional[T]</code></td>
<td><code>*Optional[T]</code></td>
</tr>
<tr>
<td>
<code>UnSet()</code><br>
clears the set flag (see <code>WasSet()</code>)
</td>
<td><code>Optional[T]</code></td>
<td><code>*Optional[T]</code></td>
</tr>
</table>

Expand Down
12 changes: 6 additions & 6 deletions optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ func (o Optional[T]) WasSet() bool {
}

// UnSet clears the set flag (see WasSet)
func (o *Optional[T]) UnSet() Optional[T] {
func (o *Optional[T]) UnSet() *Optional[T] {
o.set = false
return *o
return o
}

// Clear clears the optional
//
// Clearing sets the present to false, the set flag to false and the value to an empty value
func (o *Optional[T]) Clear() Optional[T] {
func (o *Optional[T]) Clear() *Optional[T] {
o.clear(false)
return *o
return o
}

// IfPresent if the value is present, calls the supplied function with the value, otherwise does nothing
Expand Down Expand Up @@ -215,7 +215,7 @@ func (o Optional[T]) OrElseGet(f func() T) T {
}

// OrElseSet if the value is not present it is set to the supplied value
func (o *Optional[T]) OrElseSet(v T) Optional[T] {
func (o *Optional[T]) OrElseSet(v T) *Optional[T] {
if !o.present {
if isPresent(v) {
o.present = true
Expand All @@ -226,7 +226,7 @@ func (o *Optional[T]) OrElseSet(v T) Optional[T] {
}
o.set = true
}
return *o
return o
}

// OrElseError returns the supplied error if the value is not present, otherwise returns nil
Expand Down
34 changes: 32 additions & 2 deletions optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,14 @@ func TestOptional_OrElseSet(t *testing.T) {
require.False(t, o.IsPresent())

o2 := o.OrElseSet(map[string]interface{}{})
require.Equal(t, o, o2)
require.Equal(t, &o, o2)
require.True(t, o2.IsPresent())
require.True(t, o.IsPresent())

o = Empty[map[string]interface{}]()
require.False(t, o.IsPresent())
o2 = o.OrElseSet(nil)
require.Equal(t, o, o2)
require.Equal(t, &o, o2)
require.False(t, o2.IsPresent())
require.False(t, o.IsPresent())
}
Expand Down Expand Up @@ -534,6 +534,36 @@ func (s *scannable) Scan(src any) error {
return s.err
}

func TestChainCalls(t *testing.T) {
type myStruct struct {
Foo Optional[string]
}
my := &myStruct{}
require.False(t, my.Foo.IsPresent())
require.False(t, my.Foo.WasSet())

was := my.Foo.Clear().UnSet().OrElseSet("abc").WasSet()
require.True(t, was)
require.True(t, my.Foo.WasSet())
require.True(t, my.Foo.IsPresent())
v, ok := my.Foo.GetOk()
require.True(t, ok)
require.Equal(t, "abc", v)

o := EmptyString()
was = o.Clear().WasSet()
require.False(t, was)
require.False(t, o.WasSet())
require.False(t, o.IsPresent())
was = o.Clear().UnSet().OrElseSet("abc").WasSet()
require.True(t, was)
require.True(t, o.WasSet())
require.True(t, o.IsPresent())
v, ok = o.GetOk()
require.True(t, ok)
require.Equal(t, "abc", v)
}

func TestEmpties(t *testing.T) {
require.False(t, EmptyString().IsPresent())
require.False(t, EmptyInterface().IsPresent())
Expand Down

0 comments on commit 69a0aab

Please # to comment.