Giter VIP home page Giter VIP logo

go-nulltype's Introduction

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

go-nulltype's People

Contributors

a-rieger avatar cxlblm avatar mattn avatar orisano avatar rhty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-nulltype's Issues

Does not support YAML parser?

I am using the github.com/go-yaml/yaml package to parse the YAML file, but go-nulltype does not seem to support this package, how can I solve this problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.