maps

package
v0.154.5 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: Apache-2.0 Imports: 11 Imported by: 146

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanConfigStringMap added in v0.112.0

func CleanConfigStringMap(m map[string]any) map[string]any

CleanConfigStringMap is the same as CleanConfigStringMapString but for map[string]any.

func CleanConfigStringMapString added in v0.112.0

func CleanConfigStringMapString(m map[string]string) map[string]string

CleanConfigStringMapString removes any processing instructions from m, m will never be modified.

func ConvertFloat64WithNoDecimalsToInt added in v0.118.0

func ConvertFloat64WithNoDecimalsToInt(m map[string]any)

ConvertFloat64WithNoDecimalsToInt converts float64 values with no decimals to int recursively.

func GetNestedParam added in v0.56.0

func GetNestedParam(keyStr, separator string, candidates ...Params) (any, error)

GetNestedParam gets the first match of the keyStr in the candidates given. It will first try the exact match and then try to find it as a nested map value, using the given separator, e.g. "mymap.name". It assumes that all the maps given have lower cased keys.

func GetNestedParamFn added in v0.56.0

func GetNestedParamFn(keyStr, separator string, lookupFn func(key string) any) (any, string, map[string]any, error)

func LookupEqualFold added in v0.97.0

func LookupEqualFold[T any | string](m map[string]T, key string) (T, string, bool)

LookupEqualFold finds key in m with case insensitive equality checks.

func MergeParams added in v0.112.0

func MergeParams(dst, src Params)

MergeParams transfers values from src to dst for new keys using the merge encoded in dst. This is done recursively.

func MergeParamsWithStrategy added in v0.112.0

func MergeParamsWithStrategy(strategy string, dst, src Params)

MergeParamsWithStrategy transfers values from src to dst for new keys using the merge strategy given. This is done recursively.

func MergeShallow added in v0.112.0

func MergeShallow(dst, src map[string]any)

MergeShallow merges src into dst, but only if the key does not already exist in dst. The keys are compared case insensitively.

func PrepareParams added in v0.84.0

func PrepareParams(m Params)

PrepareParams * makes all the keys in the given map lower cased and will do so recursively. * This will modify the map given. * Any nested map[interface{}]interface{}, map[string]interface{},map[string]string will be converted to Params. * Any _merge value will be converted to proper type and value.

func SetParams added in v0.112.0

func SetParams(dst, src Params)

SetParams overwrites values in dst with values in src for common or new keys. This is done recursively.

func ToSliceStringMap added in v0.77.0

func ToSliceStringMap(in any) ([]map[string]any, error)

ToSliceStringMap converts in to []map[string]interface{}.

func ToStringMap added in v0.60.0

func ToStringMap(in any) map[string]any

ToStringMap converts in to map[string]interface{}.

func ToStringMapBool added in v0.84.0

func ToStringMapBool(in any) map[string]bool

ToStringMapBool converts in to bool.

func ToStringMapE added in v0.60.0

func ToStringMapE(in any) (map[string]any, error)

ToStringMapE converts in to map[string]interface{}.

func ToStringMapString added in v0.84.0

func ToStringMapString(in any) map[string]string

ToStringMapString converts in to map[string]string.

func ToStringMapStringE added in v0.84.0

func ToStringMapStringE(in any) (map[string]string, error)

ToStringMapStringE converts in to map[string]string.

Types

type Cache added in v0.123.0

type Cache[K comparable, T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Cache is a simple thread safe cache backed by a map.

func NewCache added in v0.123.0

func NewCache[K comparable, T any]() *Cache[K, T]

NewCache creates a new Cache with default options.

func NewCacheWithOptions added in v0.149.0

func NewCacheWithOptions[K comparable, T any](opts CacheOptions) *Cache[K, T]

NewCacheWithOptions creates a new Cache with the given options.

func (*Cache[K, T]) Contains added in v0.146.0

func (c *Cache[K, T]) Contains(key K) bool

Contains returns whether the given key exists in the cache.

func (*Cache[K, T]) Drain added in v0.128.0

func (c *Cache[K, T]) Drain() map[K]T

func (*Cache[K, T]) ForEeach added in v0.126.0

func (c *Cache[K, T]) ForEeach(f func(K, T) bool)

ForEeach calls the given function for each key/value pair in the cache. If the function returns false, the iteration stops.

func (*Cache[K, T]) Get added in v0.123.0

func (c *Cache[K, T]) Get(key K) (T, bool)

Delete deletes the given key from the cache. If c is nil, this method is a no-op.

func (*Cache[K, T]) GetOrCreate added in v0.123.0

func (c *Cache[K, T]) GetOrCreate(key K, create func() (T, error)) (T, error)

GetOrCreate gets the value for the given key if it exists, or creates it if not.

func (*Cache[K, T]) InitAndGet added in v0.138.0

func (c *Cache[K, T]) InitAndGet(key K, init func(get func(key K) (T, bool), set func(key K, value T)) error) (T, error)

InitAndGet initializes the cache if not already done and returns the value for the given key. The init state will be reset on Reset or Drain.

func (*Cache[K, T]) Len added in v0.128.0

func (c *Cache[K, T]) Len() int

func (*Cache[K, T]) Reset added in v0.128.0

func (c *Cache[K, T]) Reset()

func (*Cache[K, T]) Set added in v0.123.0

func (c *Cache[K, T]) Set(key K, value T)

Set sets the given key to the given value.

func (*Cache[K, T]) SetIfAbsent added in v0.146.0

func (c *Cache[K, T]) SetIfAbsent(key K, value T)

SetIfAbsent sets the given key to the given value if the key does not already exist in the cache.

type CacheOptions added in v0.149.0

type CacheOptions struct {
	// If set, the cache will not grow beyond this size.
	Size uint64
}

CacheOptions are the options for the Cache.

type KeyParams added in v0.112.0

type KeyParams struct {
	Key    string
	Params Params
}

KeyParams is an utility struct for the WalkParams method.

type KeyRenamer added in v0.54.0

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

KeyRenamer supports renaming of keys in a map.

func NewKeyRenamer added in v0.54.0

func NewKeyRenamer(patternKeys ...string) (KeyRenamer, error)

NewKeyRenamer creates a new KeyRenamer given a list of pattern and new key value pairs.

func (KeyRenamer) Rename added in v0.54.0

func (r KeyRenamer) Rename(m map[string]any)

Rename renames the keys in the given map according to the patterns in the current KeyRenamer.

type Map added in v0.153.0

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

Map is a thread safe map backed by a Go map.

func NewMap added in v0.153.0

func NewMap[K comparable, T any]() *Map[K, T]

func (*Map[K, T]) All added in v0.153.0

func (m *Map[K, T]) All() iter.Seq2[K, T]

All returns an iterator over all key/value pairs in the map. A read lock is held during the iteration.

func (*Map[K, T]) Delete added in v0.154.0

func (m *Map[K, T]) Delete(key K) bool

Delete deletes the given key from the map. It returns true if the key was found and deleted, false otherwise.

func (*Map[K, T]) Get added in v0.153.0

func (m *Map[K, T]) Get(key K) T

Get gets the value for the given key. It returns the zero value of T if the key is not found.

func (*Map[K, T]) GetOrCreate added in v0.153.0

func (m *Map[K, T]) GetOrCreate(key K, create func() (T, error)) (T, error)

GetOrCreate gets the value for the given key if it exists, or creates it if not.

func (*Map[K, T]) Lookup added in v0.153.0

func (m *Map[K, T]) Lookup(key K) (T, bool)

Lookup looks up the given key in the map. It returns the value and a boolean indicating whether the key was found.

func (*Map[K, T]) Set added in v0.153.0

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

Set sets the given key to the given value.

func (*Map[K, T]) SetIfAbsent added in v0.153.0

func (m *Map[K, T]) SetIfAbsent(key K, value T) bool

SetIfAbsent sets the given key to the given value if the key does not already exist in the map. It returns true if the value was set, false otherwise.

func (*Map[K, T]) WithWriteLock added in v0.153.0

func (m *Map[K, T]) WithWriteLock(f func(m map[K]T) error) error

WithWriteLock executes the given function with a write lock on the map.

type Ordered added in v0.143.0

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

Ordered is a map that can be iterated in the order of insertion. Note that insertion order is not affected if a key is re-inserted into the map. In a nil map, all operations are no-ops. This is not thread safe.

func NewOrdered added in v0.143.0

func NewOrdered[K comparable, T any]() *Ordered[K, T]

NewOrdered creates a new Ordered map.

func (*Ordered[K, T]) Clone added in v0.143.0

func (m *Ordered[K, T]) Clone() *Ordered[K, T]

Clone creates a shallow copy of the map.

func (*Ordered[K, T]) Delete added in v0.143.0

func (m *Ordered[K, T]) Delete(key K)

Delete deletes the value for the given key.

func (*Ordered[K, T]) Get added in v0.143.0

func (m *Ordered[K, T]) Get(key K) (T, bool)

Get gets the value for the given key.

func (*Ordered[K, T]) Has added in v0.146.0

func (m *Ordered[K, T]) Has(key K) bool

Has returns whether the given key exists in the map.

func (*Ordered[K, T]) Hash added in v0.143.0

func (m *Ordered[K, T]) Hash() (uint64, error)

Hash calculates a hash from the values.

func (*Ordered[K, T]) Keys added in v0.143.0

func (m *Ordered[K, T]) Keys() []K

Keys returns the keys in the order they were added.

func (*Ordered[K, T]) Len added in v0.143.0

func (m *Ordered[K, T]) Len() int

Len returns the number of items in the map.

func (*Ordered[K, T]) Range added in v0.143.0

func (m *Ordered[K, T]) Range(f func(key K, value T) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. TODO(bep) replace with iter.Seq2 when we bump go Go 1.24.

func (*Ordered[K, T]) Set added in v0.143.0

func (m *Ordered[K, T]) Set(key K, value T)

Set sets the value for the given key. Note that insertion order is not affected if a key is re-inserted into the map.

func (*Ordered[K, T]) Values added in v0.143.0

func (m *Ordered[K, T]) Values() []T

Values returns the values in the order they were added.

type OrderedIntSet added in v0.153.0

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

func NewOrderedIntSet added in v0.153.0

func NewOrderedIntSet(vals ...int) *OrderedIntSet

NewOrderedIntSet creates a new OrderedIntSet. Note that this is backed by https://github.com/bits-and-blooms/bitset

func (*OrderedIntSet) Clone added in v0.153.0

func (m *OrderedIntSet) Clone() *OrderedIntSet

func (*OrderedIntSet) ForEachKey added in v0.153.0

func (m *OrderedIntSet) ForEachKey(yield func(int) bool) bool

The reason we don't use iter.Seq is https://github.com/golang/go/issues/69015 This is 70% faster than using iter.Seq2[int, int] for the keys. It returns false if the iteration was stopped early.

func (*OrderedIntSet) Has added in v0.153.0

func (m *OrderedIntSet) Has(key int) bool

func (*OrderedIntSet) IsSuperSet added in v0.153.0

func (m *OrderedIntSet) IsSuperSet(other *OrderedIntSet) bool

func (*OrderedIntSet) KeysSorted added in v0.153.0

func (m *OrderedIntSet) KeysSorted() []int

KeysSorted returns the keys in sorted order.

func (*OrderedIntSet) Len added in v0.153.0

func (m *OrderedIntSet) Len() int

func (*OrderedIntSet) Next added in v0.153.0

func (m *OrderedIntSet) Next(i int) int

Next returns the next key in the set possibly including the given key. It returns -1 if the key is not found or if there are no keys greater than the given key.

func (*OrderedIntSet) Set added in v0.153.0

func (m *OrderedIntSet) Set(key int)

Set sets the value for the given key. Note that insertion order is not affected if a key is re-inserted into the set.

func (*OrderedIntSet) SetFrom added in v0.153.0

func (m *OrderedIntSet) SetFrom(other *OrderedIntSet)

SetFrom sets the values from another OrderedIntSet.

func (*OrderedIntSet) String added in v0.153.0

func (m *OrderedIntSet) String() string

func (*OrderedIntSet) Values added in v0.153.0

func (m *OrderedIntSet) Values() *bitset.BitSet

func (*OrderedIntSet) Words added in v0.153.0

func (m *OrderedIntSet) Words() []uint64

Words returns the bitset as array of 64-bit words, giving direct access to the internal representation. It is not a copy, so changes to the returned slice will affect the bitset. It is meant for advanced users.

type Params added in v0.60.0

type Params map[string]any

Params is a map where all keys are lower case.

func CloneParamsDeep added in v0.152.0

func CloneParamsDeep(m Params) Params

CloneParamsDeep does a deep clone of the given Params, meaning that any nested Params will be cloned as well.

func CreateNestedParamsFromSegements added in v0.153.0

func CreateNestedParamsFromSegements(target Params, keySegments ...string) Params

CreateNestedParamsFromSegements creates empty nested maps for the given keySegments in the target map.

func CreateNestedParamsSepString added in v0.153.0

func CreateNestedParamsSepString(keyStr, separator string, target Params) Params

CreateNestedParamsSepString creates empty nested maps for the given keyStr in the target map It returns the last map created.

func MustToParamsAndPrepare added in v0.84.1

func MustToParamsAndPrepare(in any) Params

MustToParamsAndPrepare calls ToParamsAndPrepare and panics if it fails.

func PrepareParamsClone added in v0.143.0

func PrepareParamsClone(m Params) Params

PrepareParamsClone is like PrepareParams, but it does not modify the input.

func SetNestedParamIfNotSet added in v0.153.0

func SetNestedParamIfNotSet(keyStr, separator string, value any, target Params) Params

SetNestedParamIfNotSet sets the value for the given keyStr in the target map if it does not exist. It assumes that all but the last key in keyStr is a Params map or should be one.

func ToParamsAndPrepare added in v0.84.0

func ToParamsAndPrepare(in any) (Params, error)

ToParamsAndPrepare converts in to Params and prepares it for use. If in is nil, an empty map is returned. See PrepareParams.

func (Params) DeleteMergeStrategy added in v0.84.0

func (p Params) DeleteMergeStrategy() bool

For internal use.

func (Params) GetMergeStrategy added in v0.84.0

func (p Params) GetMergeStrategy() (ParamsMergeStrategy, bool)

For internal use.

func (Params) GetNested added in v0.112.0

func (p Params) GetNested(indices ...string) any

GetNested does a lower case and nested search in this map. It will return nil if none found. Make all of these methods internal somehow.

func (Params) IsZero added in v0.84.2

func (p Params) IsZero() bool

IsZero returns true if p is considered empty.

func (Params) SetMergeStrategy added in v0.112.0

func (p Params) SetMergeStrategy(s ParamsMergeStrategy)

For internal use.

type ParamsMergeStrategy added in v0.84.0

type ParamsMergeStrategy string

ParamsMergeStrategy tells what strategy to use in Params.Merge.

const (
	// Do not merge.
	ParamsMergeStrategyNone ParamsMergeStrategy = "none"
	// Only add new keys.
	ParamsMergeStrategyShallow ParamsMergeStrategy = "shallow"
	// Add new keys, merge existing.
	ParamsMergeStrategyDeep ParamsMergeStrategy = "deep"

	MergeStrategyKey = "_merge"
)

type SliceCache added in v0.123.0

type SliceCache[T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SliceCache is a simple thread safe cache backed by a map.

func NewSliceCache added in v0.123.0

func NewSliceCache[T any]() *SliceCache[T]

func (*SliceCache[T]) Append added in v0.123.0

func (c *SliceCache[T]) Append(key string, values ...T)

func (*SliceCache[T]) Get added in v0.123.0

func (c *SliceCache[T]) Get(key string) ([]T, bool)

func (*SliceCache[T]) Reset added in v0.123.0

func (c *SliceCache[T]) Reset()

Jump to

Keyboard shortcuts

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