Documentation ¶
Overview ¶
Package strukt provides operations on structs
SPDX-License-Identifier: Apache-2.0
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapToStruct ¶
func MapToStruct[T any](mp map[string]any, strukt *T, ignore ...MapStructIgnoreMode) error
MapToStruct recurses through a map[string]any, populating the provided struct instance. conv.To is used to convert a map key value into a struct field value. The provided struct instance cannot be nil. The map key is converted from snake_case to CamelCase, as only exported fields can be populated.
If the map key is a sub map, then the related struct field must be a (*)struct or Maybe[(*)struct]. If the map key value is an array/slice of type T, then the related struct field must be an array/slice of type T. If any map key value cannot be converted, an error is returned, and the target struct will be partially populated. If the submap is empty, a (Maybe)struct value is set to the zero value, while a (Maybe)*struct is set to nil. If the submap is not empty, a struct value is populated and if needed, pointers are allocated and/oror it is wrapped in a Maybe.
If the ignore flag is set to UnmappedFieldIgnore, then the map may contain keys that do not exist in the struct, but it is still an error if the field exists in the struct and the map value cannot be converted. The default ignore flag value is UnmappedFieldError, which requires every map key to exist in the struct.
The following errors can occur: - type T is not a struct type - strukt is a nil pointer
Types ¶
type MapStructIgnoreMode ¶
type MapStructIgnoreMode bool
MapStructIgnoreMode indicates whether or not a map key that does not correspond to a struct field is ignored
const ( UnmappedFieldError MapStructIgnoreMode = false UnmappedFieldIgnore MapStructIgnoreMode = true )