Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IfSet and IfSetOtherwise #6

Merged
merged 1 commit into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ func main() {
</td>
<td><code><code>Optional[T]</code></code></td>
</tr>
<tr>
<td>
<code>IfSet(f func(v T), notPresent func())</code><br>
if the value was set and is present, calls the supplied function with the value<br>
if the value was set but is not present, calls the supplied notPresent function<br>
otherwise, does nothing
</td>
<td><code>Optional[T]</code></td>
</tr>
<tr>
<td>
<code>IfSetOtherwise(f func(v T), notPresent func(), other func())</code><br>
if the value was set and is present, calls the supplied function with the value<br>
if the value was set but is not present, calls the supplied notPresent function<br>
otherwise, calls the other func
</td>
<td><code><code>Optional[T]</code></code></td>
</tr>
<tr>
<td>
<code>OrElse(other T)</code><br>
Expand Down
36 changes: 33 additions & 3 deletions optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,47 @@ func (o *Optional[T]) Clear() Optional[T] {

// IfPresent if the value is present, calls the supplied function with the value, otherwise does nothing
func (o Optional[T]) IfPresent(f func(v T)) Optional[T] {
if o.present {
if o.present && f != nil {
f(o.value)
}
return o
}

// IfPresentOtherwise if the value is present, calls the supplied function with the value, otherwise calls the other function
func (o Optional[T]) IfPresentOtherwise(f func(v T), other func()) Optional[T] {
if o.present {
if o.present && f != nil {
f(o.value)
} else {
} else if !o.present && other != nil {
other()
}
return o
}

// IfSet if the value was set and is present, calls the supplied function with the value
//
// if the value was set but is not present, calls the supplied notPresent function
//
// otherwise, does nothing
func (o Optional[T]) IfSet(f func(v T), notPresent func()) Optional[T] {
if o.set && o.present && f != nil {
f(o.value)
} else if o.set && !o.present && notPresent != nil {
notPresent()
}
return o
}

// IfSetOtherwise if the value was set and is present, calls the supplied function with the value
//
// if the value was set but is not present, calls the supplied notPresent function
//
// otherwise, calls the other func
func (o Optional[T]) IfSetOtherwise(f func(v T), notPresent func(), other func()) Optional[T] {
if o.set && o.present && f != nil {
f(o.value)
} else if o.set && !o.present && notPresent != nil {
notPresent()
} else if !o.set && !o.present && other != nil {
other()
}
return o
Expand Down
77 changes: 77 additions & 0 deletions optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,82 @@ func TestOptional_IfPresentOtherwise(t *testing.T) {
require.True(t, calledOther)
}

func TestOptional_IfSet(t *testing.T) {
o := EmptyString()
o.OrElseSet("aaa")
setCalled := false
notPresentCalled := false
value := ""
setFn := func(v string) {
setCalled = true
value = v
}
notPresentFn := func() {
notPresentCalled = true
}
o.IfSet(setFn, notPresentFn)
require.True(t, setCalled)
require.Equal(t, "aaa", value)
require.False(t, notPresentCalled)

setCalled = false
notPresentCalled = false
o.Clear()
err := o.UnmarshalJSON([]byte(`null`))
require.NoError(t, err)
o.IfSet(setFn, notPresentFn)
require.False(t, setCalled)
require.True(t, notPresentCalled)
}

func TestOptional_IfSetOtherwise(t *testing.T) {
type oStruct struct {
Foo Optional[string]
}
setCalled := false
notPresentCalled := false
otherCalled := false
setFn := func(v string) {
setCalled = true
}
notPresentFn := func() {
notPresentCalled = true
}
otherFn := func() {
otherCalled = true
}

strc := &oStruct{}
err := json.Unmarshal([]byte(`{}`), strc)
require.NoError(t, err)
strc.Foo.IfSetOtherwise(setFn, notPresentFn, otherFn)
require.False(t, setCalled)
require.False(t, notPresentCalled)
require.True(t, otherCalled)

setCalled = false
notPresentCalled = false
otherCalled = false
strc = &oStruct{}
err = json.Unmarshal([]byte(`{"Foo":null}`), strc)
require.NoError(t, err)
strc.Foo.IfSetOtherwise(setFn, notPresentFn, otherFn)
require.False(t, setCalled)
require.True(t, notPresentCalled)
require.False(t, otherCalled)

setCalled = false
notPresentCalled = false
otherCalled = false
strc = &oStruct{}
err = json.Unmarshal([]byte(`{"Foo":"abc"}`), strc)
require.NoError(t, err)
strc.Foo.IfSetOtherwise(setFn, notPresentFn, otherFn)
require.True(t, setCalled)
require.False(t, notPresentCalled)
require.False(t, otherCalled)
}

func TestOptional_OrElse(t *testing.T) {
o := Empty[string]()
v := o.OrElse("bbb")
Expand Down Expand Up @@ -391,6 +467,7 @@ func TestOptional_Scan(t *testing.T) {
require.False(t, o5.IsPresent())
require.False(t, o5.WasSet())
err = o5.Scan(nil)
require.NoError(t, err)
require.False(t, o5.IsPresent())
require.True(t, o5.WasSet())
}
Expand Down