Esempio n. 1
0
def handle_attribute(
    collection: Union[Dict[str, Any], List[Any]],
    cfg: dict,
) -> ResultE[MapValue]:
    """Handle one attribute with mappings, ifs, casting and default value.

    :param collection: The collection of data to find data in
    :type collection: Union[Dict[str, Any], List[Any]]

    :param configuration: :term:`configuration` data to use when mapping
    :type configuration: Dict[str, Any]

    :return: Success/Failure containers
    :rtype: MapValue

    configuration expected to look like this:

    .. code-block:: json

        {
            "mappings": [],  # array of mapping objects
            "separator": None,
            "if_statements": [],  # array of if statement objects
            "casting": {}  # casting object, for casting types
            "default": "default value"
        }

    flow description:

    Map all objects in cfg[MAPPINGS] ->
    Apply separator to values if there are more than 1
    Failure -> fix to Success(None)
    Apply if statements
    Success -> Cast Value
    Failure -> apply default value

    Return Result
    """
    mapped_values = [
        mapped.unwrap() for mapped in [
            handle_mapping(collection, mapping)
            for mapping in cfg.get(MAPPINGS, [])
        ] if is_successful(mapped)
    ]

    # partially declare if statement and casting functions
    ifs = partial(apply_if_statements, if_objects=cfg.get(IF_STATEMENTS, []))
    cast = partial(apply_casting, casting=cfg.get(CASTING, {}))

    return flow(
        apply_separator(mapped_values, separator=cfg.get(SEPARATOR, '')),
        fix(lambda _: None),  # type: ignore
        bind(ifs),
        bind(cast),
        rescue(lambda _: apply_default(default=cfg.get(DEFAULT)), ),
    )
Esempio n. 2
0
def handle_mapping(
    collection: Union[Dict[str, Any], List[Any]],
    cfg: Dict[str, Any],
) -> ResultE[MapValue]:
    """Find data in path and apply if statements or default value.

    .. versionadded:: 0.0.1

    :param configuration: :term:`configuration` data to use when mapping
    :type configuration: Dict[str, Any]

    :param collection: The collection of data to find data in
    :type collection: Union[Dict[str, Any], List[Any]]

    :return: Success/Failure containers
    :rtype: GoResult

    configuration expected to look like this:

    .. code-block:: json
        {
            "path": [],
            "if_statementss": [{}, {}],
            "default": 'val'
        }

    Flow description:

    find data from path or None ->
    apply if statements ->
    return default value if Failure else mapped value
    """
    return flow(
        collection,
        partial(fetch_data_by_keys, path=cfg.get(PATH, [])),
        fix(lambda _: None),  # type: ignore
        bind(partial(
            apply_regexp,
            regexp=cfg.get(REGEXP, {}),
        ), ),
        fix(lambda _: None),  # type: ignore
        map_(partial(
            apply_slicing,
            slicing=cfg.get(SLICING, {}),
        )),
        bind(
            partial(
                apply_if_statements,
                if_objects=cfg.get(IF_STATEMENTS, []),
            )),
        rescue(  # type: ignore
            lambda _: apply_default(cfg.get(DEFAULT)), ),
    )
Esempio n. 3
0
def test_apply_default():
    """Test if we get a default value."""
    assert apply_default(None, 'default').unwrap() == 'default'
Esempio n. 4
0
def test_bad_mapped_value():
    """Test if we get a Failure when we give bad mapped value."""
    test = apply_default(['array'], None)
    assert not is_successful(test)
    assert 'Unable to give default value' in str(test.failure())
Esempio n. 5
0
def test_no_values():
    """Test returns Failure."""
    test = apply_default(None, None)
    assert not is_successful(test)
Esempio n. 6
0
def test_no_default_value():
    """Test value returned when exists."""
    assert apply_default('val', None).unwrap() == 'val'