null

package
v1.12.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package null provides utilities for handling nullable values in database operations. It includes helper functions for converting between sql.NullBool and other boolean representations, making it easier to work with nullable boolean values in Go applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool[T boolConstraint](val T) sql.NullBool

Bool creates a sql.NullBool from type-constrained input types. This function provides a type-safe way to create nullable boolean values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • bool: Creates a valid NullBool with the given boolean value
  • *bool: Creates a valid NullBool from pointer (nil pointer creates invalid NullBool)

Returns:

  • sql.NullBool: A NullBool struct with appropriate Valid flag and Bool value.

Examples:

// From bool value
nullBool := BoolGeneric(true) // Returns: {Bool: true, Valid: true}

// From bool pointer
boolPtr := &true
nullBool := BoolGeneric(boolPtr) // Returns: {Bool: true, Valid: true}

// From nil pointer
nullBool := BoolGeneric((*bool)(nil)) // Returns: {Bool: false, Valid: false}

func BoolAny added in v1.8.0

func BoolAny(nullBool sql.NullBool) driver.Value

BoolAny converts a sql.NullBool to a driver.Value for database operations. This function is typically used when you need to pass a nullable boolean value to database driver operations.

Parameters:

  • nullBool (sql.NullBool): The nullable boolean value to convert.

Returns:

  • driver.Value: The boolean value if valid, or nil if the NullBool is invalid/null.

Example:

nullBool := sql.NullBool{Bool: true, Valid: true}
value := BoolAny(nullBool) // Returns: true

invalidBool := sql.NullBool{Bool: false, Valid: false}
value := BoolAny(invalidBool) // Returns: nil

func BoolNil added in v1.8.0

func BoolNil(nullBool sql.NullBool) *bool

BoolNil converts a sql.NullBool to a pointer to bool (*bool). This function is useful when you need to work with nullable boolean values in your application logic, where nil represents a null database value.

Parameters:

  • nullBool (sql.NullBool): The nullable boolean value to convert.

Returns:

  • *bool: A pointer to the boolean value if valid, or nil if the NullBool is invalid/null.

Example:

nullBool := sql.NullBool{Bool: true, Valid: true}
ptr := BoolNil(nullBool) // Returns: &true

invalidBool := sql.NullBool{Bool: false, Valid: false}
ptr := BoolNil(invalidBool) // Returns: nil

func Byte

func Byte[T byteConstraint](val T) sql.NullByte

Byte creates a sql.NullByte from type-constrained input types. This function provides a type-safe way to create nullable byte values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • byte: Creates a valid NullByte with the given byte value
  • *byte: Creates a valid NullByte from pointer (nil pointer creates invalid NullByte)

Returns:

  • sql.NullByte: A NullByte struct with appropriate Valid flag and Byte value.

Examples:

// From byte value
nullByte := ByteGeneric(byte(65)) // Returns: {Byte: 65, Valid: true}

// From byte pointer
bytePtr := byte(97)
nullByte := ByteGeneric(&bytePtr) // Returns: {Byte: 97, Valid: true}

// From nil pointer
nullByte := ByteGeneric((*byte)(nil)) // Returns: {Byte: 0, Valid: false}

func ByteAny added in v1.8.0

func ByteAny(nullBool sql.NullByte) driver.Value

ByteAny converts a sql.NullByte to a driver.Value for database operations. This function is typically used when you need to pass a nullable byte value to database driver operations.

Parameters:

  • nullBool (sql.NullByte): The nullable byte value to convert.

Returns:

  • driver.Value: The byte value if valid, or nil if the NullByte is invalid/null.

Example:

nullByte := sql.NullByte{Byte: 65, Valid: true}
value := ByteAny(nullByte) // Returns: 65

invalidByte := sql.NullByte{Byte: 0, Valid: false}
value := ByteAny(invalidByte) // Returns: nil

func ByteNil added in v1.8.0

func ByteNil(nullByte sql.NullByte) *byte

ByteNil converts a sql.NullByte to a pointer to byte (*byte). This function is useful when you need to work with nullable byte values in your application logic, where nil represents a null database value.

Parameters:

  • nullByte (sql.NullByte): The nullable byte value to convert.

Returns:

  • *byte: A pointer to the byte value if valid, or nil if the NullByte is invalid/null.

Example:

nullByte := sql.NullByte{Byte: 65, Valid: true}
ptr := ByteNil(nullByte) // Returns: &65

invalidByte := sql.NullByte{Byte: 0, Valid: false}
ptr := ByteNil(invalidByte) // Returns: nil

func ByteVal added in v1.2.0

func ByteVal(nullByte sql.NullByte) byte

ByteVal returns the byte value of a sql.NullByte. If the NullByte is invalid (i.e., Valid is false), it returns 0.

Parameters:

  • nullByte (sql.NullByte): The nullable byte value to convert.

Returns:

  • byte: The byte value if valid, or 0 if the NullByte is invalid/null.

func Float64

func Float64[T floatConstraint](val T) sql.NullFloat64

Float64 creates a sql.NullFloat64 from type-constrained input types. This function provides a type-safe way to create nullable float64 values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • float64: Creates a valid NullFloat64 with the given float64 value
  • *float64: Creates a valid NullFloat64 from pointer (nil pointer creates invalid NullFloat64)

Returns:

  • sql.NullFloat64: A NullFloat64 struct with appropriate Valid flag and Float64 value.

Examples:

// From float64 value
nullFloat := Float64(3.14) // Returns: {Float64: 3.14, Valid: true}

// From float64 pointer
floatPtr := 2.71
nullFloat := Float64(&floatPtr) // Returns: {Float64: 2.71, Valid: true}

// From nil pointer
nullFloat := Float64((*float64)(nil)) // Returns: {Float64: 0, Valid: false}

func Float64Any added in v1.8.0

func Float64Any(nullInt sql.NullFloat64) driver.Value

Float64Any converts a sql.NullFloat64 to a driver.Value for database operations. This function is typically used when you need to pass a nullable float64 value to database driver operations.

Parameters:

  • nullInt (sql.NullFloat64): The nullable float64 value to convert.

Returns:

  • driver.Value: The float64 value if valid, or nil if the NullFloat64 is invalid/null.

Example:

nullFloat := sql.NullFloat64{Float64: 3.14, Valid: true}
value := Float64Any(nullFloat) // Returns: 3.14

invalidFloat := sql.NullFloat64{Float64: 0, Valid: false}
value := Float64Any(invalidFloat) // Returns: nil

func FloatNil added in v1.8.0

func FloatNil(nullFloat sql.NullFloat64) *float64

FloatNil converts a sql.NullFloat64 to a pointer to float64 (*float64). This function is useful when you need to work with nullable float64 values in your application logic, where nil represents a null database value.

Parameters:

  • nullFloat (sql.NullFloat64): The nullable float64 value to convert.

Returns:

  • *float64: A pointer to the float64 value if valid, or nil if the NullFloat64 is invalid/null.

Example:

nullFloat := sql.NullFloat64{Float64: 3.14, Valid: true}
ptr := FloatNil(nullFloat) // Returns: &3.14

invalidFloat := sql.NullFloat64{Float64: 0, Valid: false}
ptr := FloatNil(invalidFloat) // Returns: nil

func FloatVal added in v1.2.0

func FloatVal(nullFloat sql.NullFloat64) float64

FloatVal returns the float64 value of a sql.NullFloat64. If the NullFloat64 is invalid (i.e., Valid is false), it returns 0.

Parameters:

  • nullFloat (sql.NullFloat64): The nullable float64 value to convert.

Returns:

  • float64: The float64 value if valid, or 0 if the NullFloat64 is invalid/null.

func Int16

func Int16[T int16Constraint](val T) sql.NullInt16

Int16 creates a sql.NullInt16 from type-constrained input types. This function provides a type-safe way to create nullable int16 values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • int16: Creates a valid NullInt16 with the given int16 value
  • *int16: Creates a valid NullInt16 from pointer (nil pointer creates invalid NullInt16)

Returns:

  • sql.NullInt16: A NullInt16 struct with appropriate Valid flag and Int16 value.

Examples:

// From int16 value
nullInt := Int16(int16(42)) // Returns: {Int16: 42, Valid: true}

// From int16 pointer
intPtr := int16(100)
nullInt := Int16(&intPtr) // Returns: {Int16: 100, Valid: true}

// From nil pointer
nullInt := Int16((*int16)(nil)) // Returns: {Int16: 0, Valid: false}

func Int16Any added in v1.8.0

func Int16Any(nullInt sql.NullInt16) driver.Value

Int16Any converts a sql.NullInt16 to a driver.Value for database operations. This function is typically used when you need to pass a nullable int16 value to database driver operations.

Parameters:

  • nullInt (sql.NullInt16): The nullable int16 value to convert.

Returns:

  • driver.Value: The int16 value if valid, or nil if the NullInt16 is invalid/null.

Example:

nullInt := sql.NullInt16{Int16: 42, Valid: true}
value := Int16Any(nullInt) // Returns: 42

invalidInt := sql.NullInt16{Int16: 0, Valid: false}
value := Int16Any(invalidInt) // Returns: nil

func Int16Nil added in v1.8.0

func Int16Nil(nullInt sql.NullInt16) *int16

Int16Nil converts a sql.NullInt16 to a pointer to int16 (*int16). This function is useful when you need to work with nullable int16 values in your application logic, where nil represents a null database value.

Parameters:

  • nullInt (sql.NullInt16): The nullable int16 value to convert.

Returns:

  • *int16: A pointer to the int16 value if valid, or nil if the NullInt16 is invalid/null.

Example:

nullInt := sql.NullInt16{Int16: 42, Valid: true}
ptr := Int16Nil(nullInt) // Returns: &42

invalidInt := sql.NullInt16{Int16: 0, Valid: false}
ptr := Int16Nil(invalidInt) // Returns: nil

func Int16Val added in v1.2.0

func Int16Val(nullInt sql.NullInt16) int16

Int16Val returns the int16 value of a sql.NullInt16. If the NullInt16 is valid, it returns the int16 value. If the NullInt16 is invalid, it returns 0.

Parameters:

  • nullInt (sql.NullInt16): The nullable int16 value to convert.

Returns:

  • int16: The int16 value if valid, or 0 if invalid.

Example:

nullInt := sql.NullInt16{Int16: 42, Valid: true}
result := Int16Val(nullInt) // Returns: 42

invalidInt := sql.NullInt16{Int16: 0, Valid: false}
result := Int16Val(invalidInt) // Returns: 0

func Int32

func Int32[T int32Constraint](val T) sql.NullInt32

Int32 creates a sql.NullInt32 from type-constrained input types. This function provides a type-safe way to create nullable int32 values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • int32: Creates a valid NullInt32 with the given int32 value
  • *int32: Creates a valid NullInt32 from pointer (nil pointer creates invalid NullInt32)

Returns:

  • sql.NullInt32: A NullInt32 struct with appropriate Valid flag and Int32 value.

Examples:

// From int32 value
nullInt := Int32(int32(42)) // Returns: {Int32: 42, Valid: true}

// From int32 pointer
intPtr := int32(100)
nullInt := Int32(&intPtr) // Returns: {Int32: 100, Valid: true}

// From nil pointer
nullInt := Int32((*int32)(nil)) // Returns: {Int32: 0, Valid: false}

func Int32Any added in v1.8.0

func Int32Any(nullInt sql.NullInt32) driver.Value

Int32Any converts a sql.NullInt32 to a driver.Value for database operations. This function is typically used when you need to pass a nullable int32 value to database driver operations.

Parameters:

  • nullInt (sql.NullInt32): The nullable int32 value to convert.

Returns:

  • driver.Value: The int32 value if valid, or nil if the NullInt32 is invalid/null.

Example:

nullInt := sql.NullInt32{Int32: 42, Valid: true}
value := Int32Any(nullInt) // Returns: 42

invalidInt := sql.NullInt32{Int32: 0, Valid: false}
value := Int32Any(invalidInt) // Returns: nil

func Int32Nil added in v1.8.0

func Int32Nil(nullInt sql.NullInt32) *int32

Int32Nil converts a sql.NullInt32 to a pointer to int32 (*int32). This function is useful when you need to work with nullable int32 values in your application logic, where nil represents a null database value.

Parameters:

  • nullInt (sql.NullInt32): The nullable int32 value to convert.

Returns:

  • *int32: A pointer to the int32 value if valid, or nil if the NullInt32 is invalid/null.

Example:

nullInt := sql.NullInt32{Int32: 42, Valid: true}
ptr := Int32Nil(nullInt) // Returns: &42

invalidInt := sql.NullInt32{Int32: 0, Valid: false}
ptr := Int32Nil(invalidInt) // Returns: nil

func Int32Val added in v1.2.0

func Int32Val(nullInt sql.NullInt32) int32

Int32Val returns the int32 value of a sql.NullInt32. If the NullInt32 is valid, it returns the int32 value. If the NullInt32 is invalid, it returns 0.

Parameters:

  • nullInt (sql.NullInt32): The nullable int32 value to convert.

Returns:

  • int32: The int32 value if valid, or 0 if invalid.

Example:

nullInt := sql.NullInt32{Int32: 42, Valid: true}
result := Int32Val(nullInt) // Returns: 42

invalidInt := sql.NullInt32{Int32: 0, Valid: false}
result := Int32Val(invalidInt) // Returns: 0

func Int64

func Int64[T int64Constraint](val T) sql.NullInt64

Int64 creates a sql.NullInt64 from type-constrained input types. This function provides a type-safe way to create nullable int64 values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • int64: Creates a valid NullInt64 with the given int64 value
  • *int64: Creates a valid NullInt64 from pointer (nil pointer creates invalid NullInt64)

Returns:

  • sql.NullInt64: A NullInt64 struct with appropriate Valid flag and Int64 value.

Examples:

// From int64 value
nullInt := Int64(int64(42)) // Returns: {Int64: 42, Valid: true}

// From int64 pointer
intPtr := int64(100)
nullInt := Int64(&intPtr) // Returns: {Int64: 100, Valid: true}

// From nil pointer
nullInt := Int64((*int64)(nil)) // Returns: {Int64: 0, Valid: false}

func Int64Any added in v1.8.0

func Int64Any(nullInt sql.NullInt64) driver.Value

Int64Any converts a sql.NullInt64 to a driver.Value for database operations. This function is typically used when you need to pass a nullable int64 value to database driver operations.

Parameters:

  • nullInt (sql.NullInt64): The nullable int64 value to convert.

Returns:

  • driver.Value: The int64 value if valid, or nil if the NullInt64 is invalid/null.

Example:

nullInt := sql.NullInt64{Int64: 42, Valid: true}
value := Int64Any(nullInt) // Returns: 42

invalidInt := sql.NullInt64{Int64: 0, Valid: false}
value := Int64Any(invalidInt) // Returns: nil

func Int64Nil added in v1.8.0

func Int64Nil(nullInt sql.NullInt64) *int64

Int64Nil converts a sql.NullInt64 to a pointer to int64 (*int64). This function is useful when you need to work with nullable int64 values in your application logic, where nil represents a null database value.

Parameters:

  • nullInt (sql.NullInt64): The nullable int64 value to convert.

Returns:

  • *int64: A pointer to the int64 value if valid, or nil if the NullInt64 is invalid/null.

Example:

nullInt := sql.NullInt64{Int64: 42, Valid: true}
ptr := Int64Nil(nullInt) // Returns: &42

invalidInt := sql.NullInt64{Int64: 0, Valid: false}
ptr := Int64Nil(invalidInt) // Returns: nil

func Int64Val added in v1.2.0

func Int64Val(nullInt sql.NullInt64) int64

Int64Val returns the int64 value of a sql.NullInt64. If the NullInt64 is valid, it returns the int64 value. If the NullInt64 is invalid, it returns 0.

Parameters:

  • nullInt (sql.NullInt64): The nullable int64 value to convert.

Returns:

  • int64: The int64 value if valid, or 0 if invalid.

Example:

nullInt := sql.NullInt64{Int64: 42, Valid: true}
result := Int64Val(nullInt) // Returns: 42

invalidInt := sql.NullInt64{Int64: 0, Valid: false}
result := Int64Val(invalidInt) // Returns: 0

func String

func String[T stringConstraint](val T) sql.NullString

String creates a sql.NullString from type-constrained input types. This function provides a type-safe way to create nullable string values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • string: Creates a valid NullString with the given string value
  • *string: Creates a valid NullString from pointer (nil pointer creates invalid NullString)

Returns:

  • sql.NullString: A NullString struct with appropriate Valid flag and String value.

Examples:

// From string value
nullString := String("hello") // Returns: {String: "hello", Valid: true}

// From string pointer
strPtr := &"world"
nullString := String(strPtr) // Returns: {String: "world", Valid: true}

// From nil pointer
nullString := String((*string)(nil)) // Returns: {String: "", Valid: false}

func StringAny added in v1.8.0

func StringAny(nullString sql.NullString) driver.Value

StringAny converts a sql.NullString to a driver.Value for database operations. This function is typically used when you need to pass a nullable string value to database driver operations.

Parameters:

  • nullString (sql.NullString): The nullable string value to convert.

Returns:

  • driver.Value: The string value if valid, or nil if the NullString is invalid/null.

Example:

nullString := sql.NullString{String: "hello", Valid: true}
value := StringAny(nullString) // Returns: "hello"

invalidString := sql.NullString{String: "", Valid: false}
value := StringAny(invalidString) // Returns: nil

func StringNil added in v1.8.0

func StringNil(nullString sql.NullString) *string

StringNil converts a sql.NullString to a pointer to string (*string). This function is useful when you need to work with nullable string values in your application logic, where nil represents a null database value.

Parameters:

  • nullString (sql.NullString): The nullable string value to convert.

Returns:

  • *string: A pointer to the string value if valid, or nil if the NullString is invalid/null.

Example:

nullString := sql.NullString{String: "hello", Valid: true}
ptr := StringNil(nullString) // Returns: &"hello"

invalidString := sql.NullString{String: "", Valid: false}
ptr := StringNil(invalidString) // Returns: nil

func StringVal added in v1.2.0

func StringVal(nullString sql.NullString) string

StringVal returns the string value of a sql.NullString. If the NullString is valid, it returns the string value. If the NullString is invalid, it returns an empty string.

Parameters:

  • nullString (sql.NullString): The nullable string value to convert.

Returns:

  • string: The string value if valid, or an empty string if invalid.

Example:

nullString := sql.NullString{String: "hello", Valid: true}
result := StringVal(nullString) // Returns: "hello"

invalidString := sql.NullString{String: "", Valid: false}
result := StringVal(invalidString) // Returns: ""

func Time

func Time[T timeConstraint](val T) sql.NullTime

Time creates a sql.NullTime from type-constrained input types. This function provides a type-safe way to create nullable time.Time values for database operations, handling both direct values and pointers with compile-time type checking.

Parameters:

  • val (T): The input value to convert. Supported types:
  • time.Time: Creates a valid NullTime with the given time.Time value
  • *time.Time: Creates a valid NullTime from pointer (nil pointer creates invalid NullTime)

Returns:

  • sql.NullTime: A NullTime struct with appropriate Valid flag and Time value.

Examples:

// From time.Time value
now := time.Now()
nullTime := Time(now) // Returns: {Time: now, Valid: true}

// From time.Time pointer
timePtr := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
nullTime := Time(&timePtr) // Returns: {Time: timePtr, Valid: true}

// From nil pointer
nullTime := Time((*time.Time)(nil)) // Returns: {Time: time.Time{}, Valid: false}

func TimeAny added in v1.8.0

func TimeAny(nullTime sql.NullTime) driver.Value

TimeAny converts a sql.NullTime to a driver.Value for database operations. This function is typically used when you need to pass a nullable time.Time value to database driver operations.

Parameters:

  • nullTime (sql.NullTime): The nullable time.Time value to convert.

Returns:

  • driver.Value: The time.Time value if valid, or nil if the NullTime is invalid/null.

Example:

now := time.Now()
nullTime := sql.NullTime{Time: now, Valid: true}
value := TimeAny(nullTime) // Returns: now

invalidTime := sql.NullTime{Time: time.Time{}, Valid: false}
value := TimeAny(invalidTime) // Returns: nil

func TimeNil added in v1.8.0

func TimeNil(nullTime sql.NullTime) *time.Time

TimeNil converts a sql.NullTime to a pointer to time.Time (*time.Time). This function is useful when you need to work with nullable time.Time values in your application logic, where nil represents a null database value.

Parameters:

  • nullTime (sql.NullTime): The nullable time.Time value to convert.

Returns:

  • *time.Time: A pointer to the time.Time value if valid, or nil if the NullTime is invalid/null.

Example:

now := time.Now()
nullTime := sql.NullTime{Time: now, Valid: true}
ptr := TimeNil(nullTime) // Returns: &now

invalidTime := sql.NullTime{Time: time.Time{}, Valid: false}
ptr := TimeNil(invalidTime) // Returns: nil

func TimeNow added in v1.10.0

func TimeNow() sql.NullTime

TimeNow creates a sql.NullTime with the current time. This is a convenience function that creates a valid NullTime using the current system time, equivalent to calling Time(time.Now()).

Returns:

  • sql.NullTime: A valid NullTime struct with the current time and Valid set to true.

Example:

nullTime := TimeNow() // Returns: {Time: <current time>, Valid: true}

// Equivalent to:
nullTime := Time(time.Now())

Types

This section is empty.

Jump to

Keyboard shortcuts

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