typeutil

package
v5.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 7 Imported by: 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert[T any](data any) (T, error)

Convert anything into the desired type using JSON marshaling and unmarshaling.

func Copy

func Copy[T, D any](model *T, dto D) *T

Copy deep-copy a DTO's non-zero fields to the given model. The model is updated in-place and returned. Field names are matched in a case sensitive way. If you need to copy a zero-value (empty string, `false`, 0, etc) into the destination model, your DTO can take advantage of `typeutil.Undefined`. Panics if an error occurs.

func MustConvert

func MustConvert[T any](data any) T

MustConvert anything into the desired type using JSON marshaling and unmarshaling. Panics if it fails.

Types

type Undefined

type Undefined[T any] struct {
	Val     T
	Present bool
}

Undefined utility type wrapping a generic value used to differentiate between the absence of a field and its zero value, without using pointers.

This is especially useful when using wrappers such as `sql.NullString`, which are structures that encode/decode to a non-struct value. When working with requests that may or may not contain a field that is a nullable value, you cannot use pointers to define the presence or absence of this kind of structure. Thus the case where the field is absent (zero-value) and where the field is present but has a null value are indistinguishable.

This type only implements:

  • `encoding.TextUnmarshaler`
  • `json.Unmarshaler`
  • `driver.Valuer`

Because it only implements "read"-related interfaces, it is not recommended to use it for responses or for scanning database results. For these use-cases, it is recommended to use pointers for the field types with the json tag "omitempty".

func NewUndefined

func NewUndefined[T any](val T) Undefined[T]

NewUndefined creates a new `Undefined` wrapper with `Present` set to `true`.

func (Undefined[T]) CopyValue

func (u Undefined[T]) CopyValue() any

CopyValue implements the copier.Valuer interface.

func (Undefined[T]) Default

func (u Undefined[T]) Default(defaultValue T) T

Default return the value if present, otherwise returns the given default value.

func (Undefined[T]) IsPresent

func (u Undefined[T]) IsPresent() bool

IsPresent returns true for present values.

func (Undefined[T]) IsZero

func (u Undefined[T]) IsZero() bool

IsZero returns true for non-present values, for potential future omitempty support.

func (*Undefined[T]) Scan

func (u *Undefined[T]) Scan(src any) error

Scan implementation of `sql.Scanner` meant to be able to support copying from and to `Undefined` structures with `typeutil.Copy`.

func (*Undefined[T]) UnmarshalJSON

func (u *Undefined[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. On successful unmarshal of the underlying value, sets the `Present` field to `true`.

func (*Undefined[T]) UnmarshalText

func (u *Undefined[T]) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. If the input is a blank string, `Present` is set to `false`, otherwise `true`. This implementation will return an error if the underlying value doesn't implement `encoding.TextUnmarshaler`.

func (Undefined[T]) Value

func (u Undefined[T]) Value() (driver.Value, error)

Value implements the driver sql.Valuer interface.

Jump to

Keyboard shortcuts

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