optional

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

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 10 Imported by: 0

README

optional

Go Reference Go Report Card

A production-ready three-state generic optional for Go that distinguishes between:

State Meaning JSON behaviour SQL behaviour
Present Field contains a value marshals the value sends the value
Null Field was explicitly set to null marshals as null sends NULL
Missing Field was not provided at all omitted with omitzero returns an error

This is essential for JSON PATCH semantics, nullable SQL columns, and any API where you need to know whether a client explicitly set a field to null, provided a value, or omitted it entirely.

Installation

go get github.com/eremin-daniil/optional

Requires Go 1.24+ (for omitzero JSON tag support).

Quick Start

package main

import (
    "encoding/json"
    "fmt"

    "github.com/eremin-daniil/optional"
)

type UpdateUserRequest struct {
    Name  optional.String `json:"name,omitzero"`
    Email optional.String `json:"email,omitzero"`
    Age   optional.Int    `json:"age,omitzero"`
}

func main() {
    input := `{"name": "Alice", "email": null}`

    var req UpdateUserRequest
    if err := json.Unmarshal([]byte(input), &req); err != nil {
        panic(err)
    }

    fmt.Println(req.Name.IsPresent()) // true  → client sent "Alice"
    fmt.Println(req.Email.IsNull())   // true  → client explicitly set null
    fmt.Println(req.Age.IsMissing())  // true  → client didn't send age at all
}

API Overview

Constructors
// Generic constructors (for any type T)
present := optional.Of(42)
fromPtr := optional.FromPtr(&value)
nullable := optional.OfNullable(&value) // alias for FromPtr
nullValue := optional.Null[int]()
missingValue := optional.Missing[int]()

// Typed constructors (for SQL-compatible scalar types)
b := optional.OfBool(true)
i := optional.OfInt(42)
s := optional.OfString("hello")
u := optional.OfUUID(uuid.New())
d := optional.OfDecimal(decimal.NewFromFloat(99.99))
t := optional.OfTime(time.Now())

// From pointers (nil → null)
nullableBool := optional.OfNullableBool(boolPtr)   // alias: FromBoolPtr
nullableInt := optional.OfNullableInt(intPtr)      // alias: FromIntPtr
nullableString := optional.OfNullableString(strPtr) // alias: FromStringPtr
Accessors
v, ok := f.Get()        // value + presence check
fallbackValue := f.GetOr(fallback)
mustValue := f.MustGet() // value or panic
ptr := f.Ptr()           // *T or nil
resolved := f.Or(other)  // self if present, otherwise other
lazyValue := f.OrElse(func() T { return compute() })
State Checks
f.IsPresent() // has a value
f.IsNull()    // explicitly null
f.IsMissing() // not provided
f.IsZero()    // same as IsMissing (supports omitzero)
Formatting
fmt.Println(f)         // "42", "null", or "missing"
fmt.Printf("%#v", f)   // "optional.Of(42)", "optional.Null()", etc.

JSON Integration

Field[T] implements json.Marshaler and json.Unmarshaler:

type Request struct {
    Name optional.Field[string] `json:"name,omitzero"`
    Age  optional.Field[int]    `json:"age,omitzero"`
}
JSON input Name state Age state
{"name":"Alice","age":30} present present
{"name":null,"age":30} null present
{"age":30} missing present
{} missing missing

The omitzero tag (Go 1.24+) ensures missing fields are omitted during marshalling.

SQL Integration

All scalar types implement sql.Scanner and driver.Valuer for seamless database usage with any SQL driver (PostgreSQL, MySQL, SQLite, etc.):

type User struct {
    ID    int
    Name  optional.String
    Email optional.String
    Age   optional.Int
}

// Reading from database
var user User
row := db.QueryRow("SELECT id, name, email, age FROM users WHERE id = $1", 1)
err := row.Scan(&user.ID, &user.Name, &user.Email, &user.Age)

// Writing to database
_, err = db.Exec(
    "INSERT INTO users (name, email, age) VALUES ($1, $2, $3)",
    user.Name, user.Email, user.Age,
)
Scan Type Conversion

Scan methods handle automatic type conversion from standard SQL driver types:

Target type Accepted SQL driver types
Bool bool, int64, float64, string, []byte
Int* int64, float64, string, []byte (with overflow checking)
Uint* int64, float64, string, []byte (with overflow and sign checking)
Float* float64, int64, string, []byte
String string, []byte
Bytes []byte (copied), string
Time time.Time, string, []byte (multiple formats)
UUID string, []byte (string format or raw 16 bytes)
Decimal string, []byte, float64, int64
Value Type Mapping

Value methods return proper driver.Value types:

Scalar type driver.Value type
Bool bool
Int, Int8Int64 int64
Uint* int64 (overflow error if > MaxInt64)
Float32 float64
Float64 float64
String string
Bytes []byte
Time time.Time
UUID string
Decimal string

Scalar Types

Type Go type External dependency
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Uintptr uintptr
Float32 float32
Float64 float64
String string
Bytes []byte
Time time.Time
UUID uuid.UUID google/uuid
Decimal decimal.Decimal shopspring/decimal

Functional Helpers

// Map transforms the value (standalone function due to Go generics limitations).
result := optional.Map(optional.Of(5), func(n int) string {
    return fmt.Sprintf("value: %d", n)
})
// result = Field[string]{present, "value: 5"}

// FlatMap for chaining optional operations.
result := optional.FlatMap(optional.Of(10), func(n int) optional.Field[float64] {
    if n == 0 {
        return optional.Null[float64]()
    }
    return optional.Of(100.0 / float64(n))
})

// Equal compares two fields (requires comparable types).
optional.Equal(optional.Of(42), optional.Of(42)) // true
optional.Equal(optional.Null[int](), optional.Null[int]()) // true

PATCH Request Example

A complete example of using optional fields for a PATCH endpoint:

type UpdateUserRequest struct {
    Name  optional.String `json:"name,omitzero"`
    Email optional.String `json:"email,omitzero"`
    Age   optional.Int    `json:"age,omitzero"`
}

func handlePatchUser(req UpdateUserRequest, userID int, db *sql.DB) error {
    // Build dynamic UPDATE query based on provided fields.
    var setClauses []string
    var args []any
    argIdx := 1

    if !req.Name.IsMissing() {
        setClauses = append(setClauses, fmt.Sprintf("name = $%d", argIdx))
        args = append(args, req.Name)
        argIdx++
    }
    if !req.Email.IsMissing() {
        setClauses = append(setClauses, fmt.Sprintf("email = $%d", argIdx))
        args = append(args, req.Email)
        argIdx++
    }
    if !req.Age.IsMissing() {
        setClauses = append(setClauses, fmt.Sprintf("age = $%d", argIdx))
        args = append(args, req.Age)
        argIdx++
    }

    if len(setClauses) == 0 {
        return nil // nothing to update
    }

    query := fmt.Sprintf("UPDATE users SET %s WHERE id = $%d",
        strings.Join(setClauses, ", "), argIdx)
    args = append(args, userID)

    _, err := db.Exec(query, args...)
    return err
}

License

MIT License — see LICENSE for details.

Documentation

Overview

Package optional provides a three-state generic container for Go that distinguishes between a present value, an explicit null, and a missing (omitted) field.

This is especially useful for JSON APIs with PATCH semantics and for working with nullable SQL columns, where you need to know whether a field was explicitly set to null, set to a value, or not provided at all.

Three States

Every Field is in exactly one of these states:

  • Present — the field contains a value.
  • Null — the field was explicitly set to null.
  • Missing — the field was not provided (the zero value of Field).

JSON Integration

Field implements encoding/json.Marshaler and encoding/json.Unmarshaler. When unmarshalling JSON:

  • A JSON value → present state.
  • A JSON null → null state.
  • A missing JSON key → missing state (zero value, never touched by unmarshaler).

Use the "omitzero" tag (Go 1.24+) to omit missing fields during marshalling:

type UpdateRequest struct {
    Name optional.String `json:"name,omitzero"`
    Age  optional.Int    `json:"age,omitzero"`
}

SQL Integration

The concrete scalar types (Bool, Int, String, UUID, etc.) implement database/sql.Scanner and database/sql/driver.Valuer for seamless database integration with any SQL driver. Scan handles the standard driver value types (int64, float64, bool, []byte, string, time.Time) with automatic type conversion and range checking.

Scalar Types

The following concrete types are provided for direct use with SQL databases:

Functional Helpers

Standalone generic functions are provided for common transformations:

  • Map — transforms the value inside a Field.
  • FlatMap — monadic bind for chaining optional operations.
  • Equal — compares two fields for equality.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T comparable](a, b Field[T]) bool

Equal reports whether two fields are equal. Two fields are equal if they have the same state and, when present, the same value.

Equal requires the [comparable] constraint because Go methods cannot introduce additional type constraints.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	fmt.Println(optional.Equal(optional.Of(42), optional.Of(42)))           // true
	fmt.Println(optional.Equal(optional.Of(1), optional.Of(2)))             // false
	fmt.Println(optional.Equal(optional.Null[int](), optional.Null[int]())) // true
}
Output:
true
false
true

Types

type Bool

type Bool struct {
	Field[bool]
}

Bool is a three-state optional for bool values.

func FromBoolPtr

func FromBoolPtr(ptr *bool) Bool

FromBoolPtr creates a Bool from a pointer. If ptr is nil, the Bool is null; otherwise it contains the dereferenced value.

func MissingBool

func MissingBool() Bool

MissingBool creates a Bool in the missing state.

func NullBool

func NullBool() Bool

NullBool creates a Bool in the null state.

func OfBool

func OfBool(value bool) Bool

OfBool creates a Bool with the given value in the present state.

func OfNullableBool

func OfNullableBool(ptr *bool) Bool

OfNullableBool creates a Bool from a pointer. It is an alias for FromBoolPtr.

func (*Bool) Scan

func (b *Bool) Scan(src any) error

Scan implements sql.Scanner.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	var b optional.Bool

	// Simulate scanning NULL from database.
	b.Scan(nil)
	fmt.Println(b.IsNull()) // true

	// Simulate scanning a value.
	b.Scan(true)
	fmt.Println(b.MustGet()) // true

	// Some databases return 0/1 for booleans.
	b.Scan(int64(1))
	fmt.Println(b.MustGet()) // true
}
Output:
true
true
true

type Bytes

type Bytes struct {
	Field[[]byte]
}

Bytes is a three-state optional for []byte values (e.g., BLOB, BYTEA columns).

func FromBytesPtr

func FromBytesPtr(ptr *[]byte) Bytes

FromBytesPtr creates a Bytes from a pointer.

func MissingBytes

func MissingBytes() Bytes

MissingBytes creates a Bytes in the missing state.

func NullBytes

func NullBytes() Bytes

NullBytes creates a Bytes in the null state.

func OfBytes

func OfBytes(value []byte) Bytes

OfBytes creates a Bytes with the given value in the present state.

func OfNullableBytes

func OfNullableBytes(ptr *[]byte) Bytes

OfNullableBytes creates a Bytes from a pointer. It is an alias for FromBytesPtr.

func (*Bytes) Scan

func (b *Bytes) Scan(src any) error

Scan implements sql.Scanner.

type Decimal

type Decimal struct {
	Field[decimal.Decimal]
}

Decimal is a three-state optional for decimal.Decimal values.

func FromDecimalPtr

func FromDecimalPtr(ptr *decimal.Decimal) Decimal

FromDecimalPtr creates a Decimal from a pointer.

func MissingDecimal

func MissingDecimal() Decimal

MissingDecimal creates a Decimal in the missing state.

func NullDecimal

func NullDecimal() Decimal

NullDecimal creates a Decimal in the null state.

func OfDecimal

func OfDecimal(value decimal.Decimal) Decimal

OfDecimal creates a Decimal with the given value in the present state.

func OfNullableDecimal

func OfNullableDecimal(ptr *decimal.Decimal) Decimal

OfNullableDecimal creates a Decimal from a pointer. It is an alias for FromDecimalPtr.

func (*Decimal) Scan

func (d *Decimal) Scan(src any) error

Scan implements sql.Scanner. Supports string, []byte, float64, and int64 source types.

func (Decimal) Value

func (d Decimal) Value() (driver.Value, error)

Value implements driver.Valuer, returning the decimal as a string for SQL compatibility.

type Field

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

Field is a generic three-state container that distinguishes between a present value, an explicit null, and a missing (omitted) field. The zero value of Field is the missing state.

func FlatMap

func FlatMap[T, U any](f Field[T], fn func(T) Field[U]) Field[U]

FlatMap transforms the value inside a Field using fn, which itself returns a Field. If the field is present, fn is applied and its result is returned directly. If the field is null or missing, the corresponding state is preserved.

FlatMap is a standalone function because Go does not support additional type parameters on methods.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	safeDivide := func(divisor int) optional.Field[float64] {
		if divisor == 0 {
			return optional.Null[float64]()
		}
		return optional.Of(100.0 / float64(divisor))
	}

	result := optional.FlatMap(optional.Of(4), safeDivide)
	fmt.Println(result.MustGet()) // 25

	zero := optional.FlatMap(optional.Of(0), safeDivide)
	fmt.Println(zero.IsNull()) // true
}
Output:
25
true

func FromPtr

func FromPtr[T any](ptr *T) Field[T]

FromPtr creates a Field from a pointer. If ptr is nil, the Field is null. If ptr is non-nil, the Field contains the dereferenced value.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	v := 42
	present := optional.FromPtr(&v)
	null := optional.FromPtr[int](nil)

	fmt.Println(present.IsPresent()) // true
	fmt.Println(null.IsNull())       // true
}
Output:
true
true

func Map

func Map[T, U any](f Field[T], fn func(T) U) Field[U]

Map transforms the value inside a Field using fn. If the field is present, fn is applied to the value and the result is wrapped in a new present Field. If the field is null or missing, the corresponding state is preserved in the returned Field.

Map is a standalone function because Go does not support additional type parameters on methods.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	f := optional.Of(5)
	doubled := optional.Map(f, func(n int) int { return n * 2 })
	fmt.Println(doubled.MustGet()) // 10

	null := optional.Null[int]()
	result := optional.Map(null, func(n int) int { return n * 2 })
	fmt.Println(result.IsNull()) // true
}
Output:
10
true

func Missing

func Missing[T any]() Field[T]

Missing creates a Field in the missing state. This is equivalent to the zero value of Field[T].

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	f := optional.Missing[int]()
	fmt.Println(f.IsMissing()) // true
	fmt.Println(f.IsZero())    // true (same as missing)
}
Output:
true
true

func Null

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

Null creates a Field in the null state.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	f := optional.Null[string]()
	fmt.Println(f.IsNull())    // true
	fmt.Println(f.IsPresent()) // false
}
Output:
true
false

func Of

func Of[T any](value T) Field[T]

Of creates a Field containing the given value in the present state.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	f := optional.Of(42)
	fmt.Println(f.IsPresent()) // true
	fmt.Println(f.MustGet())   // 42
}
Output:
true
42

func OfNullable

func OfNullable[T any](ptr *T) Field[T]

OfNullable creates a Field from a pointer. It is an alias for FromPtr.

func (Field[T]) Get

func (f Field[T]) Get() (T, bool)

Get returns the value and a boolean indicating whether the value is present.

func (Field[T]) GetOr

func (f Field[T]) GetOr(defaultValue T) T

GetOr returns the value if present, otherwise returns defaultValue.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	present := optional.Of(5)
	null := optional.Null[int]()

	fmt.Println(present.GetOr(99)) // 5
	fmt.Println(null.GetOr(99))    // 99
}
Output:
5
99

func (Field[T]) GoString

func (f Field[T]) GoString() string

GoString returns a Go-syntax representation of the field for use with %#v.

func (Field[T]) IsMissing

func (f Field[T]) IsMissing() bool

IsMissing reports whether the field is in the missing state.

func (Field[T]) IsNull

func (f Field[T]) IsNull() bool

IsNull reports whether the field is in the null state.

func (Field[T]) IsPresent

func (f Field[T]) IsPresent() bool

IsPresent reports whether the field contains a value.

func (Field[T]) IsZero

func (f Field[T]) IsZero() bool

IsZero reports whether the field is in the missing state (zero value of Field). This method supports the omitzero JSON tag introduced in Go 1.24+.

func (Field[T]) MarshalJSON

func (f Field[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. Returns an error for missing values — use the omitzero tag to omit them.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	type Request struct {
		Name optional.Field[string] `json:"name,omitzero"`
		Age  optional.Field[int]    `json:"age,omitzero"`
	}

	// All fields present.
	r1 := Request{Name: optional.Of("Alice"), Age: optional.Of(30)}
	data1, _ := json.Marshal(r1)
	fmt.Println(string(data1))

	// Age is null.
	r2 := Request{Name: optional.Of("Bob"), Age: optional.Null[int]()}
	data2, _ := json.Marshal(r2)
	fmt.Println(string(data2))

	// Age is missing (omitted from JSON).
	r3 := Request{Name: optional.Of("Carol")}
	data3, _ := json.Marshal(r3)
	fmt.Println(string(data3))

}
Output:
{"name":"Alice","age":30}
{"name":"Bob","age":null}
{"name":"Carol"}

func (Field[T]) MustGet

func (f Field[T]) MustGet() T

MustGet returns the value if present, otherwise panics.

func (Field[T]) Or

func (f Field[T]) Or(other Field[T]) Field[T]

Or returns f if it contains a present value, otherwise returns other.

Example
package main

import (
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	a := optional.Null[int]()
	b := optional.Of(42)

	result := a.Or(b)
	fmt.Println(result.MustGet()) // 42
}
Output:
42

func (Field[T]) OrElse

func (f Field[T]) OrElse(fn func() T) T

OrElse returns the value if present, otherwise calls fn and returns its result.

func (Field[T]) Ptr

func (f Field[T]) Ptr() *T

Ptr returns a pointer to the value if present, otherwise nil.

func (Field[T]) String

func (f Field[T]) String() string

String returns a human-readable string representation of the field.

func (*Field[T]) UnmarshalJSON

func (f *Field[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. JSON null sets the field to the null state. Any other valid JSON sets it to present. Fields not present in JSON remain in the missing state (zero value).

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/eremin-daniil/optional"
)

func main() {
	type Request struct {
		Name optional.Field[string] `json:"name"`
		Age  optional.Field[int]    `json:"age"`
	}

	// All fields present.
	var r1 Request
	json.Unmarshal([]byte(`{"name":"Alice","age":30}`), &r1)
	fmt.Printf("name: present=%v, age: present=%v\n", r1.Name.IsPresent(), r1.Age.IsPresent())

	// Age is null.
	var r2 Request
	json.Unmarshal([]byte(`{"name":"Bob","age":null}`), &r2)
	fmt.Printf("name: present=%v, age: null=%v\n", r2.Name.IsPresent(), r2.Age.IsNull())

	// Age is missing.
	var r3 Request
	json.Unmarshal([]byte(`{"name":"Carol"}`), &r3)
	fmt.Printf("name: present=%v, age: missing=%v\n", r3.Name.IsPresent(), r3.Age.IsMissing())

}
Output:
name: present=true, age: present=true
name: present=true, age: null=true
name: present=true, age: missing=true

func (Field[T]) Value

func (f Field[T]) Value() (driver.Value, error)

Value implements driver.Valuer. Returns nil for null, an error for missing, and the raw value for present.

Note: for scalar wrapper types (Int, UUID, Decimal, etc.), the Value method is overridden to return a valid driver.Value type.

type Float32

type Float32 struct {
	Field[float32]
}

Float32 is a three-state optional for float32 values.

func FromFloat32Ptr

func FromFloat32Ptr(ptr *float32) Float32

FromFloat32Ptr creates a Float32 from a pointer.

func MissingFloat32

func MissingFloat32() Float32

MissingFloat32 creates a Float32 in the missing state.

func NullFloat32

func NullFloat32() Float32

NullFloat32 creates a Float32 in the null state.

func OfFloat32

func OfFloat32(value float32) Float32

OfFloat32 creates a Float32 with the given value in the present state.

func OfNullableFloat32

func OfNullableFloat32(ptr *float32) Float32

OfNullableFloat32 creates a Float32 from a pointer. It is an alias for FromFloat32Ptr.

func (*Float32) Scan

func (f *Float32) Scan(src any) error

Scan implements sql.Scanner.

func (Float32) Value

func (f Float32) Value() (driver.Value, error)

Value implements driver.Valuer, returning float64 for SQL compatibility.

type Float64

type Float64 struct {
	Field[float64]
}

Float64 is a three-state optional for float64 values.

func FromFloat64Ptr

func FromFloat64Ptr(ptr *float64) Float64

FromFloat64Ptr creates a Float64 from a pointer.

func MissingFloat64

func MissingFloat64() Float64

MissingFloat64 creates a Float64 in the missing state.

func NullFloat64

func NullFloat64() Float64

NullFloat64 creates a Float64 in the null state.

func OfFloat64

func OfFloat64(value float64) Float64

OfFloat64 creates a Float64 with the given value in the present state.

func OfNullableFloat64

func OfNullableFloat64(ptr *float64) Float64

OfNullableFloat64 creates a Float64 from a pointer. It is an alias for FromFloat64Ptr.

func (*Float64) Scan

func (f *Float64) Scan(src any) error

Scan implements sql.Scanner.

type Int

type Int struct {
	Field[int]
}

Int is a three-state optional for int values.

func FromIntPtr

func FromIntPtr(ptr *int) Int

FromIntPtr creates an Int from a pointer.

func MissingInt

func MissingInt() Int

MissingInt creates an Int in the missing state.

func NullInt

func NullInt() Int

NullInt creates an Int in the null state.

func OfInt

func OfInt(value int) Int

OfInt creates an Int with the given value in the present state.

func OfNullableInt

func OfNullableInt(ptr *int) Int

OfNullableInt creates an Int from a pointer. It is an alias for FromIntPtr.

func (*Int) Scan

func (i *Int) Scan(src any) error

Scan implements sql.Scanner.

func (Int) Value

func (i Int) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Int8

type Int8 struct {
	Field[int8]
}

Int8 is a three-state optional for int8 values.

func FromInt8Ptr

func FromInt8Ptr(ptr *int8) Int8

FromInt8Ptr creates an Int8 from a pointer.

func MissingInt8

func MissingInt8() Int8

MissingInt8 creates an Int8 in the missing state.

func NullInt8

func NullInt8() Int8

NullInt8 creates an Int8 in the null state.

func OfInt8

func OfInt8(value int8) Int8

OfInt8 creates an Int8 with the given value in the present state.

func OfNullableInt8

func OfNullableInt8(ptr *int8) Int8

OfNullableInt8 creates an Int8 from a pointer. It is an alias for FromInt8Ptr.

func (*Int8) Scan

func (i *Int8) Scan(src any) error

Scan implements sql.Scanner.

func (Int8) Value

func (i Int8) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Int16

type Int16 struct {
	Field[int16]
}

Int16 is a three-state optional for int16 values.

func FromInt16Ptr

func FromInt16Ptr(ptr *int16) Int16

FromInt16Ptr creates an Int16 from a pointer.

func MissingInt16

func MissingInt16() Int16

MissingInt16 creates an Int16 in the missing state.

func NullInt16

func NullInt16() Int16

NullInt16 creates an Int16 in the null state.

func OfInt16

func OfInt16(value int16) Int16

OfInt16 creates an Int16 with the given value in the present state.

func OfNullableInt16

func OfNullableInt16(ptr *int16) Int16

OfNullableInt16 creates an Int16 from a pointer. It is an alias for FromInt16Ptr.

func (*Int16) Scan

func (i *Int16) Scan(src any) error

Scan implements sql.Scanner.

func (Int16) Value

func (i Int16) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Int32

type Int32 struct {
	Field[int32]
}

Int32 is a three-state optional for int32 values.

func FromInt32Ptr

func FromInt32Ptr(ptr *int32) Int32

FromInt32Ptr creates an Int32 from a pointer.

func MissingInt32

func MissingInt32() Int32

MissingInt32 creates an Int32 in the missing state.

func NullInt32

func NullInt32() Int32

NullInt32 creates an Int32 in the null state.

func OfInt32

func OfInt32(value int32) Int32

OfInt32 creates an Int32 with the given value in the present state.

func OfNullableInt32

func OfNullableInt32(ptr *int32) Int32

OfNullableInt32 creates an Int32 from a pointer. It is an alias for FromInt32Ptr.

func (*Int32) Scan

func (i *Int32) Scan(src any) error

Scan implements sql.Scanner.

func (Int32) Value

func (i Int32) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Int64

type Int64 struct {
	Field[int64]
}

Int64 is a three-state optional for int64 values.

func FromInt64Ptr

func FromInt64Ptr(ptr *int64) Int64

FromInt64Ptr creates an Int64 from a pointer.

func MissingInt64

func MissingInt64() Int64

MissingInt64 creates an Int64 in the missing state.

func NullInt64

func NullInt64() Int64

NullInt64 creates an Int64 in the null state.

func OfInt64

func OfInt64(value int64) Int64

OfInt64 creates an Int64 with the given value in the present state.

func OfNullableInt64

func OfNullableInt64(ptr *int64) Int64

OfNullableInt64 creates an Int64 from a pointer. It is an alias for FromInt64Ptr.

func (*Int64) Scan

func (i *Int64) Scan(src any) error

Scan implements sql.Scanner.

type String

type String struct {
	Field[string]
}

String is a three-state optional for string values.

func FromStringPtr

func FromStringPtr(ptr *string) String

FromStringPtr creates a String from a pointer.

func MissingString

func MissingString() String

MissingString creates a String in the missing state.

func NullString

func NullString() String

NullString creates a String in the null state.

func OfNullableString

func OfNullableString(ptr *string) String

OfNullableString creates a String from a pointer. It is an alias for FromStringPtr.

func OfString

func OfString(value string) String

OfString creates a String with the given value in the present state.

func (*String) Scan

func (s *String) Scan(src any) error

Scan implements sql.Scanner.

type Time

type Time struct {
	Field[time.Time]
}

Time is a three-state optional for time.Time values.

func FromTimePtr

func FromTimePtr(ptr *time.Time) Time

FromTimePtr creates a Time from a pointer.

func MissingTime

func MissingTime() Time

MissingTime creates a Time in the missing state.

func NullTime

func NullTime() Time

NullTime creates a Time in the null state.

func OfNullableTime

func OfNullableTime(ptr *time.Time) Time

OfNullableTime creates a Time from a pointer. It is an alias for FromTimePtr.

func OfTime

func OfTime(value time.Time) Time

OfTime creates a Time with the given value in the present state.

func (*Time) Scan

func (t *Time) Scan(src any) error

Scan implements sql.Scanner.

type UUID

type UUID struct {
	Field[uuid.UUID]
}

UUID is a three-state optional for uuid.UUID values.

func FromUUIDPtr

func FromUUIDPtr(ptr *uuid.UUID) UUID

FromUUIDPtr creates a UUID from a pointer.

func MissingUUID

func MissingUUID() UUID

MissingUUID creates a UUID in the missing state.

func NullUUID

func NullUUID() UUID

NullUUID creates a UUID in the null state.

func OfNullableUUID

func OfNullableUUID(ptr *uuid.UUID) UUID

OfNullableUUID creates a UUID from a pointer. It is an alias for FromUUIDPtr.

func OfUUID

func OfUUID(value uuid.UUID) UUID

OfUUID creates a UUID with the given value in the present state.

func (*UUID) Scan

func (u *UUID) Scan(src any) error

Scan implements sql.Scanner.

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value implements driver.Valuer, returning the UUID as a string for SQL compatibility.

type Uint

type Uint struct {
	Field[uint]
}

Uint is a three-state optional for uint values.

func FromUintPtr

func FromUintPtr(ptr *uint) Uint

FromUintPtr creates a Uint from a pointer.

func MissingUint

func MissingUint() Uint

MissingUint creates a Uint in the missing state.

func NullUint

func NullUint() Uint

NullUint creates a Uint in the null state.

func OfNullableUint

func OfNullableUint(ptr *uint) Uint

OfNullableUint creates a Uint from a pointer. It is an alias for FromUintPtr.

func OfUint

func OfUint(value uint) Uint

OfUint creates a Uint with the given value in the present state.

func (*Uint) Scan

func (u *Uint) Scan(src any) error

Scan implements sql.Scanner.

func (Uint) Value

func (u Uint) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Uint8

type Uint8 struct {
	Field[uint8]
}

Uint8 is a three-state optional for uint8 values.

func FromUint8Ptr

func FromUint8Ptr(ptr *uint8) Uint8

FromUint8Ptr creates a Uint8 from a pointer.

func MissingUint8

func MissingUint8() Uint8

MissingUint8 creates a Uint8 in the missing state.

func NullUint8

func NullUint8() Uint8

NullUint8 creates a Uint8 in the null state.

func OfNullableUint8

func OfNullableUint8(ptr *uint8) Uint8

OfNullableUint8 creates a Uint8 from a pointer. It is an alias for FromUint8Ptr.

func OfUint8

func OfUint8(value uint8) Uint8

OfUint8 creates a Uint8 with the given value in the present state.

func (*Uint8) Scan

func (u *Uint8) Scan(src any) error

Scan implements sql.Scanner.

func (Uint8) Value

func (u Uint8) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Uint16

type Uint16 struct {
	Field[uint16]
}

Uint16 is a three-state optional for uint16 values.

func FromUint16Ptr

func FromUint16Ptr(ptr *uint16) Uint16

FromUint16Ptr creates a Uint16 from a pointer.

func MissingUint16

func MissingUint16() Uint16

MissingUint16 creates a Uint16 in the missing state.

func NullUint16

func NullUint16() Uint16

NullUint16 creates a Uint16 in the null state.

func OfNullableUint16

func OfNullableUint16(ptr *uint16) Uint16

OfNullableUint16 creates a Uint16 from a pointer. It is an alias for FromUint16Ptr.

func OfUint16

func OfUint16(value uint16) Uint16

OfUint16 creates a Uint16 with the given value in the present state.

func (*Uint16) Scan

func (u *Uint16) Scan(src any) error

Scan implements sql.Scanner.

func (Uint16) Value

func (u Uint16) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Uint32

type Uint32 struct {
	Field[uint32]
}

Uint32 is a three-state optional for uint32 values.

func FromUint32Ptr

func FromUint32Ptr(ptr *uint32) Uint32

FromUint32Ptr creates a Uint32 from a pointer.

func MissingUint32

func MissingUint32() Uint32

MissingUint32 creates a Uint32 in the missing state.

func NullUint32

func NullUint32() Uint32

NullUint32 creates a Uint32 in the null state.

func OfNullableUint32

func OfNullableUint32(ptr *uint32) Uint32

OfNullableUint32 creates a Uint32 from a pointer. It is an alias for FromUint32Ptr.

func OfUint32

func OfUint32(value uint32) Uint32

OfUint32 creates a Uint32 with the given value in the present state.

func (*Uint32) Scan

func (u *Uint32) Scan(src any) error

Scan implements sql.Scanner.

func (Uint32) Value

func (u Uint32) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility.

type Uint64

type Uint64 struct {
	Field[uint64]
}

Uint64 is a three-state optional for uint64 values.

func FromUint64Ptr

func FromUint64Ptr(ptr *uint64) Uint64

FromUint64Ptr creates a Uint64 from a pointer.

func MissingUint64

func MissingUint64() Uint64

MissingUint64 creates a Uint64 in the missing state.

func NullUint64

func NullUint64() Uint64

NullUint64 creates a Uint64 in the null state.

func OfNullableUint64

func OfNullableUint64(ptr *uint64) Uint64

OfNullableUint64 creates a Uint64 from a pointer. It is an alias for FromUint64Ptr.

func OfUint64

func OfUint64(value uint64) Uint64

OfUint64 creates a Uint64 with the given value in the present state.

func (*Uint64) Scan

func (u *Uint64) Scan(src any) error

Scan implements sql.Scanner.

func (Uint64) Value

func (u Uint64) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility. Returns an error if the value exceeds math.MaxInt64.

type Uintptr

type Uintptr struct {
	Field[uintptr]
}

Uintptr is a three-state optional for uintptr values.

func FromUintptrPtr

func FromUintptrPtr(ptr *uintptr) Uintptr

FromUintptrPtr creates a Uintptr from a pointer.

func MissingUintptr

func MissingUintptr() Uintptr

MissingUintptr creates a Uintptr in the missing state.

func NullUintptr

func NullUintptr() Uintptr

NullUintptr creates a Uintptr in the null state.

func OfNullableUintptr

func OfNullableUintptr(ptr *uintptr) Uintptr

OfNullableUintptr creates a Uintptr from a pointer. It is an alias for FromUintptrPtr.

func OfUintptr

func OfUintptr(value uintptr) Uintptr

OfUintptr creates a Uintptr with the given value in the present state.

func (*Uintptr) Scan

func (u *Uintptr) Scan(src any) error

Scan implements sql.Scanner.

func (Uintptr) Value

func (u Uintptr) Value() (driver.Value, error)

Value implements driver.Valuer, returning int64 for SQL compatibility. Returns an error if the value exceeds math.MaxInt64.

Jump to

Keyboard shortcuts

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