nullable

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 9 Imported by: 1

README

sqlx-nullable

As we historically stacked with SQLX and PGX wasn't in its full glory, here's the collection of nullable structs for effective communication with database nulls and marshalling/unmarshalling from TS/JS

Each type must have Scan, Value, MarshalJSON and UnmarshalJSON method declared explicitly

Typical model

import (
    nl "github.com/reactima/reactima-nullable-go"
)

//go:generate go run ../generator-server/generator.go Cache hh_cache $GOFILE
//go:generate go run ../generator-front/generator.go Cache hh_cache $GOFILE
type Cache struct {
    ID     *int64         `json:"id" db:"cache_id"`

    UserID *nl.NullInt64  `json:"userID,omitempty" db:"cache_user_id"`
    Type   *nl.NullString `json:"type,omitempty" db:"cache_type"`
    TypeID *nl.NullInt64  `json:"typeID,omitempty" db:"cache_type_id"`

    Status   *nl.NullString `json:"status,omitempty" db:"cache_status"`
    Priority *nl.NullInt64  `json:"priority,omitempty" db:"cache_priority"`

    Data *nl.NullJSONText `json:"data,omitempty" db:"cache_data"`
    Meta *nl.NullJSONText `json:"meta,omitempty" db:"cache_meta"`

    EntryDate *nl.NullTime `json:"entryDate,omitempty" db:"cache_entry_date"`
    EditDate  *nl.NullTime `json:"editDate,omitempty" db:"cache_edit_date"`

    // EXTRA FIELDS
    RequestID string `json:"requestID,omitempty" hh:"ignore"`
} // @name Cache

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SqlPrepare

func SqlPrepare(x interface{}) interface{}

TODO add sanitizing function to make it even more safer see https://github.com/jackc/pgx

Types

type JSONText

type JSONText json.RawMessage

JSONText is a json.RawMessage, which is a []byte underneath. Value() validates the json format in the source, and returns an error if the json is not valid. Scan does no validation. JSONText additionally implements `Unmarshal`, which unmarshals the json within to an interface{}

func (JSONText) MarshalJSON

func (j JSONText) MarshalJSON() ([]byte, error)

MarshalJSON returns the *j as the JSON encoding of j.

func (*JSONText) Scan

func (j *JSONText) Scan(src interface{}) error

Scan stores the src in *j. No validation is done.

func (JSONText) String

func (j JSONText) String() string

String supports pretty printing for JSONText types.

func (*JSONText) Unmarshal

func (j *JSONText) Unmarshal(v interface{}) error

Unmarshal unmarshal's the json in j to v, as in json.Unmarshal.

func (*JSONText) UnmarshalJSON

func (j *JSONText) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *j to a copy of data

func (JSONText) Value

func (j JSONText) Value() (driver.Value, error)

Value returns j as a value. This does a validating unmarshal into another RawMessage. If j is invalid json, it returns an error.

type NullBool

type NullBool sql.NullBool

NullBool is an alias for sql.NullBool data type

func SetBool

func SetBool(b bool) *NullBool

func (NullBool) MarshalJSON

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

func (*NullBool) Scan

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

Scan implements the Scanner interface for NullBool

func (*NullBool) UnmarshalJSON

func (ns *NullBool) UnmarshalJSON(b []byte) error

func (*NullBool) Value

func (n *NullBool) Value() (driver.Value, error)

type NullBoolString

type NullBoolString struct {
	String string
	Valid  bool
}

TODO depricate

func SetStrBool

func SetStrBool(s string) *NullBoolString

func (NullBoolString) MarshalJSON

func (ns NullBoolString) MarshalJSON() ([]byte, error)

func (*NullBoolString) Scan

func (ns *NullBoolString) Scan(value interface{}) error

func (*NullBoolString) UnmarshalJSON

func (ns *NullBoolString) UnmarshalJSON(b []byte) error

func (NullBoolString) Value

func (ns NullBoolString) Value() (driver.Value, error)

type NullFloat64

type NullFloat64 struct {
	Float64 float64
	Valid   bool
}

func (NullFloat64) MarshalJSON

func (ns NullFloat64) MarshalJSON() ([]byte, error)

func (*NullFloat64) Scan

func (n *NullFloat64) Scan(value interface{}) error

func (NullFloat64) Value

func (n NullFloat64) Value() (driver.Value, error)

type NullInt64

type NullInt64 struct {
	Int64 int64
	Valid bool
}

NullInt64 can accept string or interger from UnmarshalJSON

func SetInt64

func SetInt64(i int64) *NullInt64

func SetInt64FromStr

func SetInt64FromStr(s string) *NullInt64

func (NullInt64) MarshalJSON

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

func (*NullInt64) Scan

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

func (*NullInt64) UnmarshalJSON

func (n *NullInt64) UnmarshalJSON(b []byte) error

func (NullInt64) Value

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

type NullJSONText

type NullJSONText struct {
	JSONText
	Valid bool // Valid is true if JSONText is not NULL
}

NullJSONText represents a JSONText that may be null. NullJSONText implements the scanner interface so it can be used as a scan destination, similar to NullString.

func SetEmptyJson

func SetEmptyJson() (*NullJSONText, error)

func (*NullJSONText) Scan

func (n *NullJSONText) Scan(value interface{}) error

Scan implements the Scanner interface.

func (NullJSONText) Value

func (n NullJSONText) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type NullString

type NullString struct {
	String string
	Valid  bool
}

NullString MarshalJSON ignores ""

func SetStr

func SetStr(s string) *NullString

func SetStrFromInt64

func SetStrFromInt64(i int64) *NullString

func (NullString) MarshalJSON

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

func (*NullString) Scan

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

func (*NullString) UnmarshalJSON

func (ns *NullString) UnmarshalJSON(b []byte) error

func (NullString) Value

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

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool
}

NullTime MarshalJSON ignores ZeroTime

func SetDateStrTime

func SetDateStrTime(s string) *NullTime

func SetNow

func SetNow() *NullTime

func (NullTime) MarshalJSON

func (ns NullTime) MarshalJSON() ([]byte, error)

func (*NullTime) Scan

func (nt *NullTime) Scan(value interface{}) error

func (*NullTime) UnmarshalJSON

func (ns *NullTime) UnmarshalJSON(b []byte) error

func (NullTime) Value

func (nt NullTime) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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