nullify

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2019 License: MIT Imports: 7 Imported by: 0

README

Nullify for Golang

Nullify is a simple go package to manage null values returned by the SQL driver when building a JSON API.

It is composed of 2 parts:

  • data type wrappers for the SQL driver null types
  • JSON marshalling methods for null elements

Example:

package main

import(
	"database/sql"
	"fmt"
	"log"
	"net/http"

	// Default driver is MySQL
	_ "github.com/go-sql-driver/mysql"
)

var db *sql.DB

type Employee struct {
    name NullString `json:"name"`
    age NullInt64 `json:"age"`
    active NullBool `json:"active"`
}

func fatal(err error) {
    if ! err.nil {
        log.Fatal(err)
    }
}


func main() {
    var err error
    var employees []employee

    db, err := sql.Open("mysql", "user:password@127.0.0.1/database")
    fatal(err)

    db.Ping()
    fatal(err)

    result, err := db.Query("SELECT * from employees")
    fatal(err)

    for result.Next() {
        var employee Employee
        err = result.Scan(&employee.name, &employee.age, &employee.active)
        fatal(err)
        employees = append(employees, employee)
    }

    defer result.Close()

    fmt.PrintLn(json.Marshal(employees))

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NullBool

type NullBool struct {
	sql.NullBool
}

NullBool is an alias for sql.NullBool data type

func (NullBool) MarshalJSON

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

MarshalJSON for NullBool

func (*NullBool) Scan

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

Scan implements the Scanner interface.

func (*NullBool) UnmarshalJSON

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

UnmarshalJSON for NullBool

func (NullBool) Value

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

Value implements the driver Valuer interface.

type NullFloat64

type NullFloat64 struct {
	sql.NullFloat64
}

NullFloat64 is an alias for sql.NullFloat64 data type

func (NullFloat64) MarshalJSON

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

MarshalJSON for NullFloat64

func (*NullFloat64) Scan

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

Scan implements the Scanner interface.

func (*NullFloat64) UnmarshalJSON

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

UnmarshalJSON for NullFloat64

func (NullFloat64) Value

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

Value implements the driver Valuer interface.

type NullInt64

type NullInt64 struct {
	sql.NullInt64
}

NullInt64 is an alias for sql.NullInt64 data type

func (NullInt64) MarshalJSON

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

MarshalJSON for NullInt64

func (*NullInt64) Scan

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

Scan implements the Scanner interface.

func (*NullInt64) UnmarshalJSON

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

UnmarshalJSON for Nullint64

func (NullInt64) Value

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

Value implements the driver Valuer interface.

type NullString

type NullString struct {
	sql.NullString
}

NullString is an alias for sql.NullString data type

func (NullString) MarshalJSON

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

MarshalJSON for NullString

func (*NullString) Scan

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

Scan implements the Scanner interface.

func (*NullString) UnmarshalJSON

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

UnmarshalJSON for NullString

func (NullString) Value

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

Value implements the driver Valuer interface.

type NullTime

type NullTime struct {
	mysql.NullTime
}

NullTime is an alias for mysql.NullTime data type

func (NullTime) MarshalJSON

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

MarshalJSON for NullTime

func (*NullTime) Scan

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

Scan implements the Scanner interface. The value type must be time.Time or string / []byte (formatted time-string), otherwise Scan fails.

func (*NullTime) UnmarshalJSON

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

UnmarshalJSON for NullTime

func (NullTime) Value

func (nt NullTime) 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