null

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2019 License: BSD-2-Clause Imports: 4 Imported by: 6

README

null int and string values

This module provides (yet another) alternative in dealing with integers and strings which may be null in your JSON or database. There are various different approaches to this, namely the built in golang SQL types and the guregu null module which adds in JSON support. These are fine approaches but both suffer from you having to deal with a struct type for your ids instead of a more natural int64 or string. That is fine in some cases but I prefer to use more primitive types as assignment is more straightforward, simple equality works, etc.

Sadly this requires a bit of boilerplate, so this package tries to make that a bit easier, proving helper methods for marshalling to/from json and reading / scanning from a database value. Due to the Golang type system, using these requires some boilerplate, but I find it nicer to have a bit more code for my ID types and less awkward code around the usage of ids rather than vice versa.

To define your own ID type which is nullable when written:

import "github.com/nyaruka/null"

type CustomID null.Int

const NullCustomID = CustomID(0)

// MarshalJSON marshals into JSON. 0 values will become null
func (i CustomID) MarshalJSON() ([]byte, error) {
	return null.Int(i).MarshalJSON()
}

// UnmarshalJSON unmarshals from JSON. null values become 0
func (i *CustomID) UnmarshalJSON(b []byte) error {
	return null.UnmarshalInt(b, (*null.Int)(i))
}

// Value returns the db value, null is returned for 0
func (i CustomID) Value() (driver.Value, error) {
	return null.Int(i).Value()
}

// Scan scans from the db value. null values become 0
func (i *CustomID) Scan(value interface{}) error {
	return null.ScanInt(value, (*null.Int)(i))
}

The process is essentially the same for nullable strings:

import "github.com/nyaruka/null"

type CustomString null.String

type NullCustomString = CustomString("")

// MarshalJSON marshals into JSON. "" values will become null
func (s CustomString) MarshalJSON() ([]byte, error) {
	return null.String(s).MarshalJSON()
}

// UnmarshalJSON unmarshals from JSON. null values become ""
func (s *CustomString) UnmarshalJSON(b []byte) error {
	return null.UnmarshalString(b, (*null.String)(s))
}

// Value returns the db value, null is returned for ""
func (s CustomString) Value() (driver.Value, error) {
	return null.String(s).Value()
}

// Scan scans from the db value. null values become ""
func (s *CustomString) Scan(value interface{}) error {
	return null.ScanString(value, (*null.String)(s))
}

If you don't care about type safety within your types, you can use a go alias which simplifies things. This is more useful for "stringy" things than it is for ids where you likely want to enforce type:

import "github.com/nyaruka/null"

// Note you lose type safety with aliases. FooID can be assigned to BarID and vice versa!
type FooID = null.Int
type BarID = null.Int

// Same here, any null.String can be assigned to CustomString
type CustomString = null.String

Documentation

Index

Constants

View Source
const NullInt = Int(0)

NullInt is our constant for an Int value that will be written as null

View Source
const NullString = String("")

NullString is our constant for an String value that will be written as null

Variables

This section is empty.

Functions

func ScanInt

func ScanInt(value interface{}, v *Int) error

ScanInt is a utility method that can be used to scan a db value and return an Int and error In the case of an error, null or "" values, NullInt is returned

func ScanString

func ScanString(value interface{}, v *String) error

ScanString is a utility method that can be used to scan a db value and return a String and error In the case of an error, null or "" values, NullString is returned

func UnmarshalInt

func UnmarshalInt(b []byte, v *Int) error

UnmarshalInt is a utility method that can be used to unmarshal a json value to an Int and error In the case of an error, null or "" values, NullInt is returned

func UnmarshalString

func UnmarshalString(b []byte, v *String) error

UnmarshalString is a utility method that can be used to unmarshal a json value to a String and error In the case of an error, null or "" values, NullString is returned

Types

type Int

type Int int64

Int is an int that will write as null when it is zero both to databases and json null values when unmashalled or scanned from a DB will result in a 0 value

func (Int) MarshalJSON

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

MarshalJSON marshals our int to JSON. 0 values will be marshalled as null

func (*Int) Scan

func (i *Int) Scan(value interface{}) error

Scan implements the Scanner interface for Int

func (*Int) UnmarshalJSON

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

UnmarshalJSON unmarshals our JSON to int. null values will be marshalled to 0

func (Int) Value

func (i Int) Value() (driver.Value, error)

Value implements the driver Valuer interface for Int

type JSON added in v1.2.0

type JSON json.RawMessage

JSON is a json.RawMessage that will marshall as null when empty or nil. null and {} values when unmashalled or scanned from a DB will result in a nil value

func (JSON) MarshalJSON added in v1.2.0

func (j JSON) MarshalJSON() ([]byte, error)

MarshalJSON encodes our JSON to JSON or null

func (*JSON) Scan added in v1.2.0

func (j *JSON) Scan(src interface{}) error

Scan implements the Scanner interface for decoding from a database

func (*JSON) UnmarshalJSON added in v1.2.0

func (j *JSON) UnmarshalJSON(data []byte) error

UnmarshalJSON sets our JSON from the passed in JSON

func (JSON) Value added in v1.2.0

func (j JSON) Value() (driver.Value, error)

Value implements the driver Valuer interface

type Map added in v1.1.1

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

Map is a one level deep dictionary that is represented as JSON text in the database. Empty maps will be written as null to the database and to JSON.

func NewMap added in v1.1.1

func NewMap(m map[string]interface{}) Map

NewMap creates a new Map

func (*Map) Get added in v1.1.1

func (m *Map) Get(key string, def interface{}) interface{}

Get returns the value with the passed in key, or def if not found

func (*Map) GetString added in v1.1.1

func (m *Map) GetString(key string, def string) string

GetString returns the string value with the passed in key, or def if not found or of wrong type

func (*Map) Map added in v1.1.1

func (m *Map) Map() map[string]interface{}

Map returns our underlying map

func (Map) MarshalJSON added in v1.1.1

func (m Map) MarshalJSON() ([]byte, error)

MarshalJSON encodes our map to JSON

func (*Map) Scan added in v1.1.1

func (m *Map) Scan(src interface{}) error

Scan implements the Scanner interface for decoding from a database

func (*Map) UnmarshalJSON added in v1.1.1

func (m *Map) UnmarshalJSON(data []byte) error

UnmarshalJSON sets our map from the passed in JSON

func (Map) Value added in v1.1.1

func (m Map) Value() (driver.Value, error)

Value implements the driver Valuer interface

type String

type String string

String is string that will write as null when it is "" both to databases and json null values when unmashalled or scanned from a DB will result in a "" value

func (String) MarshalJSON

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

MarshalJSON marshals our string to JSON. "" values will be marshalled as null

func (*String) Scan

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

Scan implements the Scanner interface for String

func (*String) UnmarshalJSON

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

UnmarshalJSON unmarshals our json to a string. null values will be marshalled to ""

func (String) Value

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

Value implements the driver Valuer interface for String

Jump to

Keyboard shortcuts

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