Documentation
¶
Overview ¶
Package nullable is specifically designed for json marshalling and unmarshalling but not limited to, where zero values have business logic meaning.
Example (NullableNestedStruct) ¶
var card nullable.Any[Card] if card.Null() { fmt.Println("card is null after var declaration") } if card.Value().Person.Null() { fmt.Println("card.Person is null after card var declaration") } card = nullable.Of(Card{}) if card.Null() { fmt.Println("card is null after zero value declaration") } if card.Value().Person.Null() { fmt.Println("card.Person is null after card zero value declaration") } card = nullable.Of(Card{ ID: "card-id", Person: nullable.Of(Person{ Name: "John Doe", Age: 18, }), }) if card.Null() { fmt.Println("card is null after value declaration") } if card.Value().Person.Null() { fmt.Println("card.Person is null after card value declaration") } fmt.Printf(`card value is "%s"`, card)
Output: card is null after var declaration card.Person is null after card var declaration card.Person is null after card zero value declaration card value is "{card-id {John Doe 18}}"
Example (NullableString) ¶
var greet nullable.Any[string] if greet.Null() { fmt.Println("greet is null after var declaration") } greet = nullable.Of("") if greet.Null() { fmt.Println("greet is null after zero value assignment") } greet = nullable.Of("Hello, World!") if greet.Null() { fmt.Println("greet is null after value assignment") } fmt.Printf(`greet value is "%s"`, greet)
Output: greet is null after var declaration greet value is "Hello, World!"
Example (NullableStruct) ¶
var person nullable.Any[Person] if person.Null() { fmt.Println("person is null after var declaration") } person = nullable.Of(Person{}) if person.Null() { fmt.Println("person is null after zero value assignment") } person = nullable.Of(Person{ Name: "John Doe", Age: 18, }) if person.Null() { fmt.Println("person is null after value assignment") } fmt.Printf(`person value is "%s"`, person)
Output: person is null after var declaration person value is "{John Doe 18}"
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Any ¶ added in v0.2.0
type Any[T any] struct { // contains filtered or unexported fields }
Any holds a single value of type T BUG(golang) Golang currently (if ever?) has no type constraint that indicates that a type is json (un)marshalling compatible. If you use a json incompatible type like complex64, the marshal and unmarshal functions will always fail. BUG(steelshot) Currently the Any type does not implement text/binary (un)marshalling. YAML, TOML and other's (un)marshalling will not work, for the time being, You should implement your own types that embed Any with custom (un)marshalling.
func (Any[T]) MarshalJSON ¶ added in v0.2.0
MarshalJSON implements json.Marshaler
func (*Any[T]) UnmarshalJSON ¶ added in v0.2.0
UnmarshalJSON implements json.Unmarshaler
Notes ¶
Bugs ¶
Golang currently (if ever?) has no type constraint that indicates that a type is json (un)marshalling compatible. If you use a json incompatible type like complex64, the marshal and unmarshal functions will always fail.
Currently the Any type does not implement text/binary (un)marshalling. YAML, TOML and other's (un)marshalling will not work, for the time being, You should implement your own types that embed Any with custom (un)marshalling.