null

package module
v1.0.0-...-5cd2a75 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2014 License: BSD-2-Clause Imports: 4 Imported by: 0

README

null GoDoc Coverage

null is a library with opinions on how to deal with nullable SQL and JSON values

There are two packages: null and nuller.

Types in null are treated like zero values in Go: blank string input will produce a null null.String, and null Strings will JSON encode to "". If you need zero and null treated the same, use these.

Types in nuller will only be considered null on null input, and will JSON encode to null. If you need zero and null be considered separate values, use these.

All types implement sql.Scanner and driver.Valuer, so you can use this library in place of sql.NullXXX. All types also implement: encoding.TextMarshaler, encoding.TextUnmarshaler, json.Marshaler, and json.Unmarshaler.

null.String

A nullable string.

Will marshal to a blank string if null. Blank string input produces a null String. In other words, null values and empty values are considered equivalent. Can unmarshal from sql.NullString JSON input.

null.Int

A nullable int64.

Will marshal to 0 if null. Blank string or 0 input produces a null Int. In other words, null values and empty values are considered equivalent. Can unmarshal from sql.NullInt64 JSON input.

null.Float

A nullable float64.

Will marshal to 0 if null. Blank string or 0 input produces a null Float. In other words, null values and empty values are considered equivalent. Can unmarshal from sql.NullFloat64 JSON input.

null.Bool

A nullable bool.

Will marshal to false if null. Blank string or false input produces a null Float. In other words, null values and empty values are considered equivalent. Can unmarshal from sql.NullBool JSON input.

nuller.String

An even nuller nullable string.

Unlike null.String, nuller.String will marshal to null if null. Zero (blank) input will not produce a null String. Can unmarshal from sql.NullString JSON input.

nuller.Int

An even nuller nullable int64.

Unlike null.Int, nuller.Int will marshal to null if null. Zero input will not produce a null Int. Can unmarshal from sql.NullInt64 JSON input.

nuller.Float

An even nuller nullable float64.

Unlike null.Float, nuller.Float will marshal to null if null. Zero input will not produce a null Float. Can unmarshal from sql.NullFloat64 JSON input.

nuller.Bool

An even nuller nullable float64.

Unlike null.Bool, nuller.Bool will marshal to null if null. False input will not produce a null Bool. Can unmarshal from sql.NullBool JSON input.

Bugs

json's ",omitempty" struct tag does not work correctly right now. It will never omit a null or empty String. This should be fixed in Go 1.4.

License

BSD

Documentation

Overview

Package null provides a convenient way of handling null values. Types in this package consider empty or zero input the same as null input. Types in this package will encode to their zero value, even if null. Use the nuller subpackage if you don't want this.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	sql.NullBool
}

Bool is a nullable bool.

func BoolFrom

func BoolFrom(b bool) Bool

BoolFrom creates a new Bool that will be null if false.

func BoolFromPtr

func BoolFromPtr(b *bool) Bool

BoolFromPtr creates a new Bool that be null if b is nil.

func NewBool

func NewBool(b bool, valid bool) Bool

NewBool creates a new Bool

func (Bool) IsZero

func (b Bool) IsZero() bool

IsZero returns true for null or zero Bools, for future omitempty support (Go 1.4?)

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this Bool is null.

func (Bool) MarshalText

func (b Bool) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It will encode a zero if this Bool is null.

func (Bool) Ptr

func (b Bool) Ptr() *bool

Ptr returns a poBooler to this Bool's value, or a nil poBooler if this Bool is null.

func (*Bool) SetValid

func (b *Bool) SetValid(v bool)

SetValid changes this Bool's value and also sets it to be non-null.

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. "false" will be considered a null Bool. It also supports unmarshalling a sql.NullBool.

func (*Bool) UnmarshalText

func (b *Bool) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Bool if the input is a false or not a bool. It will return an error if the input is not a float, blank, or "null".

type Float

type Float struct {
	sql.NullFloat64
}

Float is a nullable float64.

func FloatFrom

func FloatFrom(f float64) Float

FloatFrom creates a new Float that will be null if zero.

func FloatFromPtr

func FloatFromPtr(f *float64) Float

FloatFromPtr creates a new Float that be null if f is nil.

func NewFloat

func NewFloat(f float64, valid bool) Float

NewFloat creates a new Float

func (Float) IsZero

func (f Float) IsZero() bool

IsZero returns true for null or zero Floats, for future omitempty support (Go 1.4?)

func (Float) MarshalJSON

func (f Float) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this Float is null.

func (Float) MarshalText

func (f Float) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It will encode a zero if this Float is null.

func (Float) Ptr

func (f Float) Ptr() *float64

Ptr returns a poFloater to this Float's value, or a nil poFloater if this Float is null.

func (*Float) SetValid

func (f *Float) SetValid(v float64)

SetValid changes this Float's value and also sets it to be non-null.

func (*Float) UnmarshalJSON

func (f *Float) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Float. It also supports unmarshalling a sql.NullFloat64.

func (*Float) UnmarshalText

func (f *Float) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Float if the input is a blank, zero, or not a float. It will return an error if the input is not a float, blank, or "null".

type Int

type Int struct {
	sql.NullInt64
}

Int is a nullable int64.

func IntFrom

func IntFrom(i int64) Int

IntFrom creates a new Int that will be null if zero.

func IntFromPtr

func IntFromPtr(i *int64) Int

IntFromPtr creates a new String that be null if i is nil.

func NewInt

func NewInt(i int64, valid bool) Int

NewInt creates a new Int

func (Int) IsZero

func (i Int) IsZero() bool

IsZero returns true for null or zero Ints, for future omitempty support (Go 1.4?)

func (Int) MarshalJSON

func (i Int) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode 0 if this Int is null.

func (Int) MarshalText

func (i Int) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It will encode a zero if this Int is null.

func (Int) Ptr

func (i Int) Ptr() *int64

Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.

func (*Int) SetValid

func (i *Int) SetValid(n int64)

SetValid changes this Int's value and also sets it to be non-null.

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Int. It also supports unmarshalling a sql.NullInt64.

func (*Int) UnmarshalText

func (i *Int) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int if the input is a blank, zero, or not an integer. It will return an error if the input is not an integer, blank, or "null".

type String

type String struct {
	sql.NullString
}

String is a nullable string.

func NewString

func NewString(s string, valid bool) String

NewString creates a new String

func StringFrom

func StringFrom(s string) String

StringFrom creates a new String that will be null if s is blank.

func StringFromPtr

func StringFromPtr(s *string) String

StringFromPtr creates a new String that be null if s is nil or blank. It will make s point to the String's value.

func (String) IsZero

func (s String) IsZero() bool

IsZero returns true for null or empty strings, for future omitempty support. (Go 1.4?)

func (String) MarshalText

func (s String) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It will encode a blank string when this String is null.

func (String) Ptr

func (s String) Ptr() *string

Ptr returns a pointer to this String's value, or a nil pointer if this String is null.

func (*String) SetValid

func (s *String) SetValid(v string)

SetValid changes this String's value and also sets it to be non-null.

func (*String) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports string and null input. Blank string input produces a null String. It also supports unmarshalling a sql.NullString.

func (*String) UnmarshalText

func (s *String) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null String if the input is a blank string.

Directories

Path Synopsis
Package nuller contains types that consider zero input and null input as separate values.
Package nuller contains types that consider zero input and null input as separate values.

Jump to

Keyboard shortcuts

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