nullable

package module
v1.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2023 License: MIT Imports: 12 Imported by: 0

README

Nullable SQL Data Types for Golang (GO)

Features

  • 100% GORM support
  • Can be marshalled into JSON
  • Can be unmarshal from JSON
  • Convenient Set/Get operation
  • Support MySQL, MariaDB, SQLite, and PostgreSQL
  • Zero configuration, just use it as normal data type.
  • Heavily tested! So you don't have to worry of many bugs :D

Supported Data Types

  • uint

How to Use?

Very easy! first of all, let's install like normal Go packages

go get github.com/Valdenirmezadri/nullable

Create a new variable

all you need to have is a basic variable and a nullable variable created with nullable.NewUint(yourBasicVar).

import (
    "fmt"
    "gorm.io/gorm"
    "github.com/Valdenirmezadri/nullable"
)

func main() {
    // Create new
    var theNumber uint64 = 70
    nullableNumber := nullable.NewUint(theNumber)
    fmt.Println(nullableNumber.Get()) // Output: 70

    // Change to another number
    var anotherNumber uint64 = 3306
    nullableNumber.Set(anotherNumber)
    fmt.Println(nullableNumber.Get()) // Output: 3306

    // Change to nil
    nullableNumber.Set(nil)
    fmt.Println(nullableNumber.Get()) // Output: 0
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type String

type String struct {
	// contains filtered or unexported fields
}

String SQL type that can retrieve NULL value

func NewString

func NewString(value string) String

NewString creates a new nullable string

func (String) Get

func (n String) Get() string

Get either nil or string

func (String) GormDBDataType

func (String) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (String) GormDataType

func (String) GormDataType() string

GormDataType gorm common data type

func (String) MarshalJSON

func (n String) MarshalJSON() ([]byte, error)

MarshalJSON converts current value to JSON

func (*String) Scan

func (n *String) Scan(value interface{}) error

Scan implements scanner interface

func (*String) Set

func (n *String) Set(value string)

Set either nil or string

func (*String) UnmarshalJSON

func (n *String) UnmarshalJSON(data []byte) error

UnmarshalJSON writes JSON to this type

func (String) Value

func (n String) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Uint

type Uint struct {
	// contains filtered or unexported fields
}

Uint SQL type that can retrieve NULL value

func NewUint

func NewUint(value uint) Uint

NewUint creates a new nullable unsigned integer

func (Uint) Get

func (n Uint) Get() uint

Get either nil or unsigned integer

func (Uint) GormDBDataType

func (Uint) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (Uint) GormDataType

func (Uint) GormDataType() string

GormDataType gorm common data type

func (Uint) GormValue

func (n Uint) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

GormValue implements the driver Valuer interface via GORM.

func (Uint) MarshalJSON

func (n Uint) MarshalJSON() ([]byte, error)

MarshalJSON converts current value to JSON

func (*Uint) Scan

func (n *Uint) Scan(value interface{}) error

Scan implements scanner interface

func (*Uint) Set

func (n *Uint) Set(value uint)

Set either nil or unsigned integer

func (*Uint) UnmarshalJSON

func (n *Uint) UnmarshalJSON(data []byte) error

UnmarshalJSON writes JSON to this type

func (Uint) Value

func (n Uint) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Uint16

type Uint16 struct {
	// contains filtered or unexported fields
}

Uint16 SQL type that can retrieve NULL value

func NewUint16

func NewUint16(value uint16) Uint16

NewUint16 creates a new nullable 16-bit unsigned integer

func (Uint16) Get

func (n Uint16) Get() uint16

Get either nil or 16-bit unsigned integer

func (Uint16) GormDBDataType

func (Uint16) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (Uint16) GormDataType

func (Uint16) GormDataType() string

GormDataType gorm common data type

func (Uint16) GormValue

func (n Uint16) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

GormValue implements the driver Valuer interface via GORM.

func (Uint16) MarshalJSON

func (n Uint16) MarshalJSON() ([]byte, error)

MarshalJSON converts current value to JSON

func (*Uint16) Scan

func (n *Uint16) Scan(value interface{}) error

Scan implements scanner interface

func (*Uint16) Set

func (n *Uint16) Set(value uint16)

Set either nil or 16-bit unsigned integer

func (*Uint16) UnmarshalJSON

func (n *Uint16) UnmarshalJSON(data []byte) error

UnmarshalJSON writes JSON to this type

func (Uint16) Value

func (n Uint16) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Uint32

type Uint32 struct {
	// contains filtered or unexported fields
}

Uint32 SQL type that can retrieve NULL value

func NewUint32

func NewUint32(value uint32) Uint32

NewUint32 creates a new nullable 32-bit unsigned integer

func (Uint32) Get

func (n Uint32) Get() uint32

Get either nil or 32-bit unsigned integer

func (Uint32) GormDBDataType

func (Uint32) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (Uint32) GormDataType

func (Uint32) GormDataType() string

GormDataType gorm common data type

func (Uint32) GormValue

func (n Uint32) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

GormValue implements the driver Valuer interface via GORM.

func (Uint32) MarshalJSON

func (n Uint32) MarshalJSON() ([]byte, error)

MarshalJSON converts current value to JSON

func (*Uint32) Scan

func (n *Uint32) Scan(value interface{}) error

Scan implements scanner interface

func (*Uint32) Set

func (n *Uint32) Set(value uint32)

Set either nil or 32-bit unsigned integer

func (*Uint32) UnmarshalJSON

func (n *Uint32) UnmarshalJSON(data []byte) error

UnmarshalJSON writes JSON to this type

func (Uint32) Value

func (n Uint32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Uint8

type Uint8 struct {
	// contains filtered or unexported fields
}

Uint8 SQL type that can retrieve NULL value

func NewUint8

func NewUint8(value uint8) Uint8

NewUint8 creates a new nullable 8-bit integer

func (Uint8) Get

func (n Uint8) Get() uint8

Get either nil or 8-bit integer

func (Uint8) GormDBDataType

func (Uint8) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm db data type

func (Uint8) GormDataType

func (Uint8) GormDataType() string

GormDataType gorm common data type

func (Uint8) GormValue

func (n Uint8) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

GormValue implements the driver Valuer interface via GORM.

func (Uint8) MarshalJSON

func (n Uint8) MarshalJSON() ([]byte, error)

MarshalJSON converts current value to JSON

func (*Uint8) Scan

func (n *Uint8) Scan(value interface{}) error

Scan implements scanner interface

func (*Uint8) Set

func (n *Uint8) Set(value uint8)

Set either nil or 8-bit integer

func (*Uint8) UnmarshalJSON

func (n *Uint8) UnmarshalJSON(data []byte) error

UnmarshalJSON writes JSON to this type

func (Uint8) Value

func (n Uint8) Value() (driver.Value, error)

Value implements the driver Valuer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL