actualize items list in db

This commit is contained in:
Aleksandr Trushkin
2024-02-04 13:14:56 +03:00
parent 6042ca6822
commit 08be7de118
4 changed files with 138 additions and 43 deletions

View File

@ -1,18 +1,49 @@
package entity
func IterWithErr[T any](t []T, err error) iterWithErr[T] {
return iterWithErr[T]{
func IterIntoMap[K comparable, V any](v []V, err error) iterIntoMap[K, V] {
bi := IterWithErr(v, err)
return iterIntoMap[K, V]{
baseIter: bi,
}
}
type iterIntoMap[K comparable, V any] struct {
baseIter[V]
}
func (i iterIntoMap[K, V]) Map(f func(V) (K, error)) (map[K]V, error) {
if i.err != nil {
return nil, i.err
}
out := make(map[K]V, len(i.items))
for _, item := range i.items {
var key K
key, i.err = f(item)
if i.err != nil {
return nil, i.err
}
out[key] = item
}
return out, nil
}
func IterWithErr[T any](t []T, err error) baseIter[T] {
return baseIter[T]{
items: t,
err: err,
}
}
type iterWithErr[T any] struct {
type baseIter[T any] struct {
items []T
err error
}
func (iter iterWithErr[T]) Do(f func(T) error) error {
func (iter baseIter[T]) Do(f func(T) error) error {
if iter.err != nil {
return iter.err
}