nullable

package module
v0.0.0-...-16dac42 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 4 Imported by: 0

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

Install

To get the package, execute:

go get gopkg.in/romanyx/nullable.v1

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

import "gopkg.in/romanyx/nullable.v1"

Refer to it as nullable.

For more details, see the API documentation.

Example

import "gopkg.in/romanyx/nullable.v1"

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!

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 NewBool

func NewBool(v bool) *Bool

func NewBoolNull

func NewBoolNull() *Bool

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Bool) Validate

func (b *Bool) Validate(formats strfmt.Registry) error

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 NewFloat

func NewFloat(v float64) *Float

func NewFloatNull

func NewFloatNull() *Float

func (Float) MarshalJSON

func (f Float) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Float) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Float) Validate

func (f *Float) Validate(formats strfmt.Registry) error

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 NewInt

func NewInt(v int64) *Int

func NewIntNull

func NewIntNull() *Int

func (Int) MarshalJSON

func (i Int) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Int) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Int) Validate

func (i *Int) Validate(formats strfmt.Registry) error

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 NewString

func NewString(v string) *String

func NewStringNull

func NewStringNull() *String

func (String) MarshalJSON

func (i String) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*String) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*String) Validate

func (s *String) Validate(formats strfmt.Registry) error

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