null

package module
Version: v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2021 License: MIT Imports: 5 Imported by: 4

README

Nullable Go types

Go Reference CircleCI Go Report Card Chat on Discord

Description

This package provides nullable Go types for bool, float64, int64, int32, string and time.Time replacing sql.NullString, sql.NullInt64, ... that can be marshalled/unmarshalled to/from JSON.

Installation

To install "null", run go get within your project:

go get github.com/emvi/null

Note that from 1.3 on "null" requires Go version 1.13 or newer.

Usage

Here is a short example demonstrating the string type. The other types (int64, float64 and bool) work in the same manner.

package main

import (
    "encoding/json"
    "database/sql"
    "fmt"

    "github.com/emvi/null"
)

type NullableString struct {
    Value null.String `json:"value"`
}

func main() {
    str := NullableString{null.NewString("nullable string", true)}
    // or long version: str := NullableString{null.String{sql.NullString{String: "nullable string", Valid: true}}}
    
    data, _ := json.Marshal(str)
    fmt.Println(string(data)) // -> {"value": "nullable"}

    str.SetNil() // use str.SetValid("value") to set a value again
    data, _ = json.Marshal(str)
    fmt.Println(string(data)) // -> {"value": null}
}

Contribute

See CONTRIBUTING.md

License

MIT

Documentation

Overview

Package null provides nullable types (based on database/sql) that can be parsed to and from JSON.

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 boolean type based on sql.NullBool, that supports parsing to/from JSON.

func NewBool

func NewBool(b, valid bool) Bool

NewBool returns a new nullable Bool object. This is equivalent to `null.Bool{sql.NullBool{Bool: b, Valid: valid}}`.

func (Bool) MarshalJSON

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

MarshalJSON implements the encoding json interface.

func (*Bool) SetNil

func (b *Bool) SetNil()

SetNil sets the value to default and valid to false.

func (*Bool) SetValid

func (b *Bool) SetValid(value bool)

SetValid sets the value and valid to true.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements the encoding json interface.

type Float64

type Float64 struct {
	sql.NullFloat64
}

Float64 is a nullable float64 type based on sql.NullFloat64, that supports parsing to/from JSON.

func NewFloat64

func NewFloat64(f float64, valid bool) Float64

NewFloat64 returns a new nullable Float64 object. This is equivalent to `null.Float64{sql.NullFloat64{Float64: f, Valid: valid}}`.

func (Float64) MarshalJSON

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

MarshalJSON implements the encoding json interface.

func (*Float64) SetNil

func (f *Float64) SetNil()

SetNil sets the value to default and valid to false.

func (*Float64) SetValid

func (f *Float64) SetValid(value float64)

SetValid sets the value and valid to true.

func (*Float64) UnmarshalJSON

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

UnmarshalJSON implements the encoding json interface.

type Int32

type Int32 struct {
	sql.NullInt32
}

Int32 is a nullable int32 type based on sql.NullInt32, that supports parsing to/from JSON.

func NewInt32

func NewInt32(i int32, valid bool) Int32

NewInt32 returns a new nullable Int32 object. This is equivalent to `null.Int32{sql.NullInt32{Int32: i, Valid: valid}}`.

func (Int32) MarshalJSON

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

MarshalJSON implements the encoding json interface.

func (*Int32) SetNil

func (i *Int32) SetNil()

SetNil sets the value to default and valid to false.

func (*Int32) SetValid

func (i *Int32) SetValid(value int32)

SetValid sets the value and valid to true.

func (*Int32) UnmarshalJSON

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

UnmarshalJSON implements the encoding json interface.

type Int64

type Int64 struct {
	sql.NullInt64
}

Int64 is a nullable int64 type based on sql.NullInt64, that supports parsing to/from JSON.

func NewInt64

func NewInt64(i int64, valid bool) Int64

NewInt64 returns a new nullable Int64 object. This is equivalent to `null.Int64{sql.NullInt64{Int64: i, Valid: valid}}`.

func (Int64) MarshalJSON

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

MarshalJSON implements the encoding json interface.

func (*Int64) SetNil

func (i *Int64) SetNil()

SetNil sets the value to default and valid to false.

func (*Int64) SetValid

func (i *Int64) SetValid(value int64)

SetValid sets the value and valid to true.

func (*Int64) UnmarshalJSON

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

UnmarshalJSON implements the encoding json interface.

type String

type String struct {
	sql.NullString
}

String is a nullable string type based on sql.NullString, that supports parsing to/from JSON.

func NewString

func NewString(s string, valid bool) String

NewString returns a new nullable String object. This is equivalent to `null.String{sql.NullString{String: s, Valid: valid}}`.

func (String) MarshalJSON

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

MarshalJSON implements the encoding json interface.

func (*String) SetNil

func (s *String) SetNil()

SetNil sets the value to default and valid to false.

func (*String) SetValid

func (s *String) SetValid(value string)

SetValid sets the value and valid to true.

func (*String) UnmarshalJSON

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

UnmarshalJSON implements the encoding json interface.

type Time

type Time struct {
	sql.NullTime
}

Time is a nullable time.Time, that supports parsing to/from JSON.

func NewTime

func NewTime(t time.Time, valid bool) Time

NewTime returns a new nullable time.Time object. This is equivalent to `null.Time{Time: t, Valid: valid}`.

func (Time) MarshalJSON

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

MarshalJSON implements the encoding json interface.

func (*Time) Scan

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

Scan implements the Scanner interface.

func (*Time) SetNil

func (t *Time) SetNil()

SetNil sets the value to default and valid to false.

func (*Time) SetValid

func (t *Time) SetValid(value time.Time)

SetValid sets the value and valid to true.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the encoding json interface.

func (Time) Value

func (t 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