first commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package genutils
|
||||
|
||||
func UpsertIntoArray[T comparable](arr []T, values ...T) []T {
|
||||
outer:
|
||||
for _, value := range values {
|
||||
for _, value2 := range arr {
|
||||
if value2 == value {
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
|
||||
arr = append(arr, value)
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
func RemoveFromArray[T comparable](arr []T, values ...T) []T {
|
||||
for _, value := range values {
|
||||
for i, value2 := range arr {
|
||||
if value2 == value {
|
||||
arr = append(arr[:i], arr[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package genutils
|
||||
|
||||
func Ptr[T any](t T) *T {
|
||||
return &t
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package genutils
|
||||
|
||||
type Set[T comparable] struct {
|
||||
m map[T]bool
|
||||
a []T
|
||||
}
|
||||
|
||||
func (set *Set[T]) Add(values ...T) {
|
||||
if set.m == nil {
|
||||
set.m = make(map[T]bool, len(values)*4)
|
||||
}
|
||||
|
||||
for _, value := range values {
|
||||
if set.m[value] {
|
||||
continue
|
||||
}
|
||||
|
||||
set.m[value] = true
|
||||
set.a = append(set.a, value)
|
||||
}
|
||||
}
|
||||
|
||||
func (set *Set[T]) Len() int {
|
||||
return len(set.a)
|
||||
}
|
||||
|
||||
func (set *Set[T]) Values() []T {
|
||||
return set.a[:len(set.a):len(set.a)]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package genutils
|
||||
|
||||
import "sort"
|
||||
|
||||
type Lesser[T any] interface {
|
||||
Less(T) bool
|
||||
}
|
||||
|
||||
func SortSlice[T Lesser[T]](slice []T) {
|
||||
sort.Slice(slice, func(i, j int) bool {
|
||||
return slice[i].Less(slice[j])
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
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...)
|
||||
}
|
||||
Reference in New Issue
Block a user