Пример #1
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        if not context.expect_sequence():
            return UNDEFINED

        node = context.current_node()
        assert isinstance(node, SequenceNode)

        for child in node.value:
            result = context.load(field, child)
            if result is not UNDEFINED:
                return result

        return UNDEFINED
Пример #2
0
    def _load(self, context: ILoadingContext) -> Any:
        if not context.expect_sequence():
            return UNDEFINED

        node = context.current_node()
        result = []
        for item_node in node.value:
            item = context.load(self._item_field, item_node)
            if item is UNDEFINED:
                continue

            result.append(item)

        return result
Пример #3
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        if not context.expect_sequence():
            return UNDEFINED

        node = context.current_node()

        result: Optional[Union[Dict[str, Any], List[Any]]] = None
        for child in node.value:
            child_result = context.load(field, child)
            if child_result is UNDEFINED:
                continue

            if isinstance(child_result, dict):
                if result is None:
                    result = {}

                assert isinstance(result, dict)
                result.update(child_result)

            elif isinstance(child_result, list):
                if result is None:
                    result = []

                assert isinstance(result, list)
                result += child_result

            else:
                msg = _("Trying to merge invalid object {}, expected dict or "
                        "list")
                context.error(ErrorCode.VALUE_ERROR, msg, result)

        # If nothing it's to merge, return UNDEFINED
        if result is None:
            return UNDEFINED

        return result