Skip to content

Commit

Permalink
Fix IsNull method :(
Browse files Browse the repository at this point in the history
  • Loading branch information
pivaldi committed Nov 20, 2023
1 parent 1aa2b70 commit 8c6b8e8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nullable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type JSON any

type NullableI[T bool | int | int16 | int32 | int64 | string | float64 | JSON] interface {
// IsNull returns true iff the value is nil/null
// IsNull returns true if itself is nil or the value is nil/null
IsNull() bool
// GetValue implements the getter.
GetValue() *T
Expand Down
9 changes: 3 additions & 6 deletions of.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nullable
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"
)
Expand All @@ -13,7 +14,7 @@ type Of[T bool | int | int16 | int32 | int64 | string | float64 | JSON] struct {

// IsNull returns true iff the value is nil
func (n *Of[T]) IsNull() bool {
return n.Val != nil
return n == nil || n.Val == nil
}

// GetValue implements the getter.
Expand Down Expand Up @@ -93,10 +94,6 @@ func (n *Of[T]) UnmarshalJSON(data []byte) error {

// Value implements the driver.Valuer interface.
func (n *Of[T]) Value() (driver.Value, error) {
if n == nil {
panic("calling Value on nil receiver")
}

if n.IsNull() {
return nil, nil
}
Expand Down Expand Up @@ -124,7 +121,7 @@ func (n *Of[T]) Value() (driver.Value, error) {
// This method decodes a JSON-encoded value into the struct.
func (n *Of[T]) Scan(v any) error {
if n == nil {
panic("calling Scan on nil receiver")
return errors.New("calling Scan on nil receiver")
}

switch any(n.Val).(type) {
Expand Down

0 comments on commit 8c6b8e8

Please sign in to comment.