Categorygithub.com/mwieser/nullable
modulepackage
1.2.0
Repository: https://github.com/mwieser/nullable.git
Documentation: pkg.go.dev

# README

Build Status GoDoc Go Report Card codecov

nullable

Provides ability to determine if a json key has been set to null or not provided. Inspired by How to determine if a JSON key has been set to null or not provided article by Jon Calhoun

Install

To get the package, execute:

go get github.com/mwieser/nullable

To import this package, add the following line to your code:

import "github.com/mwieser/nullable"

Refer to it as nullable.

For more details, see the API documentation.

Example

import "github.com/mwieser/nullable"

func main() {
	usr := struct {
		Name nullable.String `json:"name"`
	}{}

	data := []byte("{}")
	if err := json.Unmarshal(data, &usr); err != nil {
		log.Fatalf("unmarshaling failed: %s\n", err)
	}

	fmt.Println(usr.Name.Present) // false
	fmt.Println(usr.Name.Valid)   // false
	fmt.Println(usr.Name.Value)   // ""

	data = []byte("{\"name\":null}")
	if err := json.Unmarshal(data, &usr); err != nil {
		log.Fatalf("unmarshaling failed: %s\n", err)
	}

	fmt.Println(usr.Name.Present) // true
	fmt.Println(usr.Name.Valid)   // false
	fmt.Println(usr.Name.Value)   // ""

	data = []byte("{\"name\":\"John\"}")
	if err := json.Unmarshal(data, &usr); err != nil {
		log.Fatalf("unmarshaling failed: %s\n", err)
	}

	fmt.Println(usr.Name.Present) // true
	fmt.Println(usr.Name.Valid)   // true
	fmt.Println(usr.Name.Value)   // "John"
}

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

# Packages

No description provided by the author

# Structs

Bool represents a bool that may be null or not present in json at all.
BoolSlice represents a []bool that may be null or not present in json at all.
Float32 represents a float32 that may be null or not present in json at all.
Float32Slice represents a []float32 that may be null or not present in json at all.
Float64 represents a float64 that may be null or not present in json at all.
Float64Slice represents a []float64 that may be null or not present in json at all.
Int represents a int that may be null or not present in json at all.
Int16 represents a int16 that may be null or not present in json at all.
Int16Slice represents a []int16 that may be null or not present in json at all.
Int32 represents a int32 that may be null or not present in json at all.
Int32Slice represents a []int32 that may be null or not present in json at all.
Int64 represents a int64 that may be null or not present in json at all.
Int64Slice represents a []int64 that may be null or not present in json at all.
IntSlice represents a []int that may be null or not present in json at all.
String represents a string that may be null or not present in json at all.
StringSlice represents a []string that may be null or not present in json at all.
Time represents a time that may be null or not present in json at all.