Documentation ¶
Overview ¶
package nullable provides Go nullable data type. This package handles database values as sql.Nullxxx does but with generic features. This package make possible to scan and store any structs' type to json and jsonb Postgresql data type thanks to github.com/jmoiron/sqlx for example.
Index ¶
- type JSON
- type NullableI
- type Of
- func (n *Of[T]) GetValue() *T
- func (n *Of[T]) IsNull() bool
- func (n *Of[T]) MarshalJSON() ([]byte, error)
- func (n *Of[T]) Scan(v any) error
- func (n *Of[T]) SetNull()
- func (n *Of[T]) SetValue(b T)
- func (n *Of[T]) SetValueP(ref *T)
- func (n *Of[T]) UnmarshalJSON(data []byte) error
- func (n *Of[T]) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NullableI ¶
type NullableI[T bool | int | int16 | int32 | int64 | string | uuid.UUID | float64 | JSON] interface { // IsNull returns true if itself is nil or the value is nil/null IsNull() bool // GetValue implements the getter. GetValue() *T // SetValue implements the setter. SetValue(T) // SetValueP implements the setter by pointer. SetValueP(*T) // SetNull set to null. SetNull() // MarshalJSON implements the encoding json interface. MarshalJSON() ([]byte, error) // UnmarshalJSON implements the decoding json interface. UnmarshalJSON([]byte) error // Value implements the driver.Valuer interface. Value() (driver.Value, error) // Scan implements the sql.Scanner interface. Scan(v any) error }
type Of ¶
type Of[T bool | int | int16 | int32 | int64 | string | uuid.UUID | float64 | JSON] struct { //nolint: tagliatelle // Internal use Val *T `json:"nullable_value" db:"_"` }
func FromValue ¶
func FromValue[T bool | int | int16 | int32 | int64 | string | uuid.UUID | float64 | JSON](b T) *Of[T]
FromValue is a Nullable constructor from the given value thanks to Go generics' inference.
func (*Of[T]) MarshalJSON ¶
MarshalJSON implements the encoding json interface.
func (*Of[T]) Scan ¶
Scan implements the sql.Scanner interface. This method decodes a JSON-encoded value into the struct.
func (*Of[T]) SetValueP ¶
func (n *Of[T]) SetValueP(ref *T)
SetValueP implements the setter by pointer. If ref is not nil, calls SetValue(*ref) If ref is nil, calls SetNull()
func (*Of[T]) UnmarshalJSON ¶
UnmarshalJSON implements the decoding json interface.