nilable

package module
v0.0.0-...-f242bb6 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 3 Imported by: 10

README

nilable

nilable is a tiny Go generics library for making non-nilable values nilable, and supports JSON serialization.

You too can stop using pointers when you don't need them!

From value to JSON

nilableString := nilable.From("hello world")
println(nilableString.Set) // true
println(nilableString.Item) // "hello world"
data, _ := json.Marshal(nilableString)
println(string(data)) // `"hello world"` 

From null JSON to value

var nilableString Nilable[string]
json.Unmarshal([]byte("null"), &nilJson)
println(nilJson.Set) // false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Nilable

type Nilable[T any] struct {
	Item T
	Set  bool
}

func From

func From[T any](item T) Nilable[T]

From converts from non-pointer types to Nilable.

func FromPtr

func FromPtr[T any](item *T) Nilable[T]

FromPtr converts from pointer types to Nilable.

func (Nilable[T]) MarshalJSON

func (n Nilable[T]) MarshalJSON() ([]byte, error)

func (*Nilable[T]) UnmarshalJSON

func (n *Nilable[T]) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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