util

package
v0.0.0-...-f0f1240 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2025 License: Apache-2.0 Imports: 7 Imported by: 40

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddUnique

func AddUnique[ID comparable, T Identifiable[ID]](list []*T, newList []*T) []*T

func All deprecated

func All[T any](collection []T, predicate func(T) bool) bool

https://github.com/samber/lo/issues/54

Deprecated: use lo.EveryBy instead.

func Any deprecated

func Any[T any](collection []T, predicate func(T) bool) bool

https://github.com/samber/lo/issues/54

Deprecated: use lo.SomeBy instead.

func Clone

func Clone[ID comparable, T any](m map[ID]*T) map[ID]*T

func CloneRef

func CloneRef[T any](r *T) *T

func Contains

func Contains[ID comparable](ids []ID, id ID) bool

func CopyURL

func CopyURL(u *url.URL) *url.URL

func DR

func DR[A, B any](a A, _ B) A

DR discards right

func Deref

func Deref[T any](list []*T, skipNil bool) []T

func DerefSlice

func DerefSlice[T any](collection []*T) []T

DerefSlice drops nil elements in the slice and return a new slice with dereferenced elements.

func ExtractIDs

func ExtractIDs[ID comparable, T Identifiable[ID]](list []*T) []ID

func ExtractKeys

func ExtractKeys[ID comparable, T any](m map[ID]*T) []ID

func Filter

func Filter[T any](collection []T, iteratee func(v T) bool) []T

Filter is similar to lo.Filter, but accepts an iteratee without the index argument.

func FilterMap

func FilterMap[T any, V any](collection []T, iteratee func(v T) *V) []V

FilterMap is similar to Map, but if the iteratee returns nil, that element will be omitted from the new slice.

func FilterMapOk deprecated

func FilterMapOk[T any, V any](collection []T, iteratee func(v T) (V, bool)) []V

FilterMapOk is similar to FilterMap, but the iteratee can return a boolean as the second return value, and it is false, that element will be omitted from the new slice.

Deprecated: use lo.FilterMap instead.

func FilterMapR

func FilterMapR[T any, V any](collection []T, iteratee func(v T) *V) []*V

FilterMapR is similar to FilterMap, but if the return value of the iteratee is not nil, it is not dereferenced and is used as the value of the new element.

func Find

func Find[ID comparable, T Identifiable[ID]](list []*T, lid ID) *T

func Get

func Get[ID comparable, T any](list []*T, getId func(*T) ID, id ID) *T

func HasDuplicates deprecated

func HasDuplicates[T comparable](collection []T) bool

HasDuplicates returns true if the array contains a duplicated element

Deprecated: use lo.FindDuplicated instead.

func IndexOf

func IndexOf[ID comparable, T any](list []*T, getId func(*T) ID, id ID) int

func Last

func Last[T any](list []*T) *T

func ListClone

func ListClone[T any](list []T, getClone func(T) T) []T

func ListFilter

func ListFilter[ID comparable, T any](list []T, id ID, getId func(T) ID) []T

func ListHas

func ListHas[ID comparable, T any](list []*T, getId func(*T) ID, id ID) bool

func ListMap

func ListMap[ID comparable, T Identifiable[ID]](list []*T) map[ID]*T

func ListMerge

func ListMerge[T comparable](list []T, list2 []T, getClone func(T) T, duplicateSkip bool) []T

func Map

func Map[T any, V any](collection []T, iteratee func(v T) V) []V

Map is similar to lo.Map, but accepts an iteratee without the index argument.

func MapAdd

func MapAdd[ID comparable, T Identifiable[ID]](m map[ID]*T, items ...*T) map[ID]*T

func MapError

func MapError[T comparable](t T, err error) func(m func(error) error) (T, error)

func MapList

func MapList[ID comparable, T any](m map[ID]*T, skipNil bool) []*T

func MapPick

func MapPick[ID comparable, T Identifiable[ID]](m map[ID]*T, idList IDLister[ID]) []*T

func MapWithIDFunc

func MapWithIDFunc[ID comparable, T any](list []*T, idFunc func(*T) ID, checkNil bool) map[ID]*T

func Merge

func Merge[ID comparable, T Identifiable[ID]](m map[ID]*T, m2 map[ID]*T) map[ID]*T

func MockNow

func MockNow(t time.Time) func()

func Now

func Now() time.Time

func OrError

func OrError[T comparable](t T, err error) (r T, _ error)

func Pick

func Pick[ID comparable, T Identifiable[ID]](list []*T, idList IDLister[ID]) []*T

func Properties

func Properties[ID comparable, T any](list []*T, getProperty func(*T) ID) []ID

func Remove

func Remove[ID comparable, T Identifiable[ID]](list []*T, idsToRemove ...ID) []*T

func RemoveById

func RemoveById[ID comparable, T any](list []*T, getId func(*T) ID, id ID) []*T

func RemoveByIds

func RemoveByIds[ID comparable, T any](list []*T, getId func(*T) ID, ids ...ID) []*T

func SortedEntries

func SortedEntries[K constraints.Ordered, V any](m map[K]V) []lo.Entry[K, V]

func Subset deprecated

func Subset[T comparable](collection, subCollection []T) bool

Subset returns true if all the subCollection items are included in the collection

Deprecated: use lo.Every instead.

func ToGenericList

func ToGenericList[S any, T any](list []*S, converter Converter[S, T]) []*T

func ToGenericListValue

func ToGenericListValue[S any, T any](list []S, converter ConverterValue[S, T]) []*T

func ToPtrIfNotEmpty

func ToPtrIfNotEmpty[T comparable](t T) *T

func Try

func Try(tries ...func() error) error

func TryFilterMap

func TryFilterMap[T any, V any](collection []T, iteratee func(v T) (V, bool, error)) ([]V, error)

TryFilterMap is similar to TryMap, but if the iteratee returns nil, that element will be omitted from the new slice.

func TryMap

func TryMap[T any, V any](collection []T, iteratee func(v T) (V, error)) ([]V, error)

TryMap is similar to Map, but when an error occurs in the iteratee, it terminates the iteration and returns an error.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache holds data can be accessed synchronously. The data will be automatically updated when it expires.

func NewCache

func NewCache[T any](updater func(context.Context, T) (T, error), expiresIn time.Duration) *Cache[T]

func (*Cache[T]) Get

func (c *Cache[T]) Get(ctx context.Context) (res T, _ error)

type Converter

type Converter[S any, T any] func(*S) *T

type ConverterValue

type ConverterValue[S any, T any] func(S) *T

type DiffResult

type DiffResult[T any] struct {
	Added      []T
	Updated    []DiffUpdate[T]
	NotUpdated []DiffUpdate[T]
	Deleted    []T
}

func Diff

func Diff[T any](prev, next []T, keyEqual, isUpdated func(T, T) bool) (r DiffResult[T])

func (DiffResult[T]) NotUpdatedNext

func (d DiffResult[T]) NotUpdatedNext() []T

func (DiffResult[T]) NotUpdatedPrev

func (d DiffResult[T]) NotUpdatedPrev() []T

func (DiffResult[T]) UpdatedNext

func (d DiffResult[T]) UpdatedNext() []T

func (DiffResult[T]) UpdatedPrev

func (d DiffResult[T]) UpdatedPrev() []T

type DiffUpdate

type DiffUpdate[T any] struct {
	Prev T
	Next T
}

type Element

type Element[T any] struct {
	Index   int
	Element T
}

func Enumerate

func Enumerate[T any](collection []T) []Element[T]

Enumerate returns a new slice with each element and its index.

type IDLister

type IDLister[ID comparable] interface {
	LayerCount() int
	Layers() []ID
}

type Identifiable

type Identifiable[ID comparable] interface {
	ID() ID
}

type List

type List[T comparable] []T

func (List[T]) Add

func (l List[T]) Add(elements ...T) List[T]

func (List[T]) AddUniq

func (l List[T]) AddUniq(elements ...T) List[T]

func (List[T]) At

func (l List[T]) At(i int) *T

func (List[T]) Concat

func (l List[T]) Concat(m []T) List[T]

func (List[T]) Copy

func (l List[T]) Copy() List[T]

func (List[T]) Delete

func (l List[T]) Delete(elements ...T) List[T]

func (List[T]) DeleteAt

func (l List[T]) DeleteAt(i int) List[T]

func (List[T]) Has

func (l List[T]) Has(elements ...T) bool

func (List[T]) Index

func (l List[T]) Index(e T) int

func (List[T]) Insert

func (l List[T]) Insert(i int, elements ...T) List[T]

func (List[T]) Intersect

func (l List[T]) Intersect(m []T) List[T]

func (List[T]) Len

func (l List[T]) Len() int

func (List[T]) Move

func (l List[T]) Move(e T, to int) List[T]

func (List[T]) MoveAt

func (l List[T]) MoveAt(from, to int) List[T]

func (List[T]) Ref

func (l List[T]) Ref() *List[T]

func (List[T]) Refs

func (l List[T]) Refs() []*T

func (List[T]) Reverse

func (l List[T]) Reverse() List[T]

type LockMap

type LockMap[T comparable] struct {
	// contains filtered or unexported fields
}

func (*LockMap[T]) Lock

func (m *LockMap[T]) Lock(k T) func()

func (*LockMap[T]) Unlock

func (m *LockMap[T]) Unlock(k T)

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewSyncMap

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

func SyncMapFrom

func SyncMapFrom[K comparable, V any](entries map[K]V) *SyncMap[K, V]

func (*SyncMap[K, V]) Clone

func (m *SyncMap[K, V]) Clone() *SyncMap[K, V]

func (*SyncMap[K, V]) CountAll

func (m *SyncMap[K, V]) CountAll(f func(key K, value V) bool) (i int)

func (*SyncMap[K, V]) Delete

func (m *SyncMap[K, V]) Delete(key K)

func (*SyncMap[K, V]) DeleteAll

func (m *SyncMap[K, V]) DeleteAll(key ...K)

func (*SyncMap[K, V]) Find

func (m *SyncMap[K, V]) Find(f func(key K, value V) bool) (v V)

func (*SyncMap[K, V]) FindAll

func (m *SyncMap[K, V]) FindAll(f func(key K, value V) bool) (v []V)

func (*SyncMap[K, V]) Keys

func (m *SyncMap[K, V]) Keys() (l []K)

func (*SyncMap[K, V]) Len

func (m *SyncMap[K, V]) Len() (i int)

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (vv V, _ bool)

func (*SyncMap[K, V]) LoadAll

func (m *SyncMap[K, V]) LoadAll(keys ...K) (r []V)

func (*SyncMap[K, V]) LoadAndDelete

func (m *SyncMap[K, V]) LoadAndDelete(key K) (vv V, ok bool)

func (*SyncMap[K, V]) LoadOr

func (m *SyncMap[K, V]) LoadOr(key K, o V) (res V)

func (*SyncMap[K, V]) LoadOrStore

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (vv V, _ bool)

func (*SyncMap[K, V]) Map

func (m *SyncMap[K, V]) Map(f func(K, V) V) *SyncMap[K, V]

func (*SyncMap[K, V]) Merge

func (m *SyncMap[K, V]) Merge(n *SyncMap[K, V])

func (*SyncMap[K, V]) Range

func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V)

func (*SyncMap[K, V]) StoreAll

func (m *SyncMap[K, V]) StoreAll(entries map[K]V)

func (*SyncMap[K, V]) Unsync

func (m *SyncMap[K, V]) Unsync() map[K]V

func (*SyncMap[K, V]) Values

func (m *SyncMap[K, V]) Values() (l []V)

type TimeNow

type TimeNow struct {
	// contains filtered or unexported fields
}

func (*TimeNow) Mock

func (t *TimeNow) Mock(now time.Time) func()

func (*TimeNow) Now

func (t *TimeNow) Now() time.Time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL