nullable

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MPL-2.0 Imports: 7 Imported by: 2

README

Nullable

Golang nullable package powered by generics

Install

go get github.com/rlshukhov/nullable

Example

package main

import (
	"fmt"
	"github.com/rlshukhov/nullable"
	"gopkg.in/yaml.v3"
)

type name struct {
	Name nullable.Nullable[string] `yaml:"name"`
	Age  nullable.Nullable[uint64] `yaml:"age"`
}

func main() {
	var nilValue *string
	value := "Alice"

	data := []name{
		{nullable.FromValue("Bob"), nullable.Null[uint64]()},
		{nullable.FromValue(""), nullable.FromValue[uint64](21)},
		{nullable.FromPointer(nilValue), nullable.FromValue[uint64](34)},
		{nullable.FromPointer(&value), nullable.FromValue[uint64](45)},
	}

	out, err := yaml.Marshal(&data)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(out))

	var parsedData []name
	err = yaml.Unmarshal(out, &parsedData)

	for _, d := range parsedData {
		fmt.Printf("Name:\t%s\tIsNull:\t%t\tHasValue:\t%t\n", d.Name.GetValue(), d.Name.IsNull(), d.Name.HasValue())
		fmt.Printf("Age:\t%d\tIsNull:\t%t\tHasValue:\t%t\n\n", d.Age.GetValue(), d.Age.IsNull(), d.Age.HasValue())
	}
}
rlshukhov@MacBook-Pro-Lane main % go run main.go
- name: Bob
  age: null
- name: ""
  age: 21
- name: null
  age: 34
- name: Alice
  age: 45

Name:   Bob     IsNull: false   HasValue:       true
Age:    0       IsNull: true    HasValue:       false

Name:           IsNull: false   HasValue:       true
Age:    21      IsNull: false   HasValue:       true

Name:           IsNull: true    HasValue:       false
Age:    34      IsNull: false   HasValue:       true

Name:   Alice   IsNull: false   HasValue:       true
Age:    45      IsNull: false   HasValue:       true

Documentation

Overview

Package nullable provides a generic Nullable type for handling nullable values. This package supports serialization to JSON and YAML, as well as integration with databases through the sql.Scanner and driver.Valuer interfaces.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedConversion = errors.New("unsupported type conversion")
)

ErrUnsupportedConversion occurs when attempting to convert a value to an unsupported type.

Functions

This section is empty.

Types

type Nullable

type Nullable[T any] struct {
	// contains filtered or unexported fields
}

Nullable represents a nullable value of any type T. The value field holds the actual value of type T. valid indicates whether the value is set (true) or is null (false).

func FromPointer

func FromPointer[T any](value *T) Nullable[T]

FromPointer creates a Nullable from a pointer. If the pointer is nil, valid is set to false.

func FromValue

func FromValue[T any](value T) Nullable[T]

FromValue creates a Nullable with the given value and sets valid to true.

func Null

func Null[T any]() Nullable[T]

Null creates a new Nullable without a value (valid = false).

func (Nullable[T]) GetValue

func (n Nullable[T]) GetValue() T

GetValue returns the actual value T.

func (Nullable[T]) HasValue

func (n Nullable[T]) HasValue() bool

HasValue checks if the value is not null (valid = true).

func (Nullable[T]) IsNull

func (n Nullable[T]) IsNull() bool

IsNull checks if the value is null (valid = false).

func (Nullable[T]) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface for Nullable.

func (Nullable[T]) MarshalYAML

func (n Nullable[T]) MarshalYAML() (any, error)

MarshalYAML implements the marshaling of YAML data.

func (Nullable[T]) OrElse

func (n Nullable[T]) OrElse(defaultVal T) T

OrElse returns the value if valid is true; otherwise, it returns the provided defaultVal.

func (*Nullable[T]) Scan

func (n *Nullable[T]) Scan(value any) error

Scan implements the sql.Scanner interface for Nullable, allowing it to be used in database operations.

func (*Nullable[T]) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface for Nullable.

func (*Nullable[T]) UnmarshalYAML

func (n *Nullable[T]) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML implements the unmarshaling of YAML data.

func (Nullable[T]) Value

func (n Nullable[T]) Value() (driver.Value, error)

Value implements the driver.Valuer interface for Nullable, allowing it to be used in database operations.

Jump to

Keyboard shortcuts

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