nulltypes

package module
v0.0.0-...-3a9d8a4 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: MIT Imports: 3 Imported by: 0

README

nulltypes Build Status Go Report Card License

nulltypes is a golang module that provides an alternative to nullable data types from database/sql with proper JSON marshalling and unmarshalling.

It also provides a wrapper for time.Time to format time to use with timestamp of SQL databases, i.e. mysql, postgres.

The default database time zone is set to UTC, but it can easily be changed with:

nulltypes.DatabaseLocation, _ = time.LoadLocation([YOUR_TIME_ZONE])

Import

import "github.com/datumbrain/nulltypes"

Usage

Here is an example usage with GORM.

package models

type User struct {
	ID              uint                 `json:"id" gorm:"primary_key"`
	Name            string               `json:"name"`
	Address         nulltypes.NullString `json:"address,omitempty"`
	CreationDate    time.Time            `json:"-" gorm:"autoCreateTime:milli;default:current_timestamp"`
	UpdationDate    nulltypes.NullTime   `json:"-" gorm:"autoUpdateTime:milli"` 
	TerminationDate nulltypes.NullTime   `json:"termination_date,omitempty"`
	ManagerID       nulltypes.NullInt64  `json:"manager_id,omitempty" gorm:"OnUpdate:CASCADE,OnDelete:SET NULL"`
}
user := User{
	ID:           0,
	Name:         "John Doe",
	Address:      nulltypes.String("221B Baker Street"),
	CreationDate: time.Now(),
	UpdationDate: nulltypes.Now(),
	ManagerID:    nulltypes.Int64(5),
}

Author

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DatabaseLocation, _ = time.LoadLocation("UTC")

DatabaseLocation is local the timezone the database is set to default UTC

View Source
var TruncateOff = time.Microsecond

TruncateOff the degree of precision to REMOVE Default time.Microsecond

Functions

func ToDatabaseFormat

func ToDatabaseFormat(t time.Time) time.Time

insure the correct ToDatabaseFormat

Types

type NullBool

type NullBool struct {
	Bool  bool
	Valid bool
}

NullBool is a wrapper around bool

func Bool

func Bool(Bool bool) NullBool

Bool method to get NullBool object from bool

func (NullBool) MarshalJSON

func (nb NullBool) MarshalJSON() ([]byte, error)

MarshalJSON method is called by json.Marshal, whenever it is of type NullBool

func (*NullBool) Scan

func (nb *NullBool) Scan(value interface{}) error

Scan satisfies the sql.scanner interface

func (*NullBool) UnmarshalJSON

func (nb *NullBool) UnmarshalJSON(b []byte) error

UnmarshalJSON method is called by json.Unmarshal, whenever it is of type NullBool

func (NullBool) Value

func (nb NullBool) Value() (driver.Value, error)

Value satisfies the driver.Value interface

type NullFloat64

type NullFloat64 struct {
	Float64 float64
	Valid   bool
}

NullFloat64 is a wrapper around float64

func Float64

func Float64(Float64 float64) NullFloat64

Float64 method to get NullFloat64 object from float64

func (NullFloat64) MarshalJSON

func (nf NullFloat64) MarshalJSON() ([]byte, error)

MarshalJSON method is called by json.Marshal, whenever it is of type NullFloat64

func (*NullFloat64) Scan

func (nf *NullFloat64) Scan(value interface{}) error

Scan satisfies the sql.scanner floaterface

func (*NullFloat64) UnmarshalJSON

func (nf *NullFloat64) UnmarshalJSON(b []byte) error

UnmarshalJSON method is called by json.Unmarshal, whenever it is of type NullFloat64

func (NullFloat64) Value

func (nf NullFloat64) Value() (driver.Value, error)

Value satisfies the driver.Value interface

type NullInt32

type NullInt32 struct {
	Int32 int32
	Valid bool
}

NullInt32 is a wrapper around int32

func Int32

func Int32(Int32 int32) NullInt32

Int32 method to get NullInt32 object from int32

func (NullInt32) MarshalJSON

func (ni NullInt32) MarshalJSON() ([]byte, error)

MarshalJSON method is called by json.Marshal, whenever it is of type NullInt32

func (*NullInt32) Scan

func (ni *NullInt32) Scan(value interface{}) error

Scan satisfies the sql.scanner interface

func (*NullInt32) UnmarshalJSON

func (ni *NullInt32) UnmarshalJSON(b []byte) error

UnmarshalJSON method is called by json.Unmarshal, whenever it is of type NullInt32

func (NullInt32) Value

func (ni NullInt32) Value() (driver.Value, error)

Value satisfies the driver.Value interface

type NullInt64

type NullInt64 struct {
	Int64 int64
	Valid bool
}

NullInt64 is a wrapper around int64

func Int64

func Int64(Int64 int64) NullInt64

Int64 method to get NullInt64 object from int64

func (NullInt64) MarshalJSON

func (ni NullInt64) MarshalJSON() ([]byte, error)

MarshalJSON method is called by json.Marshal, whenever it is of type NullInt64

func (*NullInt64) Scan

func (ni *NullInt64) Scan(value interface{}) error

Scan satisfies the sql.scanner interface

func (*NullInt64) UnmarshalJSON

func (ni *NullInt64) UnmarshalJSON(b []byte) error

UnmarshalJSON method is called by json.Unmarshal, whenever it is of type NullInt64

func (NullInt64) Value

func (ni NullInt64) Value() (driver.Value, error)

Value satisfies the driver.Value interface

type NullString

type NullString struct {
	String string
	Valid  bool
}

NullString is a wrapper around string

func String

func String(s string) NullString

String method to get NullString object from string

func (NullString) MarshalJSON

func (ns NullString) MarshalJSON() ([]byte, error)

MarshalJSON method is called by json.Marshal, whenever it is of type NullString

func (*NullString) Scan

func (ns *NullString) Scan(value interface{}) error

Scan satisfies the sql.scanner interface

func (*NullString) UnmarshalJSON

func (ns *NullString) UnmarshalJSON(b []byte) error

UnmarshalJSON method is called by json.Unmarshal, whenever it is of type NullString

func (NullString) Value

func (ns NullString) Value() (driver.Value, error)

Value satisfies the driver.Value interface

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool `default:"false"`
}

NullTime is a wrapper around time.Time

func Date

func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) NullTime

Date wrapper around the time.Date() function

func Now

func Now() NullTime

Now wrapper around the time.Now() function

func Time

func Time(Time time.Time) NullTime

Time method to get NullTime object from time.Time

func (NullTime) MarshalJSON

func (nt NullTime) MarshalJSON() ([]byte, error)

MarshalJSON method is called by json.Marshal, whenever it is of type NullTime

func (*NullTime) Scan

func (nt *NullTime) Scan(value interface{}) error

Scan satisfies the sql.scanner interface

func (*NullTime) UnmarshalJSON

func (nt *NullTime) UnmarshalJSON(b []byte) error

UnmarshalJSON method is called by json.Unmarshal, whenever it is of type NullTime

func (NullTime) Value

func (nt NullTime) Value() (driver.Value, error)

Value satisfies the driver.Value interface

Jump to

Keyboard shortcuts

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