package maps

import "github.com/gohugoio/hugo/common/maps"

Index

Functions

func CleanConfigStringMap

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

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

func CleanConfigStringMapString

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

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

func ConvertFloat64WithNoDecimalsToInt

func ConvertFloat64WithNoDecimalsToInt(m map[string]any)

ConvertFloat64WithNoDecimalsToInt converts float64 values with no decimals to int recursively.

func GetNestedParam

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

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

func LookupEqualFold

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

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

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

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

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

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

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

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

func ToStringMap

func ToStringMap(in any) map[string]any

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

func ToStringMapBool

func ToStringMapBool(in any) map[string]bool

ToStringMapBool converts in to bool.

func ToStringMapE

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

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

func ToStringMapString

func ToStringMapString(in any) map[string]string

ToStringMapString converts in to map[string]string.

func ToStringMapStringE

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

ToStringMapStringE converts in to map[string]string.

Types

type Cache

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

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

NewCache creates a new Cache.

func (*Cache[K, T]) Drain

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

func (*Cache[K, T]) ForEeach

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

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

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

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

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

func (*Cache[K, T]) Reset

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

func (*Cache[K, T]) Set

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

Set sets the given key to the given value.

type KeyParams

type KeyParams struct {
	Key    string
	Params Params
}

KeyParams is an utility struct for the WalkParams method.

type KeyRenamer

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

KeyRenamer supports renaming of keys in a map.

func NewKeyRenamer

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

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

func (KeyRenamer) Rename

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 Ordered

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

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

NewOrdered creates a new Ordered map.

func (*Ordered[K, T]) Clone

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

Clone creates a shallow copy of the map.

func (*Ordered[K, T]) Delete

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

Delete deletes the value for the given key.

func (*Ordered[K, T]) Get

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

Get gets the value for the given key.

func (*Ordered[K, T]) Hash

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

Hash calculates a hash from the values.

func (*Ordered[K, T]) Keys

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

Keys returns the keys in the order they were added.

func (*Ordered[K, T]) Len

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

Len returns the number of items in the map.

func (*Ordered[K, T]) Range

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

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

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

Values returns the values in the order they were added.

type Params

type Params map[string]any

Params is a map where all keys are lower case.

func MustToParamsAndPrepare

func MustToParamsAndPrepare(in any) Params

MustToParamsAndPrepare calls ToParamsAndPrepare and panics if it fails.

func PrepareParamsClone

func PrepareParamsClone(m Params) Params

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

func ToParamsAndPrepare

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

func (p Params) DeleteMergeStrategy() bool

For internal use.

func (Params) GetMergeStrategy

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

For internal use.

func (Params) GetNested

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

func (p Params) IsZero() bool

IsZero returns true if p is considered empty.

func (Params) SetMergeStrategy

func (p Params) SetMergeStrategy(s ParamsMergeStrategy)

For internal use.

type ParamsMergeStrategy

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 Scratch

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

Scratch is a writable context used for stateful build operations

func NewScratch

func NewScratch() *Scratch

NewScratch returns a new instance of Scratch.

func (*Scratch) Add

func (c *Scratch) Add(key string, newAddend any) (string, error)

Add will, for single values, add (using the + operator) the addend to the existing addend (if found). Supports numeric values and strings.

If the first add for a key is an array or slice, then the next value(s) will be appended.

func (*Scratch) Delete

func (c *Scratch) Delete(key string) string

Delete deletes the given key.

func (*Scratch) DeleteInMap

func (c *Scratch) DeleteInMap(key string, mapKey string) string

DeleteInMap deletes a value to a map with the given key in the Node context.

func (*Scratch) Get

func (c *Scratch) Get(key string) any

Get returns a value previously set by Add or Set.

func (*Scratch) GetSortedMapValues

func (c *Scratch) GetSortedMapValues(key string) any

GetSortedMapValues returns a sorted map previously filled with SetInMap.

func (*Scratch) Set

func (c *Scratch) Set(key string, value any) string

Set stores a value with the given key in the Node context. This value can later be retrieved with Get.

func (*Scratch) SetInMap

func (c *Scratch) SetInMap(key string, mapKey string, value any) string

SetInMap stores a value to a map with the given key in the Node context. This map can later be retrieved with GetSortedMapValues.

func (*Scratch) Values

func (c *Scratch) Values() map[string]any

Values returns the raw backing map. Note that you should just use this method on the locally scoped Scratch instances you obtain via newScratch, not .Page.Scratch etc., as that will lead to concurrency issues.

type SliceCache

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

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

func (*SliceCache[T]) Append

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

func (*SliceCache[T]) Get

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

func (*SliceCache[T]) Reset

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

type StoreProvider

type StoreProvider interface {
	// Store returns a Scratch that can be used to store temporary state.
	// Store is not reset on server rebuilds.
	Store() *Scratch
}

Source Files

cache.go maps.go ordered.go params.go scratch.go

Version
v0.144.2 (latest)
Published
Feb 19, 2025
Platform
linux/amd64
Imports
11 packages
Last checked
13 hours ago

Tools for package owners.