Skip to content

Commit

Permalink
Rename ValueOrDefault() to GetOr() for consistency with Get() (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfo authored Jan 11, 2022
1 parent 0b37e99 commit 4b36b3d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ fmt.Println(o.MustGet()) // panics
```go
o := optional.Of("foo")

fmt.Println(o.ValueOrDefault("fred")) // prints "foo"
fmt.Println(o.GetOr("fred")) // prints "foo"

o.SetNil()
fmt.Println(o.ValueOrDefault("fred")) // prints "fred"
fmt.Println(o.GetOr("fred")) // prints "fred"
```

## Checking for value
Expand All @@ -133,7 +133,7 @@ j := `{ "name": null, "age": 42 }`
p := new(Person)
json.Unmarshal([]byte(j), p)

fmt.Println("name is %s", p.Name.ValueOrDefault("Unknown"))
fmt.Println("name is %s", p.Name.GetOr("Unknown"))
// prints "name is Unknown" since `name` was null
```

Expand Down
4 changes: 2 additions & 2 deletions optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (o *Value[T]) Get() (ok bool, value T) {
return false, *zeroValue
}

// ValueOrDefault returns the underlying non-nil value or the provided fallback value
func (o *Value[T]) ValueOrDefault(value T) T {
// GetOr returns the underlying non-nil value or the provided fallback value
func (o *Value[T]) GetOr(value T) T {
if o.value == nil {
return value
}
Expand Down
4 changes: 2 additions & 2 deletions optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func TestGetters(t *testing.T) {

func TestGetterOfDefaultValue(t *testing.T) {
o := OfNil[string]()
assert.Equal(t, "bar", o.ValueOrDefault("bar"))
assert.Equal(t, "bar", o.GetOr("bar"))

o.Set("foo")
assert.Equal(t, "foo", o.ValueOrDefault("bar"))
assert.Equal(t, "foo", o.GetOr("bar"))
}

func TestTypeParameters(t *testing.T) {
Expand Down

0 comments on commit 4b36b3d

Please sign in to comment.