diff --git a/schemasheets/schemamaker.py b/schemasheets/schemamaker.py index 1e0789d..7ca4d21 100644 --- a/schemasheets/schemamaker.py +++ b/schemasheets/schemamaker.py @@ -73,7 +73,11 @@ def set_attr_via_path_accessor(obj: Union[dict, YAMLRoot], path: Union[str, List tok = toks[0] toks = toks[1:] logging.debug(f"[{depth}] Setting attr {tok} / {toks} in {obj} to {value}") - if isinstance(obj, dict): + if isinstance(obj, list): + new_dict = {} + set_attr_via_path_accessor(new_dict, path, value, depth=depth) + obj.append(new_dict) + elif isinstance(obj, dict): if not toks: obj[tok] = value else: @@ -286,14 +290,16 @@ def add_row(self, row: Dict[str, Any], table_config: TableConfig): raise ValueError(f'Cannot reset value for {k}, was {curr_val}, now {v}') if cc.settings.inner_key: obj_to_set = getattr(actual_element, cc.maps_to) - if isinstance(getattr(actual_element, cc.maps_to), list): + if isinstance(obj_to_set, list): if '|' in v: vs = v.split('|') else: vs = [v] + # not sure if this is a valid scenario, but raising error in case it is + if obj_to_set: + raise Exception(f'The case when list has multiple element set by inner_key is not implemented yet') for v1 in vs: set_attr_via_path_accessor(obj_to_set, cc.settings.inner_key, v1) - # setattr(actual_element, cc.maps_to, [{cc.settings.inner_key: v} for v in vs]) else: set_attr_via_path_accessor(obj_to_set, cc.settings.inner_key, v) # getattr(actual_element, cc.maps_to)[cc.settings.inner_key] = v diff --git a/tests/input/personinfo_anyof.tsv b/tests/input/personinfo_anyof.tsv new file mode 100644 index 0000000..cce7c97 --- /dev/null +++ b/tests/input/personinfo_anyof.tsv @@ -0,0 +1,9 @@ +record field key range any_of_range desc +> class slot identifier range any_of description +> inner_key: range + id yes string any identifier + description no string a textual description +Person n/a n/a a person,living or dead +Person id yes string identifier for a person +Person|Organization name no string full name +Person age no decimal|integer age in years diff --git a/tests/test_schemamaker.py b/tests/test_schemamaker.py index 7c20c21..2ef4d93 100644 --- a/tests/test_schemamaker.py +++ b/tests/test_schemamaker.py @@ -282,3 +282,17 @@ def test_load_table_config(): assert 'wikidata:Q215627' in person_cls.exact_mappings assert 'sdo:Person' in person_cls.exact_mappings + +def test_classes_slots_anyof(): + """testing any_of in the slots""" + sm = SchemaMaker() + schema = sm.create_schema(os.path.join(INPUT_DIR, 'personinfo_anyof.tsv')) + logging.info(f'SCHEMA={schema}') + logging.info(f'SCHEMA.cl={schema.classes}') + logging.info(f'SCHEMA.sl={schema.slots}') + yaml_dumper.dump(schema, to_file=os.path.join(OUTPUT_DIR, 'personinfo.yaml')) + yaml = yaml_dumper.dumps(schema) + logging.info(yaml) + person_cls = schema.classes['Person'] + assert person_cls.slot_usage["age"].any_of[0].range == "decimal" + assert person_cls.slot_usage["age"].any_of[1].range == "integer"