Skip to content

Provides Go types that can distinguish between null and omitted when unmarshalled from JSON.

License

Notifications You must be signed in to change notification settings

nicheinc/nullable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nup

Build Status Go Report Card Godoc license

This package provides types representing updates to struct fields, distinguishing between no-ops, removals, and modifications when marshalling those updates to/from JSON.

See godoc for usage and examples.

Motivation

It's often useful to define data updates using JSON objects, where each key-value pair represents an update to a field, using a value of null to indicate deletion. If a certain key is not present, the corresponding field is not modified. We want to define go structs corresponding to these updates, which need to be marshalled to/from JSON.

If we were to use pointer fields with the omitempty JSON struct tag option for these structs, then fields explicitly set to nil to be removed would instead simply be absent from the marshalled JSON, i.e. unchanged. If we were to use pointer fields without omitempty, then nil fields would be present and null in the JSON output, i.e. removed.

The Update and SliceUpdate types distinguish between no-op and removal updates, allowing them to correctly and seamlessly unmarshal themselves from JSON.

Installation

This package can be imported into a module-aware Go project as follows:

go get github.com/nicheinc/nullable/v2

Contributing

See CONTRIBUTING.md for details on contributing to the nup package.