maps

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapsRegistry

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

func NewRegistry

func NewRegistry() *MapsRegistry

NewRegistry creates a new instance of maps registry.

func (*MapsRegistry) Dict

func (mr *MapsRegistry) Dict(values ...any) map[string]any

Dict creates a dictionary from a list of keys and values.

Parameters:

values ...any - alternating keys and values.

Returns:

map[string]any - the created dictionary.

Example:

{{ dict "key1", "value1", "key2", "value2" }} // Output: {"key1": "value1", "key2": "value2"}

func (*MapsRegistry) Dig

func (mr *MapsRegistry) Dig(args ...any) (any, error)

Dig navigates through a nested dictionary structure using a sequence of keys and returns the value found at the specified path.

Parameters:

args ...any - a sequence of keys followed by a dictionary as the last argument.

Returns:

any - the value found at the nested key path or nil if any key in the path is not found.
error - an error if there are fewer than three arguments, if the last argument is not a dictionary, or if any key is not a string.

Example:

{{ dig "user", "profile", "name", {"user": {"profile": {"name": "John Doe"}}} }} // Output: "John Doe", nil
{{ dig "user.profile.age", {"user": {"profile": {"name": "John Doe"}}} }} // Output: nil, nil

func (*MapsRegistry) Get

func (mr *MapsRegistry) Get(args ...any) (any, error)

Get retrieves the value associated with the specified key from the dictionary.

Parameters:

key string - the key to look up.
dict map[string]any - the dictionary.

Returns:

any - the value associated with the key, or an empty string if the key does not exist.
error - protect against undesired behavior due to migration to new signature.

Example:

{{ {"key": "value"} | get "key" }} // Output: "value"

func (*MapsRegistry) HasKey

func (mr *MapsRegistry) HasKey(args ...any) (bool, error)

HasKey checks if the specified key exists in the dictionary.

Parameters:

key string - the key to look for.
dict map[string]any - the dictionary to check.

Returns:

bool - true if the key exists, otherwise false.
error - protect against undesired behavior due to migration to new signature.

Example:

{{ {"key": "value"} | hasKey "key" }} // Output: true

func (*MapsRegistry) Keys

func (mr *MapsRegistry) Keys(dicts ...map[string]any) []string

Keys retrieves all keys from one or more dictionaries.

Parameters:

dicts ...map[string]any - one or more dictionaries.

Returns:

[]string - a list of all keys from the dictionaries.

Example:

{{ keys {"key1": "value1", "key2": "value2"} }} // Output: ["key1", "key2"]

func (*MapsRegistry) LinkHandler

func (mr *MapsRegistry) LinkHandler(fh sprout.Handler) error

LinkHandler links the handler to the registry at runtime.

func (*MapsRegistry) Merge

func (mr *MapsRegistry) Merge(dest map[string]any, srcs ...map[string]any) (any, error)

Merge merges multiple source maps into a destination map without overwriting existing keys in the destination. If an error occurs during merging, it returns nil and the error.

Parameters:

dest map[string]any - the destination map to which all source map key-values are added.
srcs ...map[string]any - one or more source maps whose key-values are added to the destination.

Returns:

any - the merged destination map.
error - error if the merge fails.

Example:

{{ merge {}, {"a": 1, "b": 2}, {"b": 3, "c": 4}  }} // Output: {"a": 1, "b": 2, "c": 4}, nil

func (*MapsRegistry) MergeOverwrite

func (mr *MapsRegistry) MergeOverwrite(dest map[string]any, srcs ...map[string]any) (any, error)

MergeOverwrite merges multiple source maps into a destination map, overwriting existing keys in the destination. If an error occurs during merging, it returns nil and the error.

Parameters:

dest map[string]any - the destination map to which all source map key-values are added.
srcs ...map[string]any - one or more source maps whose key-values are added to the destination, potentially overwriting existing keys.

Returns:

any - the merged destination map with overwritten values where applicable.
error - error if the merge fails.

Example:

{{ mergeOverwrite {}, {"a": 1, "b": 2}, {"b": 3, "c": 4} }} // Output: {"a": 1, "b": 3, "c": 4}, nil

func (*MapsRegistry) Omit

func (mr *MapsRegistry) Omit(args ...any) (map[string]any, error)

Omit creates a new dictionary by excluding specified keys from the original dictionary.

Parameters:

dict map[string]any - the source dictionary.
keys ...string - the keys to exclude from the new dictionary.

Returns:

map[string]any - a dictionary without the omitted keys.
error - protect against undesired behavior due to migration to new signature.

Example:

{{ $d := dict "key1" "value1" "key2" "value2" "key3" "value3" }}
{{ omit $d "key1" "key3" }} // Output: {"key2": "value2"}

func (*MapsRegistry) Pick

func (mr *MapsRegistry) Pick(args ...any) (map[string]any, error)

Pick creates a new dictionary containing only the specified keys from the original dictionary.

Parameters:

keys ...string - the keys to include in the new dictionary.
dict map[string]any - the source dictionary.

Returns:

map[string]any - a dictionary containing only the picked keys and their values.
error - protect against undesired behavior due to migration to new signature.

Example:

{{ $d := dict "key1" "value1" "key2" "value2" "key3" "value3" }}
{{ $d | pick "key1" "key3" }} // Output: {"key1": "value1", "key3": "value3"}

func (*MapsRegistry) Pluck

func (mr *MapsRegistry) Pluck(key string, dicts ...map[string]any) []any

Pluck extracts values associated with a specified key from a list of dictionaries.

Parameters:

key string - the key to pluck values for.
dicts ...map[string]any - one or more dictionaries.

Returns:

[]any - a list of values associated with the key from each dictionary.

Example:

{{ $d1 := dict "key" "value1"}}
{{ $d2 := dict "key" "value2" }}
{{ pluck "key"	$d1 $d2 }} // Output: ["value1", "value2"]

func (*MapsRegistry) RegisterAliases added in v0.6.0

func (mr *MapsRegistry) RegisterAliases(aliasesMap sprout.FunctionAliasMap) error

func (*MapsRegistry) RegisterFunctions

func (mr *MapsRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error

RegisterFunctions registers all functions of the registry.

func (*MapsRegistry) RegisterNotices added in v0.6.0

func (mr *MapsRegistry) RegisterNotices(notices *[]sprout.FunctionNotice) error

func (*MapsRegistry) Set

func (mr *MapsRegistry) Set(args ...any) (map[string]any, error)

Set adds or updates a key with a specified value in the dictionary.

Parameters:

key string - the key to set.
value any - the value to associate with the key.
dict map[string]any - the dictionary.

Returns:

map[string]any - the updated dictionary.
error - protect against undesired behavior due to migration to new signature.

Example:

{{ {"key": "oldValue"} | set "key", "newValue" }} // Output: {"key": "newValue"}

func (*MapsRegistry) Uid

func (mr *MapsRegistry) Uid() string

Uid returns the unique identifier of the registry.

func (*MapsRegistry) Unset

func (mr *MapsRegistry) Unset(args ...any) (map[string]any, error)

Unset removes a key from the dictionary.

Parameters:

key string - the key to remove.
dict map[string]any - the dictionary.

Returns:

map[string]any - the dictionary after removing the key.
error - protect against undesired behavior due to migration to new signature.

Example:

{{ {"key": "value"} | unset "key" }} // Output: {}

func (*MapsRegistry) Values

func (mr *MapsRegistry) Values(dicts ...map[string]any) []any

Values retrieves all values from one or more dictionaries.

Parameters:

dict map[string]any - the dictionary.

Returns:

[]any - a list of all values from the dictionary.

Example:

{{ values {"key1": "value1", "key2": "value2"} }} // Output: ["value1", "value2"]

Jump to

Keyboard shortcuts

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