diff --git a/src/caoscrawler/converters/converters.py b/src/caoscrawler/converters/converters.py index 2f0e6b737bee601a817b4f91f9c7de557021e679..83d28cae211c8850e17f65066124aeb2bb64144c 100644 --- a/src/caoscrawler/converters/converters.py +++ b/src/caoscrawler/converters/converters.py @@ -169,13 +169,19 @@ Parameters ---------- value: Union[dict, str, list] - - If *str*, the value to be interpreted. E.g. "4", "hello" or "$a" etc. + - If *str*, the value to be interpreted. E.g. "4", "hello" or "$a" + etc. No unit is set and collection mode is determined from the + first character: + - '+' corresponds to "list" + - '*' corresponds to "multiproperty" + - everything else is "single" - If *dict*, it must have a ``value`` key and may ``unit``, and ``collection_mode``. The returned tuple is directly created from - the corresponding values whereby unit defaults to None and - collection_mode defaults to "single". - - If *list*, each element is checked for replacement and the resulting list will be used - as (list) value for the property + the corresponding values if they are given; ``unit`` defaults to + None and ``collection_mode`` is determined from ``value`` as + explained for the str case above. + - If *list*, each element is checked for replacement and the + resulting list will be used as (list) value for the property Returns ------- @@ -197,7 +203,7 @@ out: tuple raise NotImplementedError(f"This definition has no \"value\": {value}") propvalue = value["value"] if "unit" in value: - propunit = value["unit"] + propunit = replace_variables(value["unit"], values) # can be "single", "list" or "multiproperty" if "collection_mode" in value: collection_mode = value["collection_mode"] @@ -237,8 +243,6 @@ out: tuple return (propvalue, propunit, collection_mode) propvalue = replace_variables(propvalue, values) - if propunit: - propunit = replace_variables(propunit, values) return (propvalue, propunit, collection_mode) diff --git a/unittests/test_converters.py b/unittests/test_converters.py index f1a4b2c9d880e77a6761a8768c590b201f7b2079..eaebbc6e1341df25bf96835b363287261a3f1211 100644 --- a/unittests/test_converters.py +++ b/unittests/test_converters.py @@ -384,9 +384,13 @@ def test_variable_replacement(): # Unit specified in the same way as value: assert handle_value({"value": 5, "unit": "m"}, values) == (5, "m", "single") + assert handle_value({"value": 5, "unit": "${my_unit}"}, values) == (5, "m", "single") + assert handle_value({"value": "+5", "unit": "${my_unit}"}, values) == ("5", "m", "list") + assert handle_value({"value": "*5", "unit": "${my_unit}"}, values) == ("5", "m", "multiproperty") assert handle_value(["a", "b"], values) == (["a", "b"], None, "single") assert handle_value(["$a", "$b"], values) == ([4, "68"], None, "single") + assert handle_value({"value": ["$a", "$a"], "unit": "${my_unit}"}, values) == ([4, 4], "m", "single") def test_apply_transformers(converter_registry):