Documentation
¶
Index ¶
- Variables
- func EqualUnordered[T comparable](a, b []T) bool
- func ForEach[F, T any](input []F, mapper func(F) T) []T
- func ForEachWithErr[F, T any](input []F, mapper func(F) (T, error)) ([]T, error)
- func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V
- func MergeSlices[T any](slices ...[]T) []T
- func StructToMap(obj any) (map[string]any, error)
- func ToAnySlice[T any](slice []T) []any
- type Cache
- type DefaultMap
- type IndexedLists
- type Map
- func (m Map[K, V]) AddMapValues(source Map[K, V])
- func (m Map[K, V]) DeepCopy() (Map[K, V], error)
- func (m Map[K, V]) Has(key K) bool
- func (m Map[K, V]) KeySet() Set[K]
- func (m Map[K, V]) Keys() []K
- func (m Map[K, V]) Select(keys []K) ([]V, []K)
- func (m Map[K, V]) ShallowCopy() Map[K, V]
- func (m Map[K, V]) ShallowSubset(keys []K) Map[K, V]
- func (m Map[K, V]) Values() []V
- type NamedLists
- type Set
- func (s Set[T]) Add(values []T)
- func (s Set[T]) AddOne(value T)
- func (s Set[V]) Diff(other Set[V]) []V
- func (s Set[T]) Equals(other Set[T]) bool
- func (s Set[T]) Has(key T) bool
- func (s Set[T]) HasExtra(allowed Set[T]) bool
- func (s Set[V]) Intersection(other Set[V]) []V
- func (s Set[T]) IsEmpty() bool
- func (s Set[T]) List() []T
- func (s Set[T]) MarshalJSON() ([]byte, error)
- func (s Set[T]) Remove(key T)
- func (s Set[V]) Subtract(other Set[V]) []V
- func (s *Set[T]) UnmarshalJSON(bytes []byte) error
- type StringSet
- type UniqueLists
Constants ¶
This section is empty.
Variables ¶
var NewStringSet = NewSet[string] // nolint:gochecknoglobals
NewStringSet and StringSet are Aliases.
var Time timing // nolint:gochecknoglobals
Functions ¶
func EqualUnordered ¶
func EqualUnordered[T comparable](a, b []T) bool
func ForEach ¶
func ForEach[F, T any](input []F, mapper func(F) T) []T
ForEach applies the provided function f to each element of s, returning a new slice containing the results.
Example:
names := []string{"alice", "bob"}
lengths := datautils.Map(names, func(s string) int { return len(s) })
// lengths == []int{5, 3}
func ForEachWithErr ¶
ForEachWithErr applies the provided mapper function to each element of the input slice. If the mapper returns an error for any element, the function returns immediately with that error. Otherwise, it returns a new slice containing the mapped results.
func MergeMaps ¶
func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V
MergeMaps creates a new map by combining all provided maps.
Each key from the input maps is copied into the resulting map. This is a **shallow copy**: the values themselves are not cloned, so if they are pointers, slices, maps, or other reference types, modifications to those values will be reflected in both the input and output maps.
If the same key exists in multiple input maps, the value from the last map in the argument list takes precedence.
The original maps are not modified.
func MergeSlices ¶
func MergeSlices[T any](slices ...[]T) []T
MergeSlices combines multiple slices of the same type into a single slice.
func StructToMap ¶
StructToMap convert a struct to a map of string to any.
func ToAnySlice ¶
ToAnySlice converts a slice of any type T to a slice of empty interface values ([]any). This is useful when you need to pass a typed slice to functions that expect []any.
Example:
ints := []int{1, 2, 3}
anySlice := datautils.ToAnySlice(ints)
// anySlice == []any{1, 2, 3}
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
func NewCache ¶
func NewCache[K comparable, V any]() *Cache[K, V]
type DefaultMap ¶
type DefaultMap[K comparable, V any] struct { // Map is a delegate. // All methods are embedded which grants the same capabilities, plus default value. Map[K, V] // contains filtered or unexported fields }
DefaultMap wrapper of the map that allows setting default return value on missing keys.
func NewDefaultMap ¶
func NewDefaultMap[K comparable, V any](dict Map[K, V], fallback func(K) V) DefaultMap[K, V]
func (DefaultMap[K, V]) Get ¶
func (m DefaultMap[K, V]) Get(key K) V
Get method uses map with a fallback value.
type IndexedLists ¶
type IndexedLists[ID comparable, V any] map[ID][]V
IndexedLists is a dynamic list of identifiable slices. Each slice is associated to the unique comparable object.
func MergeIndexedLists ¶
func MergeIndexedLists[ID comparable, V any](lists ...IndexedLists[ID, V]) IndexedLists[ID, V]
MergeIndexedLists combines IndexedLists into one.
func (IndexedLists[ID, V]) Add ¶
func (l IndexedLists[ID, V]) Add(bucket ID, objects ...V)
Add objects into the named list. If the list didn't exist it will create it.
func (IndexedLists[ID, V]) CombinedLength ¶
func (l IndexedLists[ID, V]) CombinedLength() int
func (IndexedLists[ID, V]) GetBuckets ¶
func (l IndexedLists[ID, V]) GetBuckets() []ID
GetBuckets returns names of lists, buckets they are in. Example:
veggies: cucumber, tomato; fruits: pineapple;
Will return veggies and fruits.
func (IndexedLists[ID, V]) Remove ¶
func (l IndexedLists[ID, V]) Remove(bucket ID, filter func(V) bool)
Remove will remove all elements in the list under the bucket matching the filter. A bucket may have a list with no items. If you need to remove the bucket itself, do it explicitly.
type Map ¶
type Map[K comparable, V any] map[K]V
Map is a generic version of map with useful methods. It can return Keys as a slice or a Set.
func FromMap ¶
func FromMap[K comparable, V any](source map[K]V) Map[K, V]
FromMap converts golang map into Map resolving generic types on its own. Example:
Given: dictionary = make(map[string]string) Then statements are equivalent: datautils.Map[string,string](golangMap) datautils.FromMap(dictionary)
func SliceToMap ¶
func SliceToMap[K comparable, V any](list []V, makeKey func(V) K) Map[K, V]
SliceToMap builds a map from a slice, using makeKey to derive each entry's key.
func (Map[K, V]) AddMapValues ¶
func (Map[K, V]) DeepCopy ¶
DeepCopy creates a deep copy of the map using `goutils.Clone`.
Internally this uses `encoding/gob`, so all concrete key/value types must be registered with `gob.Register` before use.
Register the missing types (e.g. `gob.Register(MyStruct{})`) before calling DeepCopy.
func (Map[K, V]) Select ¶
func (m Map[K, V]) Select(keys []K) ([]V, []K)
Select returns the values for the given keys, along with the keys not found. It is the multi-key equivalent of a map lookup.
func (Map[K, V]) ShallowCopy ¶
ShallowCopy creates a shallow copy of the map. It copies the top-level keys and values, but does not clone nested or referenced objects. Use this when you only need a separate map container, not deep copies of the values.
func (Map[K, V]) ShallowSubset ¶
ShallowSubset creates a shallow subset of the map containing only the specified keys. It copies references to the values for the given keys without cloning nested or referenced objects. Use this when you need a smaller map containing specific entries, not deep copies of the values.
type NamedLists ¶
type NamedLists[V any] IndexedLists[string, V]
NamedLists is a collection of lists each having a string name.
func MergeNamedLists ¶
func MergeNamedLists[V any](lists ...NamedLists[V]) NamedLists[V]
MergeNamedLists combines NamedLists into one.
func (NamedLists[V]) Add ¶
func (l NamedLists[V]) Add(bucket string, objects ...V)
func (NamedLists[V]) GetBuckets ¶
func (l NamedLists[V]) GetBuckets() []string
type Set ¶
type Set[T comparable] map[T]struct{}
Set data structure that can hold any type of data.
func MergeSets ¶
func MergeSets[T comparable](sets ...Set[T]) Set[T]
func NewSet ¶
func NewSet[V comparable](values ...V) Set[V]
NewSet creates a set from multiple values.
func NewSetFromList ¶
func NewSetFromList[V comparable](values []V) Set[V]
NewSetFromList creates a set from slice.
func (Set[T]) Add ¶
func (s Set[T]) Add(values []T)
Add will add a list of values to the unique set. Repetitions will be omitted.
func (Set[T]) AddOne ¶
func (s Set[T]) AddOne(value T)
AddOne will add single value to the unique set.
func (Set[V]) Diff ¶
Diff is a difference between 2 sets. Every object that is not within intersection will be returned.
func (Set[T]) Equals ¶
Equals checks if two sets are equal by comparing their contents. Two sets are considered equal if they have the same length and contain exactly the same elements.
func (Set[T]) HasExtra ¶
HasExtra returns true if this set contains any elements not in the allowed set.
func (Set[V]) Intersection ¶
func (Set[T]) MarshalJSON ¶
func (Set[V]) Subtract ¶
Subtract will return objects from current set that didn't occur in the input.
func (*Set[T]) UnmarshalJSON ¶
type UniqueLists ¶
type UniqueLists[ID, V comparable] map[ID]Set[V]
UniqueLists is a collection of Sets each associated to identifier.
func MergeUniqueLists ¶
func MergeUniqueLists[ID, V comparable](lists ...UniqueLists[ID, V]) UniqueLists[ID, V]
MergeUniqueLists combines UniqueLists into one.
func (UniqueLists[ID, V]) Add ¶
func (l UniqueLists[ID, V]) Add(bucket ID, objects ...V)
func (UniqueLists[ID, V]) GetBuckets ¶
func (l UniqueLists[ID, V]) GetBuckets() []ID
func (UniqueLists[ID, V]) GetList ¶
func (l UniqueLists[ID, V]) GetList(bucket ID) []V