nulltype

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 6 Imported by: 0

README

nulltype

License Go Reference CI Codecov Go Report Card

Installation

$ go get -u github.com/r-fujiyama/nulltype

Example

import (
	"bytes"
	"encoding/json"
	"fmt"
	"strings"

	"github.com/r-fujiyama/nulltype"
)

func main() {
	// JSON Encode
	s1 := nulltype.String{String: "foo", Valid: true}
	var buf bytes.Buffer
	_ = json.NewEncoder(&buf).Encode(s1)
	fmt.Print(buf.String()) // "foo"

	buf.Reset()
	s1 = nulltype.String{String: "", Valid: true}
	_ = json.NewEncoder(&buf).Encode(s1)
	fmt.Print(buf.String()) // ""

	buf.Reset()
	s1 = nulltype.String{String: "", Valid: false}
	_ = json.NewEncoder(&buf).Encode(s1)
	fmt.Print(buf.String()) // null

	// JSON Decode
	s2 := nulltype.String{}
	_ = json.NewDecoder(strings.NewReader(`"foo"`)).Decode(&s2)
	fmt.Printf("%#v\n", s2) // nulltype.String{String:"foo", Valid:true}

	_ = json.NewDecoder(strings.NewReader(`""`)).Decode(&s2)
	fmt.Printf("%#v\n", s2) // nulltype.String{String:"", Valid:true}

	_ = json.NewDecoder(strings.NewReader("null")).Decode(&s2)
	fmt.Printf("%#v\n", s2) // nulltype.String{String:"", Valid:false}
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONMarshal

func JSONMarshal(t interface{}) ([]byte, error)

Types

type Bool

type Bool struct {
	Bool  bool
	Valid bool
}

Bool represents a bool that may be null.

func (*Bool) IsNull

func (b *Bool) IsNull() bool

IsNull returns true if Valid is false.

func (Bool) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Bool) Scan

func (b *Bool) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Bool) Value

func (b Bool) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Byte

type Byte struct {
	Byte  byte
	Valid bool
}

Byte represents a byte that may be null.

func (*Byte) IsNull

func (b *Byte) IsNull() bool

IsNull returns true if Valid is false.

func (Byte) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Byte) Scan

func (b *Byte) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Byte) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Byte) Value

func (b Byte) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Float32

type Float32 struct {
	Float32 float32
	Valid   bool
}

Float32 represents a float32 that may be null.

func (*Float32) IsNull

func (f *Float32) IsNull() bool

IsNull returns true if Valid is false.

func (Float32) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Float32) Scan

func (f *Float32) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Float32) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Float32) Value

func (f Float32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Float64

type Float64 struct {
	Float64 float64
	Valid   bool
}

Float64 represents a float64 that may be null.

func (*Float64) IsNull

func (f *Float64) IsNull() bool

IsNull returns true if Valid is false.

func (Float64) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Float64) Scan

func (f *Float64) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Float64) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Float64) Value

func (f Float64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Int

type Int struct {
	Int   int
	Valid bool
}

Int represents a int that may be null.

func (*Int) IsNull

func (i *Int) IsNull() bool

IsNull returns true if Valid is false.

func (Int) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Int) Scan

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

Scan implements the Scanner interface.

func (*Int) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Int) Value

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

Value implements the driver Valuer interface.

type Int16

type Int16 struct {
	Int16 int16
	Valid bool
}

Int16 represents a int16 that may be null.

func (*Int16) IsNull

func (i *Int16) IsNull() bool

IsNull returns true if Valid is false.

func (Int16) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Int16) Scan

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

Scan implements the Scanner interface.

func (*Int16) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Int16) Value

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

Value implements the driver Valuer interface.

type Int32

type Int32 struct {
	Int32 int32
	Valid bool
}

Int32 represents a int32 that may be null.

func (*Int32) IsNull

func (i *Int32) IsNull() bool

IsNull returns true if Valid is false.

func (Int32) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Int32) Scan

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

Scan implements the Scanner interface.

func (*Int32) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Int32) Value

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

Value implements the driver Valuer interface.

type Int64

type Int64 struct {
	Int64 int64
	Valid bool
}

Int64 represents a int64 that may be null.

func (*Int64) IsNull

func (i *Int64) IsNull() bool

IsNull returns true if Valid is false.

func (Int64) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Int64) Scan

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

Scan implements the Scanner interface.

func (*Int64) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Int64) Value

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

Value implements the driver Valuer interface.

type Int8

type Int8 struct {
	Int8  int8
	Valid bool
}

Int8 represents a int8 that may be null.

func (*Int8) IsNull

func (i *Int8) IsNull() bool

IsNull returns true if Valid is false.

func (Int8) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Int8) Scan

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

Scan implements the Scanner interface.

func (*Int8) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (Int8) Value

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

Value implements the driver Valuer interface.

type String

type String struct {
	String string
	Valid  bool
}

String represents a string that may be null.

func (*String) IsEmpty

func (s *String) IsEmpty() bool

IsEmpty return true if String is "" or Valid is false.

func (*String) IsNull

func (s *String) IsNull() bool

IsNull returns true if Valid is false.

func (String) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*String) Scan

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

Scan implements the Scanner interface.

func (*String) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

func (String) Value

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

Value implements the driver Valuer interface.

type Time

type Time struct {
	Time  time.Time
	Valid bool
}

Time represents a bool that may be null.

func (*Time) IsNull

func (s *Time) IsNull() bool

IsNull returns true if Valid is false.

func (Time) MarshalJSON

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

MarshalJSON encode the value to JSON.

func (*Time) Scan

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

Scan implements the Scanner interface.

func (*Time) UnmarshalJSON

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

UnmarshalJSON decode data to the value.

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