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 boolConstraint](val T) sql.NullBool
- func BoolNil(nullBool sql.NullBool) *bool
- func Byte[T byteConstraint](val T) sql.NullByte
- func ByteNil(nullByte sql.NullByte) *byte
- func ByteVal(nullByte sql.NullByte) byte
- func Float[T floatConstraint](val T) sql.NullFloat64
- func FloatNil(nullFloat sql.NullFloat64) *float64
- func FloatVal(nullFloat sql.NullFloat64) float64
- func Int16[T int16Constraint](val T) sql.NullInt16
- func Int16Nil(nullInt sql.NullInt16) *int16
- func Int16Val(nullInt sql.NullInt16) int16
- func Int32[T int32Constraint](val T) sql.NullInt32
- func Int32Nil(nullInt sql.NullInt32) *int32
- func Int32Val(nullInt sql.NullInt32) int32
- func Int64[T int64Constraint](val T) sql.NullInt64
- func Int64Nil(nullInt sql.NullInt64) *int64
- func Int64Val(nullInt sql.NullInt64) int64
- func String[T stringConstraint](val T) sql.NullString
- func StringNil(nullString sql.NullString) *string
- func StringVal(nullString sql.NullString) string
- func Time[T timeConstraint](val T) sql.NullTime
- 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 BoolNil ¶ added in v1.8.0
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 ByteNil ¶ added in v1.8.0
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
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 Float ¶ added in v1.12.2
func Float[T floatConstraint](val T) sql.NullFloat64
Float 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 Float value.
Examples:
// From float64 value
nullFloat := Float(3.14) // Returns: {Float: 3.14, Valid: true}
// From float64 pointer
floatPtr := 2.71
nullFloat := Float(&floatPtr) // Returns: {Float: 2.71, Valid: true}
// From nil pointer
nullFloat := Float((*float64)(nil)) // Returns: {Float: 0, Valid: false}
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{Float: 3.14, Valid: true}
ptr := FloatNil(nullFloat) // Returns: &3.14
invalidFloat := sql.NullFloat64{Float: 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 ¶
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 Int16Nil ¶ added in v1.8.0
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
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 ¶
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 Int32Nil ¶ added in v1.8.0
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
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 ¶
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 Int64Nil ¶ added in v1.8.0
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
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 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 ¶
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 TimeNil ¶ added in v1.8.0
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
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.