github.com/naemono/pop@v4.13.1+incompatible/nulls/nulls.go (about)

     1  package nulls
     2  
     3  import (
     4  	"database/sql/driver"
     5  
     6  	"github.com/gofrs/uuid"
     7  )
     8  
     9  // nullable a generic representation of nulls type.
    10  type nullable interface {
    11  	Interface() interface{}
    12  	Value() (driver.Value, error)
    13  }
    14  
    15  // Nulls a generic nulls type. something that implements
    16  // nullable interface. can be any of nulls.Int, nulls.uuid.UUID
    17  // nulls.String, etc.
    18  type Nulls struct {
    19  	Value interface{}
    20  }
    21  
    22  // Interface calls Interface function for value.
    23  func (nulls *Nulls) Interface() interface{} {
    24  	n := nulls.Value.(nullable)
    25  	return n.Interface()
    26  }
    27  
    28  // Parse parses the specified value to the corresponding
    29  // nullable type. value is one of the inner value hold
    30  // by a nullable type. i.e int, string, uuid.UUID etc.
    31  func (nulls *Nulls) Parse(value interface{}) interface{} {
    32  	switch nulls.Value.(type) {
    33  	case Int:
    34  		return NewInt(value.(int))
    35  	case Int64:
    36  		return NewInt64(value.(int64))
    37  	case UUID:
    38  		return NewUUID(value.(uuid.UUID))
    39  	default:
    40  		return value
    41  	}
    42  }
    43  
    44  // New returns a wrapper called nulls for the
    45  // interface passed as a param.
    46  func New(i interface{}) *Nulls {
    47  	if _, ok := i.(nullable); !ok {
    48  		return nil
    49  	}
    50  	return &Nulls{Value: i}
    51  }