def _yatiml_sweeten(cls, node: yatiml.Node) -> None: if node.get_attribute('model').is_scalar(type(None)): node.remove_attribute('model') if node.get_attribute('settings').is_scalar(type(None)): node.remove_attribute('settings') if len(node.get_attribute('settings').yaml_node.value) == 0: node.remove_attribute('settings') impl = node.get_attribute('implementations') if (impl.is_scalar(type(None)) or impl.is_mapping() and impl.is_empty()): node.remove_attribute('implementations') node.index_attribute_to_map('implementations', 'name', 'script') res = node.get_attribute('resources') if (res.is_scalar(type(None)) or res.is_mapping() and res.is_empty()): node.remove_attribute('resources') node.index_attribute_to_map('resources', 'name', 'num_cores')
def test_index_attribute_to_map1(class_node: yatiml.Node) -> None: class_node.index_attribute_to_map('index1', 'item_id') assert class_node.has_attribute('index1') new_mapping = class_node.get_attribute('index1') assert new_mapping.is_mapping() assert new_mapping.has_attribute('item1') item1_mapping = new_mapping.get_attribute('item1') assert item1_mapping.is_mapping() assert not item1_mapping.has_attribute('item_id') assert item1_mapping.has_attribute('price') assert new_mapping.has_attribute('item2') item2_mapping = new_mapping.get_attribute('item2') assert item2_mapping.is_mapping() assert not item2_mapping.has_attribute('item_id') assert item2_mapping.has_attribute('price') assert new_mapping.has_attribute('item3') item3_mapping = new_mapping.get_attribute('item3') assert item3_mapping.is_mapping() assert not item3_mapping.has_attribute('item_id') assert item3_mapping.has_attribute('price') assert item3_mapping.has_attribute('on_sale') # check that it fails silently if the attribute is missing or not a mapping class_node.index_attribute_to_map('non_existent_attribute', 'item_id') class_node.index_attribute_to_map('attr1', 'item_id')
def test_index_attribute_to_map2(class_node: yatiml.Node) -> None: class_node.index_attribute_to_map('index1', 'item_id', 'price') assert class_node.has_attribute('index1') new_mapping = class_node.get_attribute('index1') assert new_mapping.is_mapping() assert new_mapping.has_attribute('item1') item1 = new_mapping.get_attribute('item1') print(item1.yaml_node) assert item1.is_scalar(float) assert item1.get_value() == 100.0 assert new_mapping.has_attribute('item2') item2 = new_mapping.get_attribute('item2') assert item2.is_scalar(float) assert item2.get_value() == 200.0 assert new_mapping.has_attribute('item3') item3_mapping = new_mapping.get_attribute('item3') assert item3_mapping.is_mapping() assert not item3_mapping.has_attribute('item_id') assert item3_mapping.has_attribute('price') assert item3_mapping.has_attribute('on_sale')