mapsutil

package
v0.0.92 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 19 Imported by: 92

README

mapsutil

The package contains various helpers to interact with maps

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrReadOnly = errorutil.New("map is currently in read-only mode").WithTag("syncLockMap")
)

Functions

func Clear added in v0.0.3

func Clear[K comparable, V any](mm ...map[K]V)

Clear the map passed as parameter

func DNSToMap

func DNSToMap(msg *dns.Msg, format string) (m map[string]interface{})

DNSToMap Converts DNS to Matcher Map

func Difference

func Difference[K comparable, V any](m map[K]V, keys ...K) map[K]V

Difference returns the inputted map without the keys specified as input.

func Flatten

func Flatten(m map[string]any, separator string) map[string]any

Flatten takes a map and returns a new one where nested maps are replaced by dot-delimited keys.

func GetKeys

func GetKeys[K comparable, V any](maps ...map[K]V) []K

GetKeys returns the map's keys.

func GetSortedKeys added in v0.0.34

func GetSortedKeys[K constraints.Ordered, V any](maps ...map[K]V) []K

GetSortedKeys returns the map's keys sorted.

func GetValues

func GetValues[K comparable, V any](maps ...map[K]V) []V

GetValues returns the map's values.

func HTTPRequestToMap added in v0.0.3

func HTTPRequestToMap(req *http.Request) (map[string]interface{}, error)

HTTPRequestToMap Converts HTTP Request to Matcher Map

func HTTPResponseToMap

func HTTPResponseToMap(resp *http.Response) (map[string]interface{}, error)

HTTPResponseToMap Converts HTTP Response to Matcher Map

func HTTPToMap

func HTTPToMap(resp *http.Response, body, headers string, duration time.Duration, format string) (m map[string]interface{})

HTTPToMap Converts HTTP to Matcher Map

func IsEmpty added in v0.0.3

func IsEmpty[K comparable, V any](m map[K]V) bool

IsEmpty checks if a map is empty.

func Merge added in v0.0.3

func Merge[K comparable, V any](maps ...map[K]V) (result map[K]V)

Merge merges the inputted maps into a new one. Be aware: In case of duplicated keys in multiple maps, the one ending in the result is unknown a priori.

func SliceToMap added in v0.0.3

func SliceToMap[T comparable](s []T, dflt T) map[T]T

SliceToMap returns a map having as keys the elements in even positions and as values the elements in odd positions. If the number of elements is odd the default value applies.

func Walk

func Walk(m map[string]any, callback func(k string, v any))

Walk a map and visit all the edge key:value pairs

Types

type Map added in v0.0.8

type Map[K, V comparable] map[K]V

Map wraps a generic map type

func (Map[K, V]) Clear added in v0.0.43

func (m Map[K, V]) Clear() bool

Clear the map

func (Map[K, V]) Clone added in v0.0.36

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

Clone the current map

func (Map[K, V]) Get added in v0.0.34

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

Get returns the value for the provided key

func (Map[K, V]) GetKeyWithValue added in v0.0.14

func (m Map[K, V]) GetKeyWithValue(value V) (K, bool)

GetKeyWithValue returns the first key having value

func (Map[K, V]) GetKeys added in v0.0.8

func (m Map[K, V]) GetKeys(keys ...K) []V

GetKeys from the map as a slice

func (Map[K, V]) GetOrDefault added in v0.0.8

func (m Map[K, V]) GetOrDefault(key K, defaultValue V) V

GetOrDefault the provided key or default to the provided value

func (Map[K, V]) Has added in v0.0.8

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

Has checks if the current map has the provided key

func (Map[K, V]) IsEmpty added in v0.0.34

func (m Map[K, V]) IsEmpty() bool

IsEmpty checks if the current map is empty

func (Map[K, V]) Merge added in v0.0.8

func (m Map[K, V]) Merge(n map[K]V)

Merge the current map with the provided one

func (Map[K, V]) Set added in v0.0.34

func (m Map[K, V]) Set(key K, value V)

Set the provided key with the provided value

type OrderedMap added in v0.0.40

type OrderedMap[k comparable, v any] struct {
	// contains filtered or unexported fields
}

OrderedMap is a map that preserves the order of elements Note: Order is only guaranteed for current level of OrderedMap nested values only have order preserved if they are also OrderedMap

func NewOrderedMap added in v0.0.40

func NewOrderedMap[k comparable, v any]() OrderedMap[k, v]

NewOrderedMap creates a new OrderedMap

func (*OrderedMap[k, v]) Clone added in v0.0.40

func (o *OrderedMap[k, v]) Clone() OrderedMap[k, v]

Clone returns clone of OrderedMap

func (*OrderedMap[k, v]) Delete added in v0.0.40

func (o *OrderedMap[k, v]) Delete(key k)

Delete deletes a value from the OrderedMap

func (*OrderedMap[k, v]) Get added in v0.0.40

func (o *OrderedMap[k, v]) Get(key k) (v, bool)

Get gets a value from the OrderedMap

func (*OrderedMap[k, v]) GetByIndex added in v0.0.40

func (o *OrderedMap[k, v]) GetByIndex(index int) (v, bool)

GetByIndex gets a value from the OrderedMap by index

func (*OrderedMap[k, v]) GetKeys added in v0.0.40

func (o *OrderedMap[k, v]) GetKeys() []k

GetKeys returns the keys of the OrderedMap

func (*OrderedMap[k, v]) Has added in v0.0.40

func (o *OrderedMap[k, v]) Has(key k) bool

Has checks if the OrderedMap has the provided key

func (*OrderedMap[k, v]) IsEmpty added in v0.0.40

func (o *OrderedMap[k, v]) IsEmpty() bool

IsEmpty checks if the OrderedMap is empty

func (*OrderedMap[k, v]) Iterate added in v0.0.40

func (o *OrderedMap[k, v]) Iterate(f func(key k, value v) bool)

Iterate iterates over the OrderedMap in insertion order

func (*OrderedMap[k, v]) Len added in v0.0.40

func (o *OrderedMap[k, v]) Len() int

Len returns the length of the OrderedMap

func (OrderedMap[k, v]) MarshalJSON added in v0.0.80

func (o OrderedMap[k, v]) MarshalJSON() ([]byte, error)

MarshalJSON marshals the OrderedMap to JSON

func (*OrderedMap[k, v]) Set added in v0.0.40

func (o *OrderedMap[k, v]) Set(key k, value v)

Set sets a value in the OrderedMap (if the key already exists, it will be overwritten)

func (*OrderedMap[k, v]) UnmarshalJSON added in v0.0.80

func (o *OrderedMap[k, v]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the OrderedMap from JSON

type SyncLockMap added in v0.0.8

type SyncLockMap[K, V comparable] struct {
	ReadOnly atomic.Bool

	Map Map[K, V]
	// contains filtered or unexported fields
}

SyncLock adds sync and lock capabilities to generic map

func NewSyncLockMap added in v0.0.46

func NewSyncLockMap[K, V comparable](options ...SyncLockMapOption[K, V]) *SyncLockMap[K, V]

NewSyncLockMap creates a new SyncLockMap. If an existing map is provided, it is used; otherwise, a new map is created.

func (*SyncLockMap[K, V]) Clear added in v0.0.43

func (s *SyncLockMap[K, V]) Clear() bool

IsEmpty checks if the current map is empty

func (*SyncLockMap[K, V]) Clone added in v0.0.36

func (s *SyncLockMap[K, V]) Clone() *SyncLockMap[K, V]

Clone creates a new SyncLockMap with the same values

func (*SyncLockMap[K, V]) Delete added in v0.0.43

func (s *SyncLockMap[K, V]) Delete(k K)

Get an item with syncronous access

func (*SyncLockMap[K, V]) Get added in v0.0.8

func (s *SyncLockMap[K, V]) Get(k K) (V, bool)

Get an item with syncronous access

func (*SyncLockMap[K, V]) GetAll added in v0.0.36

func (s *SyncLockMap[K, V]) GetAll() Map[K, V]

GetAll returns Copy of the current map

func (*SyncLockMap[K, V]) GetKeyWithValue added in v0.0.36

func (s *SyncLockMap[K, V]) GetKeyWithValue(value V) (K, bool)

GetKeywithValue returns the first key having value

func (*SyncLockMap[K, V]) Has added in v0.0.36

func (s *SyncLockMap[K, V]) Has(key K) bool

Has checks if the current map has the provided key

func (*SyncLockMap[K, V]) IsEmpty added in v0.0.36

func (s *SyncLockMap[K, V]) IsEmpty() bool

IsEmpty checks if the current map is empty

func (*SyncLockMap[K, V]) Iterate added in v0.0.8

func (s *SyncLockMap[K, V]) Iterate(f func(k K, v V) error) error

Iterate with a callback function synchronously

func (*SyncLockMap[K, V]) Lock added in v0.0.8

func (s *SyncLockMap[K, V]) Lock()

Lock the current map to read-only mode

func (*SyncLockMap[K, V]) Merge added in v0.0.36

func (s *SyncLockMap[K, V]) Merge(n map[K]V) error

Merge the current map with the provided one

func (*SyncLockMap[K, V]) Set added in v0.0.8

func (s *SyncLockMap[K, V]) Set(k K, v V) error

Set an item with syncronous access

func (*SyncLockMap[K, V]) Unlock added in v0.0.8

func (s *SyncLockMap[K, V]) Unlock()

Unlock the current map

type SyncLockMapOption added in v0.0.46

type SyncLockMapOption[K, V comparable] func(slm *SyncLockMap[K, V])

func WithMap added in v0.0.46

func WithMap[K, V comparable](m Map[K, V]) SyncLockMapOption[K, V]

Jump to

Keyboard shortcuts

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