types

package
v0.0.0-...-710d95f Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package types implements some commonly used db serializable types like datetime, json, etc.

Index

Constants

View Source
const DefaultDateLayout = "2006-01-02 15:04:05.000Z"

DefaultDateLayout specifies the default app date strings layout.

Variables

This section is empty.

Functions

func Pointer

func Pointer[T any](val T) *T

Pointer is a generic helper that returns val as *T.

Types

type DateTime

type DateTime struct {
	// contains filtered or unexported fields
}

DateTime represents a time.Time instance in UTC that is wrapped and serialized using the app default date layout.

func NowDateTime

func NowDateTime() DateTime

NowDateTime returns new DateTime instance with the current local time.

func ParseDateTime

func ParseDateTime(value any) (DateTime, error)

ParseDateTime creates a new DateTime from the provided value (could be cast.ToTime supported string, time.Time, etc.).

func (DateTime) IsZero

func (d DateTime) IsZero() bool

IsZero checks whether the current DateTime instance has zero time value.

func (DateTime) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*DateTime) Scan

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

Scan implements [sql.Scanner] interface to scan the provided value into the current DateTime instance.

func (DateTime) String

func (d DateTime) String() string

String serializes the current DateTime instance into a formatted UTC date string.

The zero value is serialized to an empty string.

func (DateTime) Time

func (d DateTime) Time() time.Time

Time returns the internal time.Time instance.

func (*DateTime) UnmarshalJSON

func (d *DateTime) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (DateTime) Value

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

Value implements the driver.Valuer interface.

type JsonArray

type JsonArray[T any] []T

JsonArray defines a slice that is safe for json and db read/write.

func (JsonArray[T]) MarshalJSON

func (m JsonArray[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*JsonArray[T]) Scan

func (m *JsonArray[T]) Scan(value any) error

Scan implements [sql.Scanner] interface to scan the provided value into the current JsonArray[T] instance.

func (JsonArray[T]) Value

func (m JsonArray[T]) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type JsonMap

type JsonMap map[string]any

JsonMap defines a map that is safe for json and db read/write.

func (JsonMap) Get

func (m JsonMap) Get(key string) any

Get retrieves a single value from the current JsonMap.

This helper was added primarily to assist the goja integration since custom map types don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).

func (JsonMap) MarshalJSON

func (m JsonMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*JsonMap) Scan

func (m *JsonMap) Scan(value any) error

Scan implements [sql.Scanner] interface to scan the provided value into the current `JsonMap` instance.

func (JsonMap) Set

func (m JsonMap) Set(key string, value any)

Set sets a single value in the current JsonMap.

This helper was added primarily to assist the goja integration since custom map types don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).

func (JsonMap) Value

func (m JsonMap) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type JsonRaw

type JsonRaw []byte

JsonRaw defines a json value type that is safe for db read/write.

func ParseJsonRaw

func ParseJsonRaw(value any) (JsonRaw, error)

ParseJsonRaw creates a new JsonRaw instance from the provided value (could be JsonRaw, int, float, string, []byte, etc.).

func (JsonRaw) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*JsonRaw) Scan

func (j *JsonRaw) Scan(value interface{}) error

Scan implements [sql.Scanner] interface to scan the provided value into the current JsonRaw instance.

func (JsonRaw) String

func (j JsonRaw) String() string

String returns the current JsonRaw instance as a json encoded string.

func (*JsonRaw) UnmarshalJSON

func (j *JsonRaw) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (JsonRaw) Value

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

Value implements the driver.Valuer interface.

Jump to

Keyboard shortcuts

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