nullable

package module
v1.0.0-...-96f638d Latest Latest
Warning

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

Go to latest
Published: May 4, 2018 License: MIT Imports: 2 Imported by: 1

README

GoDoc Build Status Go Report Card

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

Example

	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"

Documentation

Overview

Package nullable provides types what allows to determine if a JSON key has been set to null or not provided.

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("name present", usr.Name.Present) // false
fmt.Println("name valid", usr.Name.Valid)     // false
fmt.Println("name value", usr.Name.Value)     // ""

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

fmt.Println("name present", usr.Name.Present) // true
fmt.Println("name valid", usr.Name.Valid)     // false
fmt.Println("name value", usr.Name.Value)     // ""

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

fmt.Println("name present", usr.Name.Present) // true
fmt.Println("name valid", usr.Name.Valid)     // true
fmt.Println("name value", usr.Name.Value)     // "John"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	Present bool // Present is true if key is present in json
	Valid   bool // Valid is true if value is not null and valid bool
	Value   bool
}

Bool represents a bool that may be null or not present in json at all.

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

type Float

type Float struct {
	Present bool // Present is true if key is present in json
	Valid   bool // Valid is true if value is not null and valid float
	Value   float64
}

Float represents a float that may be null or not present in json at all.

func (*Float) UnmarshalJSON

func (f *Float) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

type FloatSlice

type FloatSlice struct {
	Present bool // Present is true if key is present in json
	Valid   bool // Valid is true if value is not null and valid []float64
	Value   []float64
}

FloatSlice represents a float slice that may be null or not present in json at all.

func (*FloatSlice) UnmarshalJSON

func (f *FloatSlice) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

type Int

type Int struct {
	Present bool // Present is true if key is present in json
	Valid   bool // Valid is true if value is not null and valid int64
	Value   int64
}

Int represents an int that may be null or not present in json at all.

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

type IntSlice

type IntSlice struct {
	Present bool // Present is true if key is present in json
	Valid   bool // Valid is true if value is not null and valid []int64
	Value   []int64
}

IntSlice represents an int slice that may be null or not present in json at all.

func (*IntSlice) UnmarshalJSON

func (i *IntSlice) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

type String

type String struct {
	Present bool // Present is true if key is present in json
	Valid   bool // Valid is true if value is not null and valid string
	Value   string
}

String represents a string that may be null or not present in json at all.

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

type StringSlice

type StringSlice struct {
	Present bool // Present is true if key is present in json
	Valid   bool // Valid is true if value is not null
	Value   []string
}

StringSlice represents a []string that may be null or not present in json at all.

func (*StringSlice) UnmarshalJSON

func (s *StringSlice) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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