Documentation ¶
Index ¶
- func Convert[T any](data any) (T, error)
- func Copy[T, D any](model *T, dto D) *T
- func MustConvert[T any](data any) T
- type Undefined
- func (u Undefined[T]) CopyValue() any
- func (u Undefined[T]) Default(defaultValue T) T
- func (u Undefined[T]) IsPresent() bool
- func (u Undefined[T]) IsZero() bool
- func (u *Undefined[T]) Scan(src any) error
- func (u *Undefined[T]) UnmarshalJSON(data []byte) error
- func (u *Undefined[T]) UnmarshalText(text []byte) error
- func (u Undefined[T]) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
MustConvert anything into the desired type using JSON marshaling and unmarshaling. Panics if it fails.
Types ¶
type Undefined ¶
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 ¶
NewUndefined creates a new `Undefined` wrapper with `Present` set to `true`.
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]) IsZero ¶
IsZero returns true for non-present values, for potential future omitempty support.
func (*Undefined[T]) Scan ¶
Scan implementation of `sql.Scanner` meant to be able to support copying from and to `Undefined` structures with `typeutil.Copy`.
func (*Undefined[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. On successful unmarshal of the underlying value, sets the `Present` field to `true`.
func (*Undefined[T]) UnmarshalText ¶
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`.