null

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2019 License: MIT Imports: 4 Imported by: 0

README

Build Status codecov Documentation

null

Easily scan/marshal between standard types and null values.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type String

type String struct {
	String string
	Valid  bool // Valid is true if String is not ""
}

String represents a string that may be null. String implements the Scanner interface so it can be used as a scan destination. String implements the Marshaller interface so it can be used to read and write null values.

func NewString

func NewString(t string) String
Example
package main

import (
	"fmt"
	"github.com/tomwright/null"
)

func main() {
	a := null.NewString("asd")
	b := null.NewString("")

	fmt.Printf("a: valid: `%v`, string: `%s`\n", a.Valid, a.String)
	fmt.Printf("b: valid: `%v`, string: `%s`\n", b.Valid, b.String)

}
Output:

a: valid: `true`, string: `asd`
b: valid: `false`, string: ``

func (String) MarshalJSON

func (nt String) MarshalJSON() ([]byte, error)
Example
package main

import (
	"encoding/json"
	"fmt"
	"github.com/tomwright/null"
)

func main() {
	a := null.NewString("asd")
	b := null.NewString("")

	aBytes, _ := json.Marshal(a)
	bBytes, _ := json.Marshal(b)

	fmt.Printf("a: %s\n", string(aBytes))
	fmt.Printf("b: %s", string(bBytes))

}
Output:

a: "asd"
b: null

func (*String) Scan

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

func (*String) UnmarshalJSON

func (nt *String) UnmarshalJSON(data []byte) error
Example
package main

import (
	"encoding/json"
	"fmt"
	"github.com/tomwright/null"
)

func main() {
	var (
		a null.String
		b null.String
		c null.String
		d null.String
	)

	json.Unmarshal([]byte(`"asd"`), &a)
	json.Unmarshal([]byte(`null`), &b)
	json.Unmarshal([]byte(`""`), &c)
	json.Unmarshal(nil, &d)

	fmt.Printf("a: valid: `%v`, string: `%s`\n", a.Valid, a.String)
	fmt.Printf("b: valid: `%v`, string: `%s`\n", b.Valid, b.String)
	fmt.Printf("c: valid: `%v`, string: `%s`\n", c.Valid, c.String)
	fmt.Printf("d: valid: `%v`, string: `%s`\n", d.Valid, d.String)

}
Output:

a: valid: `true`, string: `asd`
b: valid: `false`, string: ``
c: valid: `false`, string: ``
d: valid: `false`, string: ``

func (String) Value

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

type Time

type Time struct {
	Time  time.Time
	Valid bool
}

Time implements the Scanner interface so it can be used as a scan destination. Time implements the Marshaller interface so it can be used to read and write null values.

func NewTime

func NewTime(t time.Time) Time
Example
package main

import (
	"fmt"
	"github.com/tomwright/null"
	"time"
)

func main() {
	a := null.NewTime(time.Time{})
	b := null.NewTime(time.Date(2019, 01, 01, 12, 00, 00, 0, time.UTC))

	fmt.Printf("a: valid: `%v`, time: `%s`\n", a.Valid, a.Time.Format(time.RFC3339))
	fmt.Printf("b: valid: `%v`, time: `%s`\n", b.Valid, b.Time.Format(time.RFC3339))

}
Output:

a: valid: `false`, time: `0001-01-01T00:00:00Z`
b: valid: `true`, time: `2019-01-01T12:00:00Z`

func (Time) MarshalJSON

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

MarshalJSON returns either a marshal'd time.Time if it was valid, or a marshal'd NULL value if it was not valid.

func (*Time) Scan

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

Scan implements the Scanner interface.

func (Time) TimeOrZero

func (nt Time) TimeOrZero() time.Time

TimeOrZero returns either a valid time.Time, or a zero-value time.Time object.

func (*Time) UnmarshalJSON

func (nt *Time) UnmarshalJSON(data []byte) error
Example
package main

import (
	"encoding/json"
	"fmt"
	"github.com/tomwright/null"
	"time"
)

func main() {
	var (
		a null.Time
		b null.Time
		c null.Time
	)

	json.Unmarshal([]byte(`"2019-01-01T12:00:00Z"`), &a)
	json.Unmarshal([]byte(`null`), &b)
	json.Unmarshal([]byte(`""`), &c)

	fmt.Printf("a: valid: `%v`, time: `%s`\n", a.Valid, a.Time.Format(time.RFC3339))
	fmt.Printf("b: valid: `%v`, time: `%s`\n", b.Valid, b.Time.Format(time.RFC3339))
	fmt.Printf("c: valid: `%v`, time: `%s`\n", c.Valid, c.Time.Format(time.RFC3339))

}
Output:

a: valid: `true`, time: `2019-01-01T12:00:00Z`
b: valid: `false`, time: `0001-01-01T00:00:00Z`
c: valid: `false`, time: `0001-01-01T00:00:00Z`

func (Time) Value

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