null

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: MIT Imports: 0 Imported by: 0

README

Description

Package provides generic nullable types.

Example

Base type

type (
	//Nullable is defined in order to implement custom JSON marshaling and unmarshaling and database sql compatability
	Nullable[T any] struct {
		null.Type[T]
	}
)

Implementing json.Marshaler and json.Unmarshaler

func (t *Nullable[T]) MarshalJSON() ([]byte, error) {
    if t.IsNull() {
        return []byte("null"), nil
    }

    vPtr := t.RawValuePtr()

    switch v := any(vPtr).(type) {
    case *time.Time: //Custom time.Time marshaling
        return []byte(v.Format("\"2006-01-02\"")), nil
    default:
        return json.Marshal(vPtr)
    }
}

func (t *Nullable[T]) UnmarshalJSON(bytes []byte) error {
    if string(bytes) == "null" {
        t.SetNull()
        return nil
    }

    switch ptr := any(t.RawValuePtr()).(type) {
	case *time.Time: //Custom time.Time unmarshaling
        ts, err := time.Parse("\"2006-01-02\"", string(bytes))
        if err != nil {
            return err
        }

        t.SetValue(t.DefaultValue())
        *ptr = ts
    default:
        var v T
        err := json.Unmarshal(bytes, &v)
        if err != nil {
            return err
        }

        t.SetValue(v)
    }

    return nil
}

You can implement sql.Scanner and driver.Valuer for compatability with sql/database so provided nullable types can be used for either scanning as destinations, or performing query with parameters. Example:

// Value Implements driver.Valuer
func (t *Nullable[T]) Value() (driver.Value, error) {
    if t.IsNull() {
        return nil, nil
    }

    return t.RawValue(), nil
}

// Scan implements sql.Scanner
func (t *Nullable[T]) Scan(src any) error {
    switch src.(type) {
    case nil:
        t.SetNull()
    default:
        v, ok := src.(T)
        if !ok {
            return errors.New("unsupported")
        }
        t.SetValue(v)
    }
    return nil
}
// ...
var nullableInt64 Nullable[int64]
// ...
_ = rows.Scan(&nullableInt64)
// ...
_, _ = conn.Exec(context.Background(), 'DELETE FROM table WHERE id = $1', &nullableInt64)
// ...

Compatible with pgx.
Keep in mind that only int64, float64, bool, []byte, string, time.Time can be used for such purposes.

Documentation

Overview

Package null provides generic nullable types.

Provided types are compatible with encoding/json (json.Marshaler and json.Unmarshaler) and sql/database (sql.Scanner and driver.Valuer)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Type

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

Type represents a nullable type. Default value is null.

func Value added in v1.2.1

func Value[T any](value T) Type[T]

Value returns not null value of T.

func ValueFromPtr added in v1.2.1

func ValueFromPtr[T any](valuePtr *T) Type[T]

ValueFromPtr returns null Type if valuePtr is nil, Type with actual value otherwise.

func (*Type[T]) CheckedValue added in v1.2.0

func (s *Type[T]) CheckedValue() (T, bool)

CheckedValue returns actual value and true if Type is not null, default value of T and false otherwise.

func (*Type[T]) CheckedValuePtr added in v1.2.0

func (s *Type[T]) CheckedValuePtr() (*T, bool)

CheckedValuePtr returns pointer to actual value and true if Type is not null, false otherwise

func (*Type[T]) DefaultValue added in v1.2.0

func (s *Type[T]) DefaultValue() T

DefaultValue returns default value of T

func (*Type[T]) IsNull

func (s *Type[T]) IsNull() bool

IsNull returns true if value is null.

func (*Type[T]) RawValue

func (s *Type[T]) RawValue() T

RawValue returns actual value if Type is not null, default value of T otherwise.

func (*Type[T]) RawValuePtr added in v1.2.0

func (s *Type[T]) RawValuePtr() *T

RawValuePtr returns pointer to actual value. Is useful only when Type is not null

func (*Type[T]) SetNull

func (s *Type[T]) SetNull()

SetNull sets Type to null.

func (*Type[T]) SetValue

func (s *Type[T]) SetValue(v T)

SetValue sets Type to not null value v.

func (*Type[T]) SetValueFromPtr added in v1.2.0

func (s *Type[T]) SetValueFromPtr(valuePtr *T)

SetValueFromPtr sets Type to not null value if valuePtr is not nil, null value otherwise.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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