Help us shape the future of Go! Take the 2026 Go Developer Survey

nullable

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: MIT Imports: 6 Imported by: 0

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/allaboutapps/nullable

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

import "github.com/allaboutapps/nullable"

Refer to it as nullable.

For more details, see the API documentation.

Example

import "github.com/allaboutapps/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"
}

go-swagger usage example

definitions:
  NullableInt:
    type: integer
    example: 1234
    x-go-type:
      import:
        package: github.com/allaboutapps/nullable
      type: Int
  NullableInt16:
    type: integer
    example: 1234
    x-go-type:
      import:
        package: github.com/allaboutapps/nullable
      type: Int16
  NullableFloat:
    type: number
    format: float
    example: 1.5
    x-go-type:
      import:
        package: github.com/allaboutapps/nullable
      type: Float32
  NullableFloat64:
    type: number
    format: float
    example: 1.5
    x-go-type:
      import:
        package: github.com/allaboutapps/nullable
      type: Float64
  NullableString:
    type: string
    example: example
    x-go-type:
      import:
        package: github.com/allaboutapps/nullable
      type: String

Contributing

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

Documentation

Overview

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

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"

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

Code generated by go generate; DO NOT EDIT. This file was generated by scripts/models/gen_nullable_types.go

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) ContextValidate added in v1.3.0

func (b *Bool) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Bool) Ptr

func (b *Bool) Ptr() *bool

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type BoolSlice

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

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

func (*BoolSlice) ContextValidate added in v1.3.0

func (b *BoolSlice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*BoolSlice) Ptr added in v1.3.0

func (b *BoolSlice) Ptr() *[]bool

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*BoolSlice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*BoolSlice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Float32

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

Float32 represents a float32 that may be null or not present in json at all.

func (*Float32) ContextValidate added in v1.3.0

func (f *Float32) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Float32) Ptr

func (f *Float32) Ptr() *float32

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Float32) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Float32) Validate

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Float32Slice

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

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

func (*Float32Slice) ContextValidate added in v1.3.0

func (f *Float32Slice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Float32Slice) Ptr added in v1.3.0

func (f *Float32Slice) Ptr() *[]float32

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Float32Slice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Float32Slice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Float64

type Float64 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
}

Float64 represents a float64 that may be null or not present in json at all.

func (*Float64) ContextValidate added in v1.3.0

func (f *Float64) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Float64) Ptr

func (f *Float64) Ptr() *float64

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Float64) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Float64) Validate

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Float64Slice

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

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

func (*Float64Slice) ContextValidate added in v1.3.0

func (f *Float64Slice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Float64Slice) Ptr added in v1.3.0

func (f *Float64Slice) Ptr() *[]float64

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Float64Slice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Float64Slice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

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 int
	Value   int
}

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

func (*Int) ContextValidate added in v1.3.0

func (i *Int) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Int) Ptr

func (i *Int) Ptr() *int

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Int16

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

Int16 represents a int16 that may be null or not present in json at all.

func (*Int16) ContextValidate added in v1.3.0

func (i *Int16) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Int16) Ptr

func (i *Int16) Ptr() *int16

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Int16) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Int16) Validate

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Int16Slice

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

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

func (*Int16Slice) ContextValidate added in v1.3.0

func (i *Int16Slice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Int16Slice) Ptr added in v1.3.0

func (i *Int16Slice) Ptr() *[]int16

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Int16Slice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Int16Slice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Int32

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

Int32 represents a int32 that may be null or not present in json at all.

func (*Int32) ContextValidate added in v1.3.0

func (i *Int32) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Int32) Ptr

func (i *Int32) Ptr() *int32

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Int32) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Int32) Validate

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Int32Slice

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

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

func (*Int32Slice) ContextValidate added in v1.3.0

func (i *Int32Slice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Int32Slice) Ptr added in v1.3.0

func (i *Int32Slice) Ptr() *[]int32

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Int32Slice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Int32Slice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Int64

type Int64 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
}

Int64 represents a int64 that may be null or not present in json at all.

func (*Int64) ContextValidate added in v1.3.0

func (i *Int64) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Int64) Ptr

func (i *Int64) Ptr() *int64

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Int64) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Int64) Validate

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Int64Slice

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

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

func (*Int64Slice) ContextValidate added in v1.3.0

func (i *Int64Slice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*Int64Slice) Ptr added in v1.3.0

func (i *Int64Slice) Ptr() *[]int64

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Int64Slice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*Int64Slice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

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
	Value   []int
}

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

func (*IntSlice) ContextValidate added in v1.3.0

func (i *IntSlice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*IntSlice) Ptr added in v1.3.0

func (i *IntSlice) Ptr() *[]int

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*IntSlice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*IntSlice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

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) ContextValidate added in v1.3.0

func (s *String) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*String) Ptr

func (s *String) Ptr() *string

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

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) ContextValidate added in v1.3.0

func (s *StringSlice) ContextValidate(ctx context.Context, formats strfmt.Registry) error

ContextValidate implements runtime.ContextValidatable from github.com/go-openapi/runtime

func (*StringSlice) Ptr added in v1.3.0

func (s *StringSlice) Ptr() *[]string

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*StringSlice) UnmarshalJSON

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

UnmarshalJSON implements json.Marshaler interface.

func (*StringSlice) Validate added in v1.3.0

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

Validate implements runtime.Validateable interface from github.com/go-openapi/runtime

type Time

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

Time represents a time that may be null or not present in json at all.

func (*Time) Ptr

func (t *Time) Ptr() *time.Time

Returns nil if not present or valid. Otherwise it will return a pointer to the value.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Marshaler interface.

func (*Time) Validate

func (t *Time) Validate(formats strfmt.Registry) error

Validate implements runtime.Validateable interface for go-swagger generation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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