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 ¶
- func Bool[T boolType](val T) sql.NullBool
- func BoolAny(nullBool sql.NullBool) driver.Value
- func BoolNil(nullBool sql.NullBool) *bool
- func Byte[T byteType](val T) sql.NullByte
- func ByteAny(nullBool sql.NullByte) driver.Value
- func ByteNil(nullByte sql.NullByte) *byte
- func Float64[T floatType](val T) sql.NullFloat64
- func Float64Any(nullInt sql.NullFloat64) driver.Value
- func FloatNil(nullFloat sql.NullFloat64) *float64
- func Int16[T int16Type](val T) sql.NullInt16
- func Int16Any(nullInt sql.NullInt16) driver.Value
- func Int16Nil(nullInt sql.NullInt16) *int16
- func Int32[T int32Type](val T) sql.NullInt32
- func Int32Any(nullInt sql.NullInt32) driver.Value
- func Int32Nil(nullInt sql.NullInt32) *int32
- func Int64[T int64Type](val T) sql.NullInt64
- func Int64Any(nullInt sql.NullInt64) driver.Value
- func Int64Nil(nullInt sql.NullInt64) *int64
- func String[T stringType](val T) sql.NullString
- func StringAny(nullString sql.NullString) driver.Value
- func StringNil(nullString sql.NullString) *string
- func Time[T timeType](val T) sql.NullTime
- func TimeAny(nullTime sql.NullTime) driver.Value
- func TimeNil(nullTime sql.NullTime) *time.Time
- func TimeNow() sql.NullTime
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Float64 ¶
func Float64[T floatType](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 ¶
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 ¶
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 Int16 ¶
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 ¶
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 ¶
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 Int32 ¶
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 ¶
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 ¶
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 Int64 ¶
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 ¶
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 ¶
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 String ¶
func String[T stringType](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 ¶
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 ¶
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 Time ¶
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 ¶
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 ¶
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 ¶
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.