Skip to content

Collection functions

πŸ€– AI-Generated Content

This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.

pyvider.components.functions.collection_functions

Collection manipulation functions for arrays and objects.

Classes

Functions

lookup

lookup(
    map_to_search: dict[str, Any] | None,
    key: str,
    *defaults: Any,
) -> Any

Lookup a key in a map and return its value, or a default if the key doesn't exist.

Using *defaults allows us to distinguish between: - lookup(map, key) - no default provided, should error if key missing - lookup(map, key, None) - default is None - lookup(map, key, False) - default is False - lookup(map, key, 0) - default is 0

Source code in pyvider/components/functions/collection_functions.py
@register_function(name="lookup", summary="Performs a dynamic lookup into a map.")
@resilient()
def lookup(map_to_search: dict[str, Any] | None, key: str, *defaults: Any) -> Any:
    """
    Lookup a key in a map and return its value, or a default if the key doesn't exist.

    Using *defaults allows us to distinguish between:
    - lookup(map, key) - no default provided, should error if key missing
    - lookup(map, key, None) - default is None
    - lookup(map, key, False) - default is False
    - lookup(map, key, 0) - default is 0
    """
    if map_to_search is None:
        return None
    if key in map_to_search:
        logger.debug("Map lookup successful", key=key, map_size=len(map_to_search))
        return map_to_search[key]
    if defaults:  # Default was provided (even if falsy)
        default = defaults[0]
        logger.debug("Map lookup using default", key=key, has_default=True, default_value=default)
        return default
    logger.debug("Map lookup failed", key=key, available_keys=list(map_to_search.keys()))
    raise FunctionError(f'Invalid key for map lookup: key "{key}" does not exist in the map.')