nullable

package module
v0.0.0-...-2e266ff Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: MIT Imports: 2 Imported by: 0

README

nullable

Build Status Coverage Status GoDoc Go Report Card

Package nullable provides a simple way to marshal/unmarshal Go structs from sql.Null* types.

Installation

go get -u github.com/martinusso/nullable

Why

var person struct {
	Name        sql.NullString
	Age         sql.NullInt64
	Married     sql.NullBool
	Height      sql.NullFloat64
}

got, _ := json.Marshal(person)
fmt.Println(string(got))

Output:

{
   "Name":{
      "String":"",
      "Valid":false
   },
   "Age":{
      "Int64":0,
      "Valid":false
   },
   "Married":{
      "Bool":false,
      "Valid":false
   },
   "Height":{
      "Float64":0,
      "Valid":false
   }
}

Usage

type Person struct {
	Name        String
	Age         Int64
	Married     Bool
	Height      Float64
}
json.Marshaler
var person Person
got, _ := json.Marshal(person)

Output:

{
	"Name": "John Doe",
	"Age": 42,
	"Married": true,
	"Height": 1.79
}
json.Unmarshaler
body := []byte(`{"Name":"John Doe","Age":42,"Married":true,"Height":1.79}`)
var person Person
json.Unmarshal(body, &person)

Output:

(main.Person) {
 Name: (nullable.String) {
  NullString: (sql.NullString) {
   String: (string) (len=8) "John Doe",
   Valid: (bool) true
  }
 },
 Age: (nullable.Int64) {
  NullInt64: (sql.NullInt64) {
   Int64: (int64) 42,
   Valid: (bool) true
  }
 },
 Married: (nullable.Bool) {
  NullBool: (sql.NullBool) {
   Bool: (bool) true,
   Valid: (bool) true
  }
 },
 Height: (nullable.Float64) {
  NullFloat64: (sql.NullFloat64) {
   Float64: (float64) 1.79,
   Valid: (bool) true
  }
 }
}

License

This software is open source, licensed under the The MIT License (MIT). See LICENSE for details.

Documentation

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 represents a bool that may be null. Similar to sql.NullBool, but implement json.Marshaler/json.Unmarshaler

func (Bool) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface

type Float64

type Float64 struct {
	sql.NullFloat64
}

Float64 represents a float64 that may be null. Similar to sql.NullFloat64, but implement json.Marshaler/json.Unmarshaler

func (Float64) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*Float64) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface

type Int64

type Int64 struct {
	sql.NullInt64
}

Int64 represents an int64 that may be null. Similar to sql.NullInt64, but implement json.Marshaler/json.Unmarshaler

func (Int64) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*Int64) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface

type String

type String struct {
	sql.NullString
}

String represents a string that may be null. Similar to sql.NullString, but implement json.Marshaler/json.Unmarshaler

func (String) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*String) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface

Jump to

Keyboard shortcuts

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