types

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date struct {
	Time  time.Time
	Valid bool
}

Date is a custom type for representing dates (without time-of-day).

func NewDate

func NewDate(t time.Time) Date

NewDate creates a new valid Date, truncating the time to midnight.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether the Date is invalid or represents the zero time.

func (Date) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface. It converts the Date into a JSON string (or null if invalid).

func (*Date) Scan

func (d *Date) Scan(value any) error

Scan implements the sql.Scanner interface. It converts a database value into a Date, handling NULL, time.Time, []byte, and string inputs.

func (Date) String

func (d Date) String() string

String returns the Date formatted as YYYY-MM-DD, or an empty string if invalid.

func (*Date) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface. It parses a JSON string into a Date, handling null and empty strings.

func (Date) Value

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

Value implements the driver.Valuer interface. It converts the Date into a database-compatible value (string or NULL).

type String

type String struct {
	Val   string
	Valid bool
}

String is a custom type for handling nullable strings. It wraps a string value and a validity flag, similar to sql.NullString, but with extra helpers for JSON and convenience.

func NewString

func NewString(s string) String

Creates a new valid String from a raw string.

func (String) IsZero

func (s String) IsZero() bool

IsZero returns true if the String is invalid or contains an empty string. Useful for omitempty behavior in JSON or zero-value checks.

func (String) MarshalJSON

func (s String) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It encodes the string as a JSON string, or null if invalid.

func (String) Ptr

func (s String) Ptr() *string

Ptr returns a pointer to the underlying string value. Returns nil if the String is invalid. Useful for APIs expecting *string.

func (*String) Scan

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

Scan implements the sql.Scanner interface. It converts database values into a String, supporting NULL, string, and []byte.

func (String) String

func (s String) String() string

String returns the underlying string value, or an empty string if invalid. Implements the fmt.Stringer interface.

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It decodes JSON input into the String type, handling "null" as invalid.

func (String) Value

func (s String) Value() (driver.Value, error)

Value implements the driver.Valuer interface. It returns the string value for database storage, or nil if invalid.

type Time

type Time struct {
	Time  time.Time // The stored time-of-day (date is always set to year 1, month 1, day 1, UTC)
	Valid bool
}

Time is a custom type for representing only the time of day (HH:MM), without any associated date. It includes a validity flag to support NULL-like semantics for database and JSON operations.

func NewTime

func NewTime(t time.Time) Time

NewTime creates a new valid Time from a time.Time, stripping away the date and seconds while keeping only HH:MM.

func (Time) IsZero

func (t Time) IsZero() bool

IsZero reports whether the Time is invalid or represents the zero value.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It converts the Time into a JSON string ("HH:MM") or null if invalid.

func (*Time) Scan

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

Scan implements the sql.Scanner interface. It converts database values into a Time, handling NULL, time.Time, []byte, and string values.

func (Time) String

func (t Time) String() string

String returns the Time formatted as "HH:MM", or an empty string if invalid. Implements the fmt.Stringer interface.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It parses a JSON string into a Time, handling null and empty strings.

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements the driver.Valuer interface. It converts the Time into a database-compatible value (string or NULL).

type Timestamp

type Timestamp struct {
	Time  time.Time // The stored timestamp value, normalized to UTC
	Valid bool
}

Timestamp is a custom type for handling full date-time values (with timezone), stored in RFC3339 format. It includes a validity flag to support NULL-like semantics for databases and JSON.

func CombineDateAndTime

func CombineDateAndTime(d Date, t Time) Timestamp

CombineDateAndTime creates a new valid Timestamp from separate Date and Time values,

func NewTimestamp

func NewTimestamp(t time.Time) Timestamp

NewTimestamp creates a new valid Timestamp from a time.Time, normalizing to UTC and truncating to the nearest second.

func (Timestamp) IsZero

func (t Timestamp) IsZero() bool

IsZero reports whether the Timestamp is invalid or represents the zero time.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It converts the Timestamp into a JSON string in RFC3339 format, or null if invalid.

func (*Timestamp) Scan

func (t *Timestamp) Scan(value any) error

Scan implements the sql.Scanner interface. It converts database values into a Timestamp, handling NULL, time.Time, []byte, and string values.

func (Timestamp) String

func (t Timestamp) String() string

String returns the Timestamp formatted in RFC3339, or an empty string if invalid. Implements the fmt.Stringer interface.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It parses a JSON string into a Timestamp, handling null and empty strings.

func (Timestamp) Value

func (t Timestamp) Value() (driver.Value, error)

Value implements the driver.Valuer interface. It converts the Timestamp into a database-compatible value (time.Time or NULL).

Jump to

Keyboard shortcuts

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