Giter VIP home page Giter VIP logo

null-1's Introduction

null int and string values

This module provides (yet another) alternative in dealing with integers and strings which may be null in your JSON or database. There are various different approaches to this, namely the built in golang SQL types and the guregu null module which adds in JSON support. These are fine approaches but both suffer from you having to deal with a struct type for your ids instead of a more natural int64 or string. That is fine in some cases but I prefer to use more primitive types as assignment is more straightforward, simple equality works, etc.

Sadly this requires a bit of boilerplate, so this package tries to make that a bit easier, proving helper methods for marshalling to/from json and reading / scanning from a database value. Due to the Golang type system, using these requires some boilerplate, but I find it nicer to have a bit more code for my ID types and less awkward code around the usage of ids rather than vice versa.

To define your own ID type which is nullable when written:

import "github.com/nyaruka/null"

type CustomID null.Int

const NullCustomID = CustomID(0)

// MarshalJSON marshals into JSON. 0 values will become null
func (i CustomID) MarshalJSON() ([]byte, error) {
	return null.Int(i).MarshalJSON()
}

// UnmarshalJSON unmarshals from JSON. null values become 0
func (i *CustomID) UnmarshalJSON(b []byte) error {
	return null.UnmarshalInt(b, (*null.Int)(i))
}

// Value returns the db value, null is returned for 0
func (i CustomID) Value() (driver.Value, error) {
	return null.Int(i).Value()
}

// Scan scans from the db value. null values become 0
func (i *CustomID) Scan(value interface{}) error {
	return null.ScanInt(value, (*null.Int)(i))
}

The process is essentially the same for nullable strings:

import "github.com/nyaruka/null"

type CustomString null.String

type NullCustomString = CustomString("")

// MarshalJSON marshals into JSON. "" values will become null
func (s CustomString) MarshalJSON() ([]byte, error) {
	return null.String(s).MarshalJSON()
}

// UnmarshalJSON unmarshals from JSON. null values become ""
func (s *CustomString) UnmarshalJSON(b []byte) error {
	return null.UnmarshalString(b, (*null.String)(s))
}

// Value returns the db value, null is returned for ""
func (s CustomString) Value() (driver.Value, error) {
	return null.String(s).Value()
}

// Scan scans from the db value. null values become ""
func (s *CustomString) Scan(value interface{}) error {
	return null.ScanString(value, (*null.String)(s))
}

If you don't care about type safety within your types, you can use a go alias which simplifies things. This is more useful for "stringy" things than it is for ids where you likely want to enforce type:

import "github.com/nyaruka/null"

// Note you lose type safety with aliases. FooID can be assigned to BarID and vice versa!
type FooID = null.Int
type BarID = null.Int

// Same here, any null.String can be assigned to CustomString
type CustomString = null.String

null-1's People

Contributors

nicpottier avatar

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.