types

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package types provides nullable wrappers and not-null helpers for values that must travel through both database/sql and JSON.

Every wrapper implements sql.Scanner and driver.Valuer so the types can be used directly with sql.Row.Scan and db.Exec, and every wrapper implements json.Marshaler / json.Unmarshaler with a canonical shape: an invalid value marshals to null, a valid one to its documented string form (for example "2006-01-02" for Date/NullDate). Each type exposes an IsEmpty method (see Emptiable) and a ToString method (see ToStringAble) that returns "" when the value is NULL.

The package is organised around pairs that share a single serializer/parser:

  • Date / NullDate — calendar date ("2006-01-02" or "02.01.2006" on input; always "2006-01-02" on output).
  • LocalDateTime / NullLocalDateTime — wall-clock date-time without a timezone ("2006-01-02T15:04:05[.fff…]").
  • OffsetDateTime / NullOffsetDateTime — date-time with an offset ("2006-01-02T15:04:05[.fff…]Z" or "±HH:MM").
  • LocalTime / NullLocalTime — wall-clock time of day without a timezone ("15:04:05[.fff…]").
  • OffsetTime / NullOffsetTime — time of day with an offset ("15:04:05[.fff…]Z" or "±HH:MM").

Additional nullable wrappers cover primitive types: NullString, NullBool, NullInt16/32/64, NullFloat, NullDecimal and NullUuid.

Nullable types accept the JSON token null on UnmarshalJSON and SQL NULL on Scan; their not-null counterparts reject both. Scan delegates to the corresponding sql.Null* wrapper so that typed nils from database drivers are honoured.

Index

Constants

View Source
const RuOnlyDateMask = "02.01.2006"

RuOnlyDateMask is the dd.MM.yyyy locale date format accepted by parseDate in addition to ISO 8601 (time.DateOnly).

Variables

This section is empty.

Functions

func AssembleDateTime

func AssembleDateTime(
	dateValue *time.Time,
	timeValue *time.Time,
	location *time.Location,
) *time.Time

AssembleDateTime combines the calendar date from dateValue with the wall-clock components from timeValue. If location is non-nil it is used as the resulting time.Location, otherwise timeValue's location is kept.

func AssembleDateTimeTZ

func AssembleDateTimeTZ(
	dateValue *time.Time,
	timeValue *time.Time,
	timeZone string,
) (*time.Time, error)

AssembleDateTimeTZ combines dateValue and timeValue and resolves the resulting location from the textual timeZone using ParseTimezoneExtended (IANA names, "Z", and "+HH:MM" / "+HHMM" offsets are all accepted). On a zone parse error the original dateValue is returned alongside the error.

func AssembleNullDateTimeTZ

func AssembleNullDateTimeTZ(
	dateValue *NullDate,
	defaultDate *time.Time,
	timeValue *NullOffsetTime,
	defaultTime *time.Time,
	timeZone string,
) (*time.Time, error)

AssembleNullDateTimeTZ is the NULL-aware variant of AssembleDateTimeTZ. A NULL date or time is replaced by the corresponding default (defaultDate / defaultTime); the returned pointer reflects the assembled instant in the zone decoded from timeZone.

func DateTimeToString

func DateTimeToString(t time.Time) string

DateTimeToString renders a datetime in the library's canonical TZ-aware format. Kept as a thin wrapper for callers that operate on bare time.Time values.

func DateToString

func DateToString(date time.Time) string

DateToString renders a calendar date in the library's canonical ISO 8601 form ("2006-01-02").

func GetNullString

func GetNullString(s sql.NullString) string

GetNullString returns the underlying string of an sql.NullString, or "" when the value is NULL. Mirrors NSFromString on the read side.

func IsEmpty

func IsEmpty(t interface{}) bool

IsEmpty reports whether v should be treated as empty/NULL. It accepts nil, primitives, the standard library's sql.Null* family, and any value that implements Emptiable (including pointer-receiver implementations invoked on a value).

func MaxDateTime

func MaxDateTime(dt1, dt2 time.Time) time.Time

MaxDateTime returns the later of dt1 and dt2. Ties return dt2.

func MinDateTime

func MinDateTime(dt1, dt2 time.Time) time.Time

MinDateTime returns the earlier of dt1 and dt2. Ties return dt2.

func NSFromString

func NSFromString(s string) sql.NullString

NSFromString builds an sql.NullString from a raw string, treating the empty string as NULL. Handy when composing queries against the database/sql package directly.

func ParseDateFromString

func ParseDateFromString(strValue string) (*time.Time, error)

ParseDateFromString parses a calendar date from a string using the formats supported by the library (ISO 8601 and dd.MM.yyyy). The returned pointer is never nil on success.

func ParseDateTimeFromString

func ParseDateTimeFromString(strValue string) (*time.Time, error)

ParseDateTimeFromString parses a datetime string. The date and time parts must be separated by either 'T' or ' '. The time part may carry an optional timezone designator ("Z", "+HH:MM", "+HHMM").

func ParseTimeFromString

func ParseTimeFromString(strValue string) (*time.Time, error)

ParseTimeFromString parses a time-of-day with an optional timezone designator ("HH:MM[:SS[.fff…]][Z|±HH:MM|±HHMM]").

func ParseTimezoneExtended

func ParseTimezoneExtended(strValue string) (*time.Location, string, error)

ParseTimezoneExtended attempts to extract a trailing timezone designator from strValue. Supported forms are "+HH:MM" / "-HH:MM" (six chars) and "+HHMM" / "-HHMM" (five chars). When a designator is found it is removed from the returned string and represented as a *time.Location; otherwise time.Local is returned.

func ToString

func ToString(val interface{}) string

ToString renders v using its ToString method when available (including pointer-receiver implementations invoked on a value), and falls back to fmt.Sprintf("%v", ...) otherwise.

Types

type Date

type Date time.Time

Date is a not-null calendar date (no time component, no timezone).

Date shares its serializer with NullDate: both render as "2006-01-02" and accept the same parser inputs (ISO 8601 and dd.MM.yyyy). Use Date for required JSON or SQL fields where NULL is not permitted; use NullDate for optional ones.

func DateFromString

func DateFromString(strValue string) (Date, error)

DateFromString parses a calendar date using the formats supported by the library (ISO 8601 and dd.MM.yyyy).

func NewDate

func NewDate(t time.Time) Date

NewDate wraps t as a Date. The time-of-day component is preserved on the underlying value but is dropped by the serializer (formatDate writes only "YYYY-MM-DD").

func (Date) AsTime

func (thisVal Date) AsTime() time.Time

AsTime returns the underlying time.Time.

func (Date) MarshalJSON

func (thisVal Date) MarshalJSON() ([]byte, error)

MarshalJSON renders the date as a JSON string in the library's canonical format ("2006-01-02").

func (*Date) Scan

func (thisVal *Date) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. A NULL value is rejected — Date cannot be empty.

func (Date) ToString

func (thisVal Date) ToString() string

ToString renders the date in the library's canonical format ("2006-01-02").

func (*Date) UnmarshalJSON

func (thisVal *Date) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string in any of the formats accepted by DateFromString. JSON null is rejected — Date cannot be empty.

func (Date) Value

func (thisVal Date) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface.

type Emptiable

type Emptiable interface {
	IsEmpty() bool
}

Emptiable is the interface implemented by every nullable type in the package. IsEmpty reports whether the value is missing (NULL).

type LocalDateTime

type LocalDateTime struct {
	Year    int
	Month   time.Month
	Day     int
	Hour    int
	Minute  int
	Second  int
	Nanosec int
}

LocalDateTime is a not-null wall-clock datetime without a timezone.

Use LocalDateTime for business events anchored to a calendar moment in a human's wall-clock time (e.g. "contract signed on 2026-04-18 at 13:00") where the offset is intentionally absent. For points in time with a known offset, use OffsetDateTime instead.

func LocalDateTimeFromString

func LocalDateTimeFromString(strValue string) (LocalDateTime, error)

LocalDateTimeFromString parses a wall-clock datetime in either ISO 8601 "T" form ("2006-01-02T15:04:05[.fff]") or the equivalent space-separated form. Inputs carrying a timezone designator are rejected.

func LocalDateTimeFromTime

func LocalDateTimeFromTime(t time.Time) LocalDateTime

LocalDateTimeFromTime extracts the wall-clock components of t and discards its location.

func NewLocalDateTime

func NewLocalDateTime(year int, month time.Month, day, hour, minute, second, nanosec int) LocalDateTime

NewLocalDateTime constructs a LocalDateTime from its components.

func (LocalDateTime) MarshalJSON

func (thisVal LocalDateTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in the library's canonical format.

func (*LocalDateTime) Scan

func (thisVal *LocalDateTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. A NULL value is rejected — LocalDateTime cannot be empty.

func (LocalDateTime) ToString

func (thisVal LocalDateTime) ToString() string

ToString renders the value in the library's canonical format ("2006-01-02T15:04:05[.fff]").

func (LocalDateTime) ToTime

func (thisVal LocalDateTime) ToTime(loc *time.Location) time.Time

ToTime materialises the wall-clock components in the given location. Pass time.UTC, time.Local, or a fixed zone according to how the value should be interpreted at the boundary with timezone-aware code. A nil loc is treated as time.UTC.

func (*LocalDateTime) UnmarshalJSON

func (thisVal *LocalDateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string in any of the formats accepted by LocalDateTimeFromString. JSON null is rejected — LocalDateTime cannot be empty.

func (LocalDateTime) Value

func (thisVal LocalDateTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. The value is emitted in time.UTC; the SQL column type should be TIMESTAMP WITHOUT TIME ZONE so the driver writes the wall-clock fields verbatim.

type LocalTime

type LocalTime struct {
	Hour    int
	Minute  int
	Second  int
	Nanosec int
}

LocalTime is a not-null wall-clock time-of-day without a timezone.

Use LocalTime for values like "shop opens at 09:00" — a time-of-day with no offset. For points in time with a known offset, use OffsetTime instead.

func LocalTimeFromString

func LocalTimeFromString(strValue string) (LocalTime, error)

LocalTimeFromString parses a wall-clock time-of-day in ISO 8601 form ("HH:MM[:SS[.fff]]"). Inputs carrying a timezone designator are rejected.

func LocalTimeFromTime

func LocalTimeFromTime(t time.Time) LocalTime

LocalTimeFromTime extracts the time-of-day components of t and discards its date and location.

func NewLocalTime

func NewLocalTime(hour, minute, second, nanosec int) LocalTime

NewLocalTime constructs a LocalTime from its components.

func (LocalTime) MarshalJSON

func (thisVal LocalTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in the library's canonical format.

func (*LocalTime) Scan

func (thisVal *LocalTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. Accepts time.Time, string, or []byte. A NULL value is rejected — LocalTime cannot be empty.

func (LocalTime) ToString

func (thisVal LocalTime) ToString() string

ToString renders the value in the library's canonical format ("15:04:05[.fff]").

func (LocalTime) ToTime

func (thisVal LocalTime) ToTime() time.Time

ToTime materialises the time-of-day on the zero date (year 0, January 1) in time.UTC.

func (*LocalTime) UnmarshalJSON

func (thisVal *LocalTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string in any of the formats accepted by LocalTimeFromString. JSON null is rejected — LocalTime cannot be empty.

func (LocalTime) Value

func (thisVal LocalTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. The value is emitted as a time.Time on the zero date in time.UTC.

type NullBool

type NullBool struct {
	Val   bool
	Valid bool
}

NullBool is a nullable boolean. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullBool

func NewNullBool(value bool) NullBool

NewNullBool constructs a valid NullBool wrapping value.

func NewNullBoolEmpty

func NewNullBoolEmpty() NullBool

NewNullBoolEmpty returns an invalid (NULL) NullBool.

func NullBoolFromString

func NullBoolFromString(strValue *string) NullBool

NullBoolFromString parses a bool from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullBool.

func (*NullBool) IsEmpty

func (thisVal *NullBool) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullBool) IsZero

func (thisVal NullBool) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullBool) MarshalJSON

func (thisVal NullBool) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON boolean, or null when empty. The valid path returns a shared literal slice ("true"/"false") to skip the reflection dispatch and allocation that json.Marshal does for the primitive bool type.

func (*NullBool) Scan

func (thisVal *NullBool) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullBool so that the driver's NULL signalling is honoured.

func (*NullBool) ToString

func (thisVal *NullBool) ToString() string

ToString renders the value as "true"/"false", or "" when NULL.

func (*NullBool) UnmarshalJSON

func (thisVal *NullBool) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON boolean or the token null. Any other input is treated as a parse error.

func (NullBool) Value

func (thisVal NullBool) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil).

type NullDate

type NullDate struct {
	Val   time.Time
	Valid bool
}

NullDate is a nullable calendar date (year/month/day with no time-of-day or zone component). Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullDate

func NewNullDate(value time.Time) NullDate

NewNullDate constructs a valid NullDate wrapping value.

func NewNullDateEmpty

func NewNullDateEmpty() NullDate

NewNullDateEmpty returns an invalid (NULL) NullDate.

func NullDateFromString

func NullDateFromString(strValue *string) NullDate

NullDateFromString parses a date from the string pointer using the library's accepted formats (ISO 8601 "2006-01-02" and the Russian "dd.MM.yyyy"). A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullDate.

func (*NullDate) IsEmpty

func (thisVal *NullDate) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullDate) IsZero

func (thisVal NullDate) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullDate) MarshalJSON

func (thisVal NullDate) MarshalJSON() ([]byte, error)

MarshalJSON renders the date as a JSON string in ISO 8601 form, or null when empty. The valid path writes directly to a single buffer via time.Time.AppendFormat, skipping the intermediate string and reflection dispatch that json.Marshal would perform.

func (*NullDate) Scan

func (thisVal *NullDate) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullTime so that the driver's NULL signalling is honoured.

func (*NullDate) ToString

func (thisVal *NullDate) ToString() string

ToString renders the date in ISO 8601 form, or "" when NULL.

func (*NullDate) UnmarshalJSON

func (thisVal *NullDate) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string using the accepted date formats (ISO 8601, dd.MM.yyyy) or the token null.

func (NullDate) Value

func (thisVal NullDate) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case is emitted as the underlying time.Time (drivers then format it as DATE).

type NullDecimal

type NullDecimal struct {
	Val   decimal.Decimal
	Valid bool
}

NullDecimal is a nullable arbitrary-precision decimal backed by github.com/shopspring/decimal. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func MulNullDecimals

func MulNullDecimals(val1, val2 NullDecimal) NullDecimal

MulNullDecimals multiplies two NullDecimals. The result is NULL if either operand is NULL.

func NewNullDecimal

func NewNullDecimal(val decimal.Decimal) NullDecimal

NewNullDecimal constructs a valid NullDecimal wrapping val.

func NewNullDecimalEmpty

func NewNullDecimalEmpty() NullDecimal

NewNullDecimalEmpty returns an invalid (NULL) NullDecimal.

func NullDecimalFromString

func NullDecimalFromString(strValue *string) NullDecimal

NullDecimalFromString parses a decimal from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullDecimal.

func (*NullDecimal) IsEmpty

func (thisVal *NullDecimal) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullDecimal) IsZero

func (thisVal NullDecimal) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullDecimal) MarshalJSON

func (thisVal NullDecimal) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON number (the format decimal.Decimal itself emits), or null when empty.

func (*NullDecimal) Scan

func (thisVal *NullDecimal) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. The decimal is received as text from the driver (via sql.NullString) and parsed through NullDecimalFromString so precision is preserved.

func (*NullDecimal) ToString

func (thisVal *NullDecimal) ToString() string

ToString renders the decimal via decimal.Decimal.String (no trailing zeros), or "" when NULL.

func (*NullDecimal) UnmarshalJSON

func (thisVal *NullDecimal) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON number, JSON string, or the token null. Any other input is treated as a parse error.

func (NullDecimal) Value

func (thisVal NullDecimal) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case delegates to decimal.Decimal.Value so full precision is preserved on the wire.

type NullFloat

type NullFloat struct {
	Val   float64
	Valid bool
}

NullFloat is a nullable float64. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullFloat

func NewNullFloat(value float64) NullFloat

NewNullFloat constructs a valid NullFloat wrapping value.

func NewNullFloatEmpty

func NewNullFloatEmpty() NullFloat

NewNullFloatEmpty returns an invalid (NULL) NullFloat.

func NullFloatFromString

func NullFloatFromString(strValue *string) NullFloat

NullFloatFromString parses a float64 from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullFloat.

func (*NullFloat) IsEmpty

func (thisVal *NullFloat) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullFloat) IsZero

func (thisVal NullFloat) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullFloat) MarshalJSON

func (thisVal NullFloat) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON number, or null when empty. The valid path uses strconv.AppendFloat directly to skip the reflection dispatch and intermediate buffer allocation that json.Marshal does for primitive numeric types. The 'g' verb mirrors encoding/json's own format for float64.

func (*NullFloat) Scan

func (thisVal *NullFloat) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullFloat64 so that the driver's NULL signalling is honoured.

func (*NullFloat) ToString

func (thisVal *NullFloat) ToString() string

ToString renders the value via fmt's %f verb, or "" when NULL.

func (*NullFloat) UnmarshalJSON

func (thisVal *NullFloat) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON number or the token null. Any other input is treated as a parse error.

func (NullFloat) Value

func (thisVal NullFloat) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil).

type NullInt16

type NullInt16 struct {
	Val   int16
	Valid bool
}

NullInt16 is a nullable int16. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullInt16

func NewNullInt16(value int16) NullInt16

NewNullInt16 constructs a valid NullInt16 wrapping value.

func NewNullInt16Empty

func NewNullInt16Empty() NullInt16

NewNullInt16Empty returns an invalid (NULL) NullInt16.

func NullInt16FromNullString

func NullInt16FromNullString(str NullString) NullInt16

NullInt16FromNullString parses an int16 from a NullString. An invalid or empty NullString, or a parse error, produce an invalid NullInt16.

func NullInt16FromString

func NullInt16FromString(strValue *string) NullInt16

NullInt16FromString parses an int16 from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullInt16.

func (*NullInt16) IsEmpty

func (thisVal *NullInt16) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullInt16) IsZero

func (thisVal NullInt16) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullInt16) MarshalJSON

func (thisVal NullInt16) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON number, or null when empty. The valid path uses strconv.AppendInt directly to skip the reflection dispatch and intermediate buffer allocation that json.Marshal does for primitive numeric types.

func (*NullInt16) Scan

func (thisVal *NullInt16) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullInt16 so that the driver's NULL signalling is honoured.

func (*NullInt16) ToString

func (thisVal *NullInt16) ToString() string

ToString renders the value in decimal form, or "" when NULL.

func (*NullInt16) UnmarshalJSON

func (thisVal *NullInt16) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON number or the token null. Any other input is treated as a parse error.

func (NullInt16) Value

func (thisVal NullInt16) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case is widened to int64 per the database/sql contract.

type NullInt32

type NullInt32 struct {
	Val   int32
	Valid bool
}

NullInt32 is a nullable int32. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullInt32

func NewNullInt32(value int32) NullInt32

NewNullInt32 constructs a valid NullInt32 wrapping value.

func NewNullInt32Empty

func NewNullInt32Empty() NullInt32

NewNullInt32Empty returns an invalid (NULL) NullInt32.

func NullInt32FromNullString

func NullInt32FromNullString(str NullString) NullInt32

NullInt32FromNullString parses an int32 from a NullString. An invalid or empty NullString, or a parse error, produce an invalid NullInt32.

func NullInt32FromString

func NullInt32FromString(strValue *string) NullInt32

NullInt32FromString parses an int32 from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullInt32.

func (*NullInt32) IsEmpty

func (thisVal *NullInt32) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullInt32) IsZero

func (thisVal NullInt32) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullInt32) MarshalJSON

func (thisVal NullInt32) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON number, or null when empty. The valid path uses strconv.AppendInt directly to skip the reflection dispatch and intermediate buffer allocation that json.Marshal does for primitive numeric types.

func (*NullInt32) Scan

func (thisVal *NullInt32) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullInt32 so that the driver's NULL signalling is honoured.

func (*NullInt32) ToString

func (thisVal *NullInt32) ToString() string

ToString renders the value in decimal form, or "" when NULL.

func (*NullInt32) UnmarshalJSON

func (thisVal *NullInt32) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON number or the token null. Any other input is treated as a parse error.

func (NullInt32) Value

func (thisVal NullInt32) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case is widened to int64 per the database/sql contract.

type NullInt64

type NullInt64 struct {
	Val   int64
	Valid bool
}

NullInt64 is a nullable int64. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullInt64

func NewNullInt64(value int64) NullInt64

NewNullInt64 constructs a valid NullInt64 wrapping value.

func NewNullInt64Empty

func NewNullInt64Empty() NullInt64

NewNullInt64Empty returns an invalid (NULL) NullInt64.

func NullInt64FromNullString

func NullInt64FromNullString(str NullString) NullInt64

NullInt64FromNullString parses an int64 from a NullString. An invalid or empty NullString, or a parse error, produce an invalid NullInt64.

func NullInt64FromString

func NullInt64FromString(strValue *string) NullInt64

NullInt64FromString parses an int64 from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullInt64.

func (*NullInt64) IsEmpty

func (thisVal *NullInt64) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullInt64) IsZero

func (thisVal NullInt64) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullInt64) MarshalJSON

func (thisVal NullInt64) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON number, or null when empty. The valid path uses strconv.AppendInt directly to skip the reflection dispatch and intermediate buffer allocation that json.Marshal does for primitive numeric types.

func (*NullInt64) Scan

func (thisVal *NullInt64) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullInt64 so that the driver's NULL signalling is honoured.

func (*NullInt64) ToString

func (thisVal *NullInt64) ToString() string

ToString renders the value in decimal form, or "" when NULL.

func (*NullInt64) UnmarshalJSON

func (thisVal *NullInt64) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON number or the token null. Any other input is treated as a parse error.

func (NullInt64) Value

func (thisVal NullInt64) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil).

type NullLocalDateTime

type NullLocalDateTime struct {
	Val   LocalDateTime
	Valid bool
}

NullLocalDateTime is the nullable counterpart of LocalDateTime. It shares the same serializer (formatLocalDateTime / parseLocalDateTime). Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullLocalDateTime

func NewNullLocalDateTime(value LocalDateTime) NullLocalDateTime

NewNullLocalDateTime constructs a valid NullLocalDateTime wrapping value.

func NewNullLocalDateTimeEmpty

func NewNullLocalDateTimeEmpty() NullLocalDateTime

NewNullLocalDateTimeEmpty returns an invalid (NULL) NullLocalDateTime.

func NullLocalDateTimeFromString

func NullLocalDateTimeFromString(strValue *string) NullLocalDateTime

NullLocalDateTimeFromString parses a local datetime from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullLocalDateTime.

func (*NullLocalDateTime) IsEmpty

func (thisVal *NullLocalDateTime) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullLocalDateTime) IsZero

func (thisVal NullLocalDateTime) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullLocalDateTime) MarshalJSON

func (thisVal NullLocalDateTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in canonical local datetime form, or null when empty. The valid path appends directly into a single buffer via time.Time.AppendFormat, skipping the intermediate string and reflection dispatch that json.Marshal(formatLocalDateTime(...)) would perform.

func (*NullLocalDateTime) Scan

func (thisVal *NullLocalDateTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullTime so that the driver's NULL signalling is honoured. The returned time's wall-clock components are preserved verbatim.

func (*NullLocalDateTime) ToString

func (thisVal *NullLocalDateTime) ToString() string

ToString renders the value in the library's canonical local datetime form, or "" when NULL.

func (*NullLocalDateTime) UnmarshalJSON

func (thisVal *NullLocalDateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string containing a local datetime, or the token null. Any other input is treated as a parse error.

func (NullLocalDateTime) Value

func (thisVal NullLocalDateTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case is materialised as a time.Time in UTC (drivers interpret TIMESTAMP WITHOUT TIME ZONE as the wall-clock components).

type NullLocalTime

type NullLocalTime struct {
	Val   LocalTime
	Valid bool
}

NullLocalTime is the nullable counterpart of LocalTime. It shares the same serializer (formatLocalTime / parseLocalTime). Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullLocalTime

func NewNullLocalTime(value LocalTime) NullLocalTime

NewNullLocalTime constructs a valid NullLocalTime wrapping value.

func NewNullLocalTimeEmpty

func NewNullLocalTimeEmpty() NullLocalTime

NewNullLocalTimeEmpty returns an invalid (NULL) NullLocalTime.

func NullLocalTimeFromString

func NullLocalTimeFromString(strValue *string) NullLocalTime

NullLocalTimeFromString parses a local time from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullLocalTime.

func (*NullLocalTime) IsEmpty

func (thisVal *NullLocalTime) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullLocalTime) IsZero

func (thisVal NullLocalTime) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullLocalTime) MarshalJSON

func (thisVal NullLocalTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in canonical local time form, or null when empty. The valid path appends directly into a single buffer via time.Time.AppendFormat, skipping the intermediate string and reflection dispatch that json.Marshal(formatLocalTime(...)) would perform.

func (*NullLocalTime) Scan

func (thisVal *NullLocalTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. NULL values produce an invalid NullLocalTime; non-NULL values are delegated to LocalTime.Scan.

func (*NullLocalTime) ToString

func (thisVal *NullLocalTime) ToString() string

ToString renders the value in the library's canonical local time form, or "" when NULL.

func (*NullLocalTime) UnmarshalJSON

func (thisVal *NullLocalTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string containing a local time, or the token null. Any other input is treated as a parse error.

func (NullLocalTime) Value

func (thisVal NullLocalTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case is materialised via LocalTime.ToTime (a same-day time.Time in UTC).

type NullOffsetDateTime

type NullOffsetDateTime struct {
	Val   time.Time
	Valid bool
}

NullOffsetDateTime is the nullable counterpart of OffsetDateTime. It shares the same serializer (formatOffsetDateTime / parseOffsetDateTime). Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullOffsetDateTime

func NewNullOffsetDateTime(value time.Time) NullOffsetDateTime

NewNullOffsetDateTime constructs a valid NullOffsetDateTime wrapping value.

func NewNullOffsetDateTimeEmpty

func NewNullOffsetDateTimeEmpty() NullOffsetDateTime

NewNullOffsetDateTimeEmpty returns an invalid (NULL) NullOffsetDateTime.

func NullOffsetDateTimeFromString

func NullOffsetDateTimeFromString(strValue *string) NullOffsetDateTime

NullOffsetDateTimeFromString parses an offset datetime from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullOffsetDateTime.

func (NullOffsetDateTime) After

func (thisVal NullOffsetDateTime) After(dt time.Time) bool

After reports whether the receiver is valid and its instant is after dt. An invalid (NULL) value is never after anything.

func (NullOffsetDateTime) Before

func (thisVal NullOffsetDateTime) Before(dt time.Time) bool

Before reports whether the receiver is valid and its instant is before dt. An invalid (NULL) value is never before anything.

func (*NullOffsetDateTime) IsEmpty

func (thisVal *NullOffsetDateTime) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullOffsetDateTime) IsZero

func (thisVal NullOffsetDateTime) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullOffsetDateTime) MarshalJSON

func (thisVal NullOffsetDateTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in canonical offset datetime form, or null when empty. The valid path appends directly into a single buffer via time.Time.AppendFormat, skipping the intermediate string and reflection dispatch that json.Marshal(formatOffsetDateTime(...)) would perform.

func (*NullOffsetDateTime) Scan

func (thisVal *NullOffsetDateTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullTime so that the driver's NULL signalling is honoured.

func (NullOffsetDateTime) ToString

func (thisVal NullOffsetDateTime) ToString() string

ToString renders the value in the library's canonical offset datetime form, or "" when NULL.

func (NullOffsetDateTime) Unix

func (thisVal NullOffsetDateTime) Unix() int64

Unix returns the underlying instant as a Unix timestamp — the number of seconds elapsed since January 1, 1970 UTC. An invalid (NULL) value returns 0.

func (*NullOffsetDateTime) UnmarshalJSON

func (thisVal *NullOffsetDateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string containing an offset datetime, or the token null. Any other input is treated as a parse error.

func (NullOffsetDateTime) Value

func (thisVal NullOffsetDateTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case is emitted as the underlying time.Time.

type NullOffsetTime

type NullOffsetTime struct {
	Val   time.Time
	Valid bool
}

NullOffsetTime is the nullable counterpart of OffsetTime. It shares the same serializer (formatOffsetTime / parseOffsetTime). Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullOffsetTime

func NewNullOffsetTime(value time.Time) NullOffsetTime

NewNullOffsetTime constructs a valid NullOffsetTime wrapping value.

func NewNullOffsetTimeEmpty

func NewNullOffsetTimeEmpty() NullOffsetTime

NewNullOffsetTimeEmpty returns an invalid (NULL) NullOffsetTime.

func NullOffsetTimeFromString

func NullOffsetTimeFromString(strValue *string) NullOffsetTime

NullOffsetTimeFromString parses an offset time from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullOffsetTime.

func (*NullOffsetTime) IsEmpty

func (thisVal *NullOffsetTime) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullOffsetTime) IsZero

func (thisVal NullOffsetTime) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullOffsetTime) MarshalJSON

func (thisVal NullOffsetTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in canonical offset time form, or null when empty. The valid path appends directly into a single buffer via time.Time.AppendFormat, skipping the intermediate string and reflection dispatch that json.Marshal(formatOffsetTime(...)) would perform.

func (*NullOffsetTime) Scan

func (thisVal *NullOffsetTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullTime so that the driver's NULL signalling is honoured.

func (NullOffsetTime) ToString

func (thisVal NullOffsetTime) ToString() string

ToString renders the value in the library's canonical offset time form, or "" when NULL.

func (*NullOffsetTime) UnmarshalJSON

func (thisVal *NullOffsetTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string containing an offset time, or the token null. Any other input is treated as a parse error.

func (NullOffsetTime) Value

func (thisVal NullOffsetTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case is emitted as the underlying time.Time.

type NullString

type NullString struct {
	Val   string
	Valid bool
}

NullString is a nullable string. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullString

func NewNullString(value string) NullString

NewNullString constructs a valid NullString wrapping value. The empty string is preserved as a valid value; use NewNullStringEmpty or NSFromString if an empty input should collapse to NULL.

func NewNullStringEmpty

func NewNullStringEmpty() NullString

NewNullStringEmpty returns an invalid (NULL) NullString.

func (*NullString) IsEmpty

func (thisVal *NullString) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullString) IsZero

func (thisVal NullString) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullString) MarshalJSON

func (thisVal NullString) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string, or null when empty.

func (*NullString) Scan

func (thisVal *NullString) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface, delegating to sql.NullString so that the driver's NULL signalling is honoured.

func (*NullString) ToString

func (thisVal *NullString) ToString() string

ToString returns the underlying string, or "" when NULL. Note that valid empty strings are indistinguishable from NULL in this output — use the Valid field directly to discriminate.

func (*NullString) UnmarshalJSON

func (thisVal *NullString) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string or the token null. Surrounding quotes and JSON escape sequences are decoded via encoding/json.

func (NullString) Value

func (thisVal NullString) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil).

type NullUuid

type NullUuid struct {
	Val   uuid.UUID
	Valid bool
}

NullUuid is a nullable UUID backed by github.com/google/uuid. Valid reports whether Val holds a meaningful value (true) or a SQL/JSON NULL (false).

func NewNullUuid

func NewNullUuid(val uuid.UUID) NullUuid

NewNullUuid constructs a valid NullUuid wrapping val.

func NewNullUuidEmpty

func NewNullUuidEmpty() NullUuid

NewNullUuidEmpty returns an invalid (NULL) NullUuid.

func NullUuidFromString

func NullUuidFromString(strValue *string) NullUuid

NullUuidFromString parses a UUID from the string pointer. A nil pointer, an empty string, the tokens "null"/"nil" (case-insensitive), or a parse error all produce an invalid NullUuid.

func (*NullUuid) IsEmpty

func (thisVal *NullUuid) IsEmpty() bool

IsEmpty reports whether the value is NULL (Valid == false).

func (NullUuid) IsZero

func (thisVal NullUuid) IsZero() bool

IsZero reports whether the value is NULL (Valid == false). Mirroring time.Time.IsZero, this also enables encoding/json's `omitzero` tag (Go 1.24+) to elide invalid wrappers from marshalled output.

func (NullUuid) MarshalJSON

func (thisVal NullUuid) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in canonical UUID form, or null when empty.

func (*NullUuid) Scan

func (thisVal *NullUuid) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. The UUID is received as text from the driver (via sql.NullString) and parsed through NullUuidFromString.

func (*NullUuid) ToString

func (thisVal *NullUuid) ToString() string

ToString renders the UUID in canonical 8-4-4-4-12 hex form, or "" when NULL.

func (*NullUuid) UnmarshalJSON

func (thisVal *NullUuid) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string containing a UUID, or the token null. Any other input is treated as a parse error.

func (NullUuid) Value

func (thisVal NullUuid) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. A NULL value is emitted as (nil, nil); the valid case delegates to uuid.UUID.Value, which renders the UUID as its canonical 8-4-4-4-12 hex string.

type OffsetDateTime

type OffsetDateTime time.Time

OffsetDateTime is a not-null datetime with a timezone offset (RFC 3339).

Use OffsetDateTime when the value is anchored to a specific point in time (e.g. an event timestamp). For wall-clock datetimes without an offset, use LocalDateTime.

func NewOffsetDateTime

func NewOffsetDateTime(t time.Time) OffsetDateTime

NewOffsetDateTime wraps t as an OffsetDateTime, preserving its location.

func OffsetDateTimeFromString

func OffsetDateTimeFromString(strValue string) (OffsetDateTime, error)

OffsetDateTimeFromString parses a datetime in any of the formats accepted by ParseDateTimeFromString.

func (OffsetDateTime) After

func (thisVal OffsetDateTime) After(dt time.Time) bool

After reports whether the receiver is after dt.

func (OffsetDateTime) AsTime

func (thisVal OffsetDateTime) AsTime() time.Time

AsTime returns the underlying time.Time.

func (OffsetDateTime) Before

func (thisVal OffsetDateTime) Before(dt time.Time) bool

Before reports whether the receiver is before dt.

func (OffsetDateTime) MarshalJSON

func (thisVal OffsetDateTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in the library's canonical TZ-aware format.

func (*OffsetDateTime) Scan

func (thisVal *OffsetDateTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. A NULL value is rejected — OffsetDateTime cannot be empty.

func (OffsetDateTime) ToString

func (thisVal OffsetDateTime) ToString() string

ToString renders the value in the library's canonical TZ-aware format.

func (OffsetDateTime) Unix

func (thisVal OffsetDateTime) Unix() int64

Unix returns the underlying instant as a Unix timestamp — the number of seconds elapsed since January 1, 1970 UTC.

func (*OffsetDateTime) UnmarshalJSON

func (thisVal *OffsetDateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string in any of the formats accepted by OffsetDateTimeFromString. JSON null is rejected — OffsetDateTime cannot be empty.

func (OffsetDateTime) Value

func (thisVal OffsetDateTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface.

type OffsetTime

type OffsetTime time.Time

OffsetTime is a not-null time-of-day with a timezone offset.

Use OffsetTime when the time-of-day value carries a meaningful zone offset (e.g. exchange opens at "09:30:00-05:00"). For zone-less times, use LocalTime.

func NewOffsetTime

func NewOffsetTime(t time.Time) OffsetTime

NewOffsetTime wraps t as an OffsetTime, preserving its location.

func OffsetTimeFromString

func OffsetTimeFromString(strValue string) (OffsetTime, error)

OffsetTimeFromString parses a time-of-day with an optional timezone designator. When no designator is present, time.Local is assumed.

func (OffsetTime) AsTime

func (thisVal OffsetTime) AsTime() time.Time

AsTime returns the underlying time.Time.

func (OffsetTime) MarshalJSON

func (thisVal OffsetTime) MarshalJSON() ([]byte, error)

MarshalJSON renders the value as a JSON string in the library's canonical TZ-aware format.

func (*OffsetTime) Scan

func (thisVal *OffsetTime) Scan(value interface{}) error

Scan implements the database/sql.Scanner interface. A NULL value is rejected — OffsetTime cannot be empty.

func (OffsetTime) String

func (thisVal OffsetTime) String() string

String renders the value in the library's canonical TZ-aware format.

func (OffsetTime) ToString

func (thisVal OffsetTime) ToString() string

ToString is an alias for String, satisfying the ToStringAble interface.

func (*OffsetTime) UnmarshalJSON

func (thisVal *OffsetTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses a JSON string in any of the formats accepted by OffsetTimeFromString. JSON null is rejected — OffsetTime cannot be empty.

func (OffsetTime) Value

func (thisVal OffsetTime) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface.

type ToStringAble

type ToStringAble interface {
	ToString() string
}

ToStringAble is the interface implemented by every nullable type in the package. ToString renders the value in its canonical textual form (an empty string when the value is missing/NULL).

Jump to

Keyboard shortcuts

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