Skip to content

Commit

Permalink
refactor: Make Option immutable (#769)
Browse files Browse the repository at this point in the history
* Make HasValue immutable

* Make Value immutable
  • Loading branch information
AndrewSisley authored Aug 30, 2022
1 parent 7d072bf commit 58fabba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
20 changes: 16 additions & 4 deletions client/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,33 @@ package client
type Option[T any] struct {
// If HasValue is true, this Option contains a value, if
// it is false it contains no value.
HasValue bool
hasValue bool

// The Value of this Option. Should be ignored if HasValue is false.
Value T
value T
}

// Some returns an `Option` of type `T` with the given value.
func Some[T any](value T) Option[T] {
return Option[T]{
HasValue: true,
Value: value,
hasValue: true,
value: value,
}
}

// Some returns an `Option` of type `T` with no value.
func None[T any]() Option[T] {
return Option[T]{}
}

// HasValue returns a boolean indicating whether or not this optino contains a value. If
// it returns true, this Option contains a value, if it is false it contains no value.
func (o Option[T]) HasValue() bool {
return o.hasValue
}

// Value returns the Value of this Option. Value returned is invalid HasValue() is false
// and should be ignored.
func (o Option[T]) Value() T {
return o.value
}
16 changes: 8 additions & 8 deletions connor/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ func eq(condition, data interface{}) (bool, error) {
return false, nil

case client.Option[bool]:
if !arr.HasValue {
if !arr.HasValue() {
return condition == nil, nil
}
data = arr.Value
data = arr.Value()

case client.Option[int64]:
if !arr.HasValue {
if !arr.HasValue() {
return condition == nil, nil
}
data = arr.Value
data = arr.Value()

case client.Option[float64]:
if !arr.HasValue {
if !arr.HasValue() {
return condition == nil, nil
}
data = arr.Value
data = arr.Value()

case client.Option[string]:
if !arr.HasValue {
if !arr.HasValue() {
return condition == nil, nil
}
data = arr.Value
data = arr.Value()
}

switch cn := condition.(type) {
Expand Down
8 changes: 4 additions & 4 deletions query/graphql/planner/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ func (n *sumNode) Next() (bool, error) {

case []client.Option[int64]:
collectionSum, err = sumItems(childCollection, source.Filter, func(childItem client.Option[int64]) float64 {
if !childItem.HasValue {
if !childItem.HasValue() {
return 0
}
return float64(childItem.Value)
return float64(childItem.Value())
})

case []float64:
Expand All @@ -238,10 +238,10 @@ func (n *sumNode) Next() (bool, error) {

case []client.Option[float64]:
collectionSum, err = sumItems(childCollection, source.Filter, func(childItem client.Option[float64]) float64 {
if !childItem.HasValue {
if !childItem.HasValue() {
return 0
}
return childItem.Value
return childItem.Value()
})
}
if err != nil {
Expand Down

0 comments on commit 58fabba

Please sign in to comment.