builtin

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2014 License: MIT Imports: 0 Imported by: 5

README

builtin

wrap builtin Go types to make them optional and interfacy

Status

stable, ready for consumption, feature complete

Build Status GoDoc Coverage Status

Example 1: not set vs zero value

package main

import (
    "encoding/json"
    "fmt"
    b "gopkg.in/go-on/builtin.v1"
)

type repo struct {
    Name    string
    Desc    b.Stringer  `json:",omitempty"`
    Private b.Booler    `json:",omitempty"`
    Age     b.Int64er   `json:",omitempty"`
    Price   b.Float64er `json:",omitempty"`
}

func (r *repo) print() {
    b, _ := json.Marshal(r)
    fmt.Printf("%s\n", b)
}

func main() {
    notSet := &repo{Name: "not-set"}
    allSet := &repo{"allSet", b.String("the allset repo"), b.Bool(true), b.Int64(20), b.Float64(4.5)}
    zero := &repo{"", b.String(""), b.Bool(false), b.Int64(0), b.Float64(0)}

    allSet.print()
    notSet.print()
    zero.print()
}

// Output:
// {"Name":"allSet","Desc":"the allset repo","Private":true,"Age":20,"Price":4.5}
// {"Name":"not-set"}
// {"Name":"","Desc":"","Private":false,"Age":0,"Price":0}

Example 2: set nullable values with database/sql

package main

import (
    "database/sql"
    "fmt"
    "gopkg.in/go-on/builtin.v1"
    "gopkg.in/go-on/builtin/sqlnull.v1"
    "github.com/lib/pq"
)

type person struct {
    LastName  string
    FirstName builtin.Stringer
}

func query(db *sql.DB, q string) *person {
    r := db.QueryRow(q)
    var p = new(person)
    err := sqlnull.Wrap(r).Scan(&p.LastName, &p.FirstName)
    if err != nil {
        panic(err.Error())
    }
    return p
}

func main() {
    connectString, err := pq.ParseURL("postgres://docker:docker@172.17.0.2:5432/pgsqltest")
    if err != nil {
        panic(err.Error())
    }
    db, err := sql.Open("postgres", connectString)
    if err != nil {
        panic(err.Error())
    }

    fmt.Printf("%#v\n%#v\n%#v\n",
        query(db, `SELECT 'Doe', 'John'`),
        query(db, `SELECT 'Doe', null`),
        query(db, `SELECT 'Doe', ''`),
    )

    // Output
    // &main.person{LastName:"Doe", FirstName:"John"}
    // &main.person{LastName:"Doe", FirstName:builtin.Stringer(nil)}
    // &main.person{LastName:"Doe", FirstName:""}
}

Credits

The basic problem was well described in a blog post of Will Noris.

The nice solution was brought up by Levi Cook in the comments of the post.

I really think, there should be compiler support for the supported builtin types, so that the type wrappers are not necessary.

Therefor I have created a ticket that you might be interested in.

Documentation

Overview

Package builtin provides wrappers around builtin types to make them optional and let them fulfill a specific interface for each supported type.

Example

This example shows how to distinguish values that are not set from zero values

type repo struct {
	Name    string
	Desc    Stringer  `json:",omitempty"`
	Private Booler    `json:",omitempty"`
	Age     Int64er   `json:",omitempty"`
	Price   Float64er `json:",omitempty"`
}

print := func(r *repo) {
	b, _ := json.Marshal(r)
	fmt.Printf("%s\n", b)
}

notSet := &repo{Name: "not-set"}
allSet := &repo{"allSet", String("the allset repo"), Bool(true), Int64(20), Float64(4.5)}
zero := &repo{"", String(""), Bool(false), Int64(0), Float64(0)}

print(allSet)
print(notSet)
print(zero)
Output:

{"Name":"allSet","Desc":"the allset repo","Private":true,"Age":20,"Price":4.5}
{"Name":"not-set"}
{"Name":"","Desc":"","Private":false,"Age":0,"Price":0}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool bool

func (Bool) Bool

func (b Bool) Bool() bool

type Booler

type Booler interface {
	Bool() bool
}

type Byte

type Byte byte

func (Byte) Byte

func (b Byte) Byte() byte

type Byter

type Byter interface {
	Byte() byte
}

type Complex128

type Complex128 complex128

func (Complex128) Complex128

func (c Complex128) Complex128() complex128

type Complex128er

type Complex128er interface {
	Complex128() complex128
}

type Complex64

type Complex64 complex64

func (Complex64) Complex64

func (c Complex64) Complex64() complex64

type Complex64er

type Complex64er interface {
	Complex64() complex64
}

type Float32

type Float32 float32

func (Float32) Float32

func (f Float32) Float32() float32

type Float32er

type Float32er interface {
	Float32() float32
}

type Float64

type Float64 float64

func (Float64) Float64

func (f Float64) Float64() float64

type Float64er

type Float64er interface {
	Float64() float64
}

type Int

type Int int

func (Int) Int

func (i Int) Int() int

type Int16

type Int16 int16

func (Int16) Int16

func (i Int16) Int16() int16

type Int16er

type Int16er interface {
	Int16() int16
}

type Int32

type Int32 int32

func (Int32) Int32

func (i Int32) Int32() int32

type Int32er

type Int32er interface {
	Int32() int32
}

type Int64

type Int64 int64

func (Int64) Int64

func (i Int64) Int64() int64

type Int64er

type Int64er interface {
	Int64() int64
}

type Int8

type Int8 int8

func (Int8) Int8

func (i Int8) Int8() int8

type Int8er

type Int8er interface {
	Int8() int8
}

type Inter

type Inter interface {
	Int() int
}

type Rune

type Rune rune

func (Rune) Rune

func (r Rune) Rune() rune

type Runer

type Runer interface {
	Rune() rune
}

type String

type String string

func (String) String

func (s String) String() string

type Stringer

type Stringer interface {
	String() string
}

type Uint

type Uint uint

func (Uint) Uint

func (u Uint) Uint() uint

type Uint16

type Uint16 uint16

func (Uint16) Uint16

func (u Uint16) Uint16() uint16

type Uint16er

type Uint16er interface {
	Uint16() uint16
}

type Uint32

type Uint32 uint32

func (Uint32) Uint32

func (u Uint32) Uint32() uint32

type Uint32er

type Uint32er interface {
	Uint32() uint32
}

type Uint64

type Uint64 uint64

func (Uint64) Uint64

func (u Uint64) Uint64() uint64

type Uint64er

type Uint64er interface {
	Uint64() uint64
}

type Uint8

type Uint8 uint8

func (Uint8) Uint8

func (u Uint8) Uint8() uint8

type Uint8er

type Uint8er interface {
	Uint8() uint8
}

type Uinter

type Uinter interface {
	Uint() uint
}

Directories

Path Synopsis
examples
Package sqlnull provides a wrapper around a Scanner such as *sql.Row or *sql.Rows from database/sql that simplifies the handling of nullable results.
Package sqlnull provides a wrapper around a Scanner such as *sql.Row or *sql.Rows from database/sql that simplifies the handling of nullable results.

Jump to

Keyboard shortcuts

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