datautils

package
v0.0.0-...-fc13fb6 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewStringSet = NewSet[string] // nolint:gochecknoglobals

NewStringSet and StringSet are Aliases.

View Source
var Time timing // nolint:gochecknoglobals

Functions

func StructToMap

func StructToMap(obj any) (map[string]any, error)

Convert a struct to a map of string to any.

Types

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. nolint:ireturn

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 (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]) 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.

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 (Map[K, V]) Has

func (m Map[K, V]) Has(key K) bool

func (Map[K, V]) KeySet

func (m Map[K, V]) KeySet() Set[K]

func (Map[K, V]) Keys

func (m Map[K, V]) Keys() []K

func (Map[K, V]) ShallowCopy

func (m Map[K, V]) ShallowCopy() Map[K, V]

ShallowCopy performs copying which should cover most cases. For deep copy you could use goutils.Clone.

func (Map[K, V]) Values

func (m Map[K, V]) Values() []V

type NamedLists

type NamedLists[V any] IndexedLists[string, V]

NamedLists is a collection of lists each having a string name.

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 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

func (s Set[V]) Diff(other Set[V]) []V

Diff is a difference between 2 sets. Every object that is not within intersection will be returned.

func (Set[T]) Has

func (s Set[T]) Has(key T) bool

Has returns true if key is found in the set.

func (Set[T]) IsEmpty

func (s Set[T]) IsEmpty() bool

func (Set[T]) List

func (s Set[T]) List() []T

List returns unique set in a shape of a slice.

func (Set[T]) Remove

func (s Set[T]) Remove(key T)

Remove will delete a key from the set.

func (Set[V]) Subtract

func (s Set[V]) Subtract(other Set[V]) []V

Subtract will return objects from current set that didn't occur in the input.

type StringSet

type StringSet = Set[string]

type UniqueLists

type UniqueLists[ID, V comparable] map[ID]Set[V]

UniqueLists is a collection of Sets each associated to identifier.

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

Jump to

Keyboard shortcuts

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