You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.2 KiB
70 lines
1.2 KiB
package genutils
|
|
|
|
type hasValid interface {
|
|
Valid() bool
|
|
}
|
|
|
|
func ApplyUpdate[T any](dst *T, value *T) {
|
|
if value != nil {
|
|
*dst = *value
|
|
}
|
|
}
|
|
|
|
func ApplyUpdateValid[T hasValid](dst *T, value *T) {
|
|
if value != nil && (*value).Valid() {
|
|
*dst = *value
|
|
}
|
|
}
|
|
|
|
func ApplyUpdateNonZero[T comparable](dst *T, value *T) {
|
|
var zero T
|
|
if value != nil && *value != zero {
|
|
*dst = *value
|
|
}
|
|
}
|
|
|
|
func ApplyUpdateNilZero[T comparable](dst **T, value *T) {
|
|
if value != nil {
|
|
var zero T
|
|
|
|
if *value == zero {
|
|
*dst = nil
|
|
} else {
|
|
valueCopy := *value
|
|
*dst = &valueCopy
|
|
}
|
|
}
|
|
}
|
|
|
|
func ApplyMapUpdate[K comparable, V any](dst *map[K]V, src map[K]*V) {
|
|
if *dst == nil {
|
|
dst = &map[K]V{}
|
|
}
|
|
for key, value := range src {
|
|
if value != nil {
|
|
(*dst)[key] = *value
|
|
} else {
|
|
delete(*dst, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ApplyUpdateMapNilZero[K comparable, V comparable](dst *map[K]V, src map[K]V) {
|
|
var zero V
|
|
if *dst == nil {
|
|
dst = &map[K]V{}
|
|
}
|
|
for key, value := range src {
|
|
if value != zero {
|
|
(*dst)[key] = value
|
|
} else {
|
|
delete(*dst, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ApplyArrayUpdate[T comparable](arr *[]T, upsert []T, remove []T) {
|
|
*arr = make([]T, 0, len(*arr)+len(upsert))
|
|
*arr = UpsertIntoArray(*arr, upsert...)
|
|
*arr = RemoveFromArray(*arr, remove...)
|
|
}
|