• Stars
    star
    1,723
  • Rank 26,152 (Top 0.6 %)
  • Language
    Go
  • License
    BSD 2-Clause "Sim...
  • Created almost 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

reasonable handling of nullable values

null GoDoc CircleCI

import "gopkg.in/guregu/null.v4"

null is a library with reasonable options for dealing with nullable SQL and JSON values

There are two packages: null and its subpackage zero.

Types in null will only be considered null on null input, and will JSON encode to null. If you need zero and null be considered separate values, use these.

Types in zero are treated like zero values in Go: blank string input will produce a null zero.String, and null Strings will JSON encode to "". Zero values of these types will be considered null to SQL. If you need zero and null treated the same, use these.

All types implement sql.Scanner and driver.Valuer, so you can use this library in place of sql.NullXXX. All types also implement: encoding.TextMarshaler, encoding.TextUnmarshaler, json.Marshaler, and json.Unmarshaler. A null object's MarshalText will return a blank string.

null package

import "gopkg.in/guregu/null.v4"

null.String

Nullable string.

Marshals to JSON null if SQL source data is null. Zero (blank) input will not produce a null String.

null.Int

Nullable int64.

Marshals to JSON null if SQL source data is null. Zero input will not produce a null Int.

null.Float

Nullable float64.

Marshals to JSON null if SQL source data is null. Zero input will not produce a null Float.

null.Bool

Nullable bool.

Marshals to JSON null if SQL source data is null. False input will not produce a null Bool.

null.Time

Marshals to JSON null if SQL source data is null. Zero input will not produce a null Time.

zero package

import "gopkg.in/guregu/null.v4/zero"

zero.String

Nullable string.

Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent.

zero.Int

Nullable int64.

Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent.

zero.Float

Nullable float64.

Will marshal to 0.0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent.

zero.Bool

Nullable bool.

Will marshal to false if null. false produces a null Float. Null values and zero values are considered equivalent.

zero.Time

Will marshal to the zero time if null. Uses time.Time's marshaler.

Can you add support for other types?

This package is intentionally limited in scope. It will only support the types that driver.Value supports. Feel free to fork this and add more types if you want.

Can you add a feature that ____?

This package isn't intended to be a catch-all data-wrangling package. It is essentially finished. If you have an idea for a new feature, feel free to open an issue to talk about it or fork this package, but don't expect this to do everything.

Package history

As of v4, unmarshaling from JSON sql.NullXXX JSON objects (ex. {"Int64": 123, "Valid": true}) is no longer supported. It's unlikely many people used this, but if you need it, use v3.

Bugs

json's ",omitempty" struct tag does not work correctly right now. It will never omit a null or empty String. This might be fixed eventually.

License

BSD

More Repositories

1

dynamo

expressive DynamoDB library for Go
Go
1,233
star
2

kami

web "framework" with context
Go
289
star
3

intertube

music storage locker
Go
139
star
4

php

Prolog Home Page
HTML
62
star
5

mogi

fancy sql stubs
Go
60
star
6

db

shove database connections in your context (don't do this)
Go
37
star
7

trealla-js

Trealla Prolog for the web
TypeScript
32
star
8

worker-prolog

serverless Prolog for Cloudflare Workers
TypeScript
12
star
9

hakobiya

real-time helper
Go
12
star
10

bbs

generic message board protocol
Go
12
star
11

buildver

when 3 versions just isn't enough!!
Go
11
star
12

rediscache

little library for caching things in redis (via go-redis)
Go
10
star
13

predicates

various predicates for ichiban/prolog
Go
9
star
14

toki

SQL TIME type package
Go
8
star
15

roguetactics

textual tactics game
Go
8
star
16

pengine

pengines (SWI Prolog) client for Go
Go
7
star
17

kizuna.js

crazy global two-way data flow thing for react
JavaScript
6
star
18

hammertime

alternative WASI for wasmtime-go
Go
5
star
19

tesuto

HTTP testing glue
Go
4
star
20

baka

stupidly simple centralized peer list for groupcache
Go
3
star
21

i18n-example

html/template & go-i18n example
Go
3
star
22

into

coercion convenience for Go
Go
3
star
23

relay

various gateways implementing the bbs protocol
Go
3
star
24

trealla-spin

Trealla Prolog templates for Spin
Prolog
3
star
25

bq

BigQuery thing
Go
2
star
26

magic

go black magic
Go
2
star
27

limbo

message board software supporting the bbs protocol
Go
2
star
28

socket.js

old-fashioned ES5 WebSocket wrapper for the un-modern web
JavaScript
1
star
29

gredlib

tiny helper for embedding the gred redis server
Go
1
star
30

bbs-client

javascript bbs client
JavaScript
1
star
31

imbue

stores & retrieves files in the transparency of images
Java
1
star
32

localcache

local cache for DynamoDB
Go
1
star