types

package module
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 9 Imported by: 6

README

types

License Coverage GitHub Workflow Status Go Report Card Go PKG

Library for types conversion.

go get github.com/worldline-go/types

Usage

types.Map

Map based on map[string]interface{} and null values stay as nil.

This type not convert to base64 when marshaling, it is directly a json string.

type Train struct {
	Details types.Map `db:"details"`
}
types.RawJSON

[]byte type behind, same as json.RawMessage with scan and value methods.

type Train struct {
	Details types.RawJSON `db:"details"`
}
types.Slice[T]

Slice based on []T and null values stay as nil.

type Train struct {
	Slice types.Slice[string] `db:"slice"`
}
types.JSON[T]

For any type of json nullable value. Useful for struct values.

type Details struct {
	Name string `json:"name"`
}

type Train struct {
	Details types.JSON[Details] `db:"details"`
}
types.Null[T]

Wrapper of sql.Null[T] with additional json marshal and unmarshal methods.

types.Time and types.Null[types.Time]

Wrapper of time.Time with additional json marshal and unmarshal methods with database scan and value methods.


string, json.Number OR decimal.Decimal

Use decimal.Decimal always for calculations.

To use decimal.Decimal type from github.com/shopspring/decimal package.
Use json.Number type for json number values and after that convert to decimal.Decimal.
Use string type to direct get numeric values as string and convert to decimal.Decimal.

Use with nullable sql.Null[decimal.Decimal] package or pointer or decimal.NullDecimal type.

In struct use like this:

type Train struct {
	ID          int64                     `db:"id"          goqu:"skipinsert"`
	Details     types.Map                 `db:"details"     json:"details,omitempty"`
	Additionals types.RawJSON             `db:"additionals" json:"additionals,omitempty"`
	Price       sql.Null[json.Number]     `db:"price"`
	LastPrice   decimal.NullDecimal       `db:"last_price"`
}

Development

Go to example folder and run make command and fallow usage.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupportedType = errors.New("unsupported type")

Functions

This section is empty.

Types

type Decimal added in v0.4.7

type Decimal = decimal.Decimal

type JSON added in v0.2.0

type JSON[T any] struct {
	V     T
	Valid bool
}

func NewJSON added in v0.2.0

func NewJSON[T any](v T) JSON[T]

func (JSON[T]) MarshalJSON added in v0.2.1

func (s JSON[T]) MarshalJSON() ([]byte, error)

func (*JSON[T]) Scan added in v0.2.0

func (s *JSON[T]) Scan(value interface{}) error

func (*JSON[T]) UnmarshalJSON added in v0.2.1

func (s *JSON[T]) UnmarshalJSON(data []byte) error

func (JSON[T]) Value added in v0.2.0

func (s JSON[T]) Value() (driver.Value, error)

type Map

type Map[T any] map[string]T

func (*Map[T]) Scan

func (m *Map[T]) Scan(value interface{}) error

func (Map[T]) ToRawJSON added in v0.4.8

func (m Map[T]) ToRawJSON() (RawJSON, error)

func (*Map[T]) UnmarshalJSON added in v0.4.1

func (m *Map[T]) UnmarshalJSON(data []byte) error

func (Map[T]) Value

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

type Null added in v0.2.3

type Null[T any] struct {
	sql.Null[T]
	// ParsedNull is a helper field to distinguish between a null value and an omitted field during JSON unmarshalling.
	//  - if the field is present in the JSON (even if it's null), ParsedNull will be true.
	ParsedNull bool `json:"-"`
}

func NewNull added in v0.2.5

func NewNull[T any](v T) Null[T]

NewNull creates a Null[T] with the given value and Valid set to true.

func NewNullFromPtr added in v0.4.5

func NewNullFromPtr[T any](v *T) Null[T]

NewNullFromPtr creates a Null[T] from a pointer to T.

  • If the pointer is nil, it returns a Null[T] with Valid set to false and V set to the zero value of T.

func NewNullWithValid added in v0.4.5

func NewNullWithValid[T any](v T, valid bool) Null[T]

NewNullWithValid creates a Null[T] with the given value and Valid set to the specified boolean.

func NewTimeNull added in v0.4.8

func NewTimeNull(t time.Time) Null[Time]

func NewTimeNullFromPtr added in v0.4.8

func NewTimeNullFromPtr(t *time.Time) Null[Time]

func NewTimeNullWithValid added in v0.4.8

func NewTimeNullWithValid(t time.Time, valid bool) Null[Time]

func (Null[T]) MarshalJSON added in v0.2.3

func (n Null[T]) MarshalJSON() ([]byte, error)

func (Null[T]) Ptr added in v0.4.3

func (n Null[T]) Ptr() *T

Ptr returns a pointer to the inner value if Valid is true, otherwise it returns nil.

func (*Null[T]) Scan added in v0.5.3

func (n *Null[T]) Scan(value any) error

func (*Null[T]) UnmarshalJSON added in v0.2.3

func (n *Null[T]) UnmarshalJSON(data []byte) error

func (Null[T]) ValueOrZero added in v0.4.5

func (n Null[T]) ValueOrZero() T

ValueOrZero returns the inner value if valid, otherwise zero.

type NullDecimal added in v0.4.7

type NullDecimal = decimal.NullDecimal

type RawJSON added in v0.1.3

type RawJSON []byte

RawJSON same functionality with json.RawMessage and scan and value methods.

func (RawJSON) MarshalJSON added in v0.1.3

func (r RawJSON) MarshalJSON() ([]byte, error)

MarshalJSON returns m as the JSON encoding of m.

func (*RawJSON) Scan added in v0.1.3

func (r *RawJSON) Scan(value interface{}) error

func (RawJSON) ToMap added in v0.1.3

func (r RawJSON) ToMap() (Map[any], error)

func (*RawJSON) UnmarshalJSON added in v0.1.3

func (r *RawJSON) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *m to a copy of data.

func (RawJSON) Value added in v0.1.3

func (r RawJSON) Value() (driver.Value, error)

type Slice added in v0.2.0

type Slice[T any] []T

func (*Slice[T]) Scan added in v0.2.0

func (s *Slice[T]) Scan(value interface{}) error

func (Slice[T]) Value added in v0.2.0

func (s Slice[T]) Value() (driver.Value, error)

type Time added in v0.2.7

type Time struct {
	time.Time
}

func NewTime added in v0.4.8

func NewTime(t time.Time) Time

func (Time) MarshalJSON added in v0.2.7

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

func (*Time) Parse added in v0.2.8

func (t *Time) Parse(s string) error

func (*Time) Scan added in v0.3.0

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

Scan implements the [Scanner] interface.

func (Time) String added in v0.2.7

func (t Time) String() string

String returns the time in RFC3339 format.

func (*Time) UnmarshalJSON added in v0.2.7

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

func (Time) Value added in v0.3.0

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

Value implements the driver.Valuer interface.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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