Null friendly types
Branch: master
Clone or download
Latest commit fd89418 Jan 31, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml Add CI tests Jan 23, 2019
README.md Update doc Jan 31, 2019
bool.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
bool_test.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
example_fmt_stringer_test.go Add example tests Jan 31, 2019
example_json_marshaler_test.go Add example tests Jan 31, 2019
example_json_unmarshaler_test.go Add example tests Jan 31, 2019
float64.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
float64_test.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
go.mod Add go.mod Jan 31, 2019
go.test.sh chmod +x Jan 23, 2019
int64.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
int64_test.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
nulltype.go Fix godoc Jan 31, 2019
nulltype_test.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
string.go
string_test.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
time.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019
time_test.go Drastic change, Stringer return empty string instead of "nil" Jan 31, 2019

README.md

go-nulltype

Build Status codecov

Nullable types friendly to json.Encoder, json.Decoder, database/sql, fmt.Stringer, text/template, html/template, some of ORMs.

Supported types:

  • NullBool
  • NullString
  • NullFloat64
  • NullInt64
  • NullTime

Usage

import "github.com/mattn/go-nulltype"

type User struct {
	Name	nulltype.NullString `json:"name"`
}

friendly to Stringer

var user User
fmt.Println(user.Name.Valid()) // false
fmt.Println(user.Name) // ""

user.Name.Set("Bob")
fmt.Println(user.Name.Valid()) // true
fmt.Println(user.Name) // "Bob"

fmt.Println(user.Name.StringValue() == "Bob") // true

user.Name.Reset()
fmt.Println(user.Name.Valid()) // false
fmt.Println(user.Name) // ""

friendly to json.MarshalJSON

var user User
fmt.Println(user.Name.Valid()) // false
json.NewEncoder(os.Stdout).Encode(user) // {"name": null}

user.Name.Set("Bob")
fmt.Println(user.Name.Valid()) // true
json.NewEncoder(os.Stdout).Encode(user) // {"name": "Bob"}

friendly to json.UnmarshalJSON

var user User
s := `{"name": "Bob"}`
json.NewDecoder(strings.NewReader(s)).Decode(&user)
fmt.Println(user.Name.Valid()) // true
fmt.Println(user.Name) // "Bob"

s = `{"name": null}`
json.NewDecoder(strings.NewReader(s)).Decode(&user)
fmt.Println(user.Name.Valid()) // false
fmt.Println(user.Name) // ""

friendly to database/sql

var user User
db.QueryRow(`SELECT name FROM users`).Scan(&user.Name)
fmt.Println(user.Name.Valid()) // true or false
fmt.Println(user.Name) // "Bob" or ""
db.Exec(`INSERT INTO users(name) VALUES($1)`, user.Name)

friendly to ORM

Struct tag with gorp.

type Post struct {
	Id      int64 `db:"post_id"`
	Created int64
	Title   string              `db:",size:50"`
	Body    nulltype.NullString `db:"body,size:1024"`
}
p := Post{
	Created: time.Now().UnixNano(),
	Title:   title,
	Body:    nulltype.NullStringOf(body),
}
err = dbmap.Insert(&p)

Installation

go get github.com/mattn/go-nulltype

License

MIT

Author

Yasuhiro Matsumoto