def test_unique_list(input_value, output): assert unique_list(input_value) == output assert unique_list(unique_list(input_value)) == unique_list(input_value)
def merge_fhir_json(filepath: str, new_cs: CodeSystem): existing_cs = CodeSystem.parse_file(filepath) click.echo(f"there is an existing file at {filepath}, merging...") validation_errors = validate_attributes_match(existing_cs, new_cs) if len(validation_errors) > 0: click.echo("there were validation messages when merging:\n " + ',\n '.join(validation_errors), err=True, color=True) return None new_codes = list(c.code for c in new_cs.concept) concept_missing_in_existing = [ concept for concept in existing_cs.concept if concept.code not in new_codes ] if len(concept_missing_in_existing) > 0: click.echo("codes missing in the existing CodeSystem: " + ", ".join(f"'{c.code}'='{c.display}'" for c in concept_missing_in_existing)) click.echo("") existing_codes = list(c.code for c in existing_cs.concept) concept_missing_in_new = [ concept for concept in new_cs.concept if concept.code not in existing_codes ] if len(concept_missing_in_new) > 0: click.echo("codes missing in the new CodeSystem: " + ", ".join(f"'{c.code}'='{c.display}'" for c in concept_missing_in_new)) click.echo("") all_codes = unique_list(new_codes + existing_codes) common_codes = list(c for c in all_codes if c in existing_codes and c in new_codes) concepts_in_both_old = { c.code: c for c in existing_cs.concept if c in common_codes } concepts_in_both_new = { c.code: c for c in new_cs.concept if c in common_codes } merge_errors = [] for code in concepts_in_both_old.keys(): assert (code in concepts_in_both_new.keys()) old_concept: CodeSystemConcept = concepts_in_both_old[code] new_concept: CodeSystemConcept = concepts_in_both_new[code] if old_concept.display != new_concept.display: merge_errors.append( f"code '{code}' has different display, old '{old_concept.display}' vs new '{new_concept.display}'" ) old_parent = None new_parent = None if "property" in old_concept.json(): old_parent = [ p for p in old_concept.property if p.code == "parent" ] if "property" in new_concept.json(): new_parent = [ p for p in new_concept.property if p.code == "parent" ] if old_parent != None and new_parent == None or old_parent == None and new_parent != None: merge_errors.append( f"unhandled parent for code '{code}' in only one code system, please handle." ) elif old_parent != None and new_parent != None and len( old_parent) > 0 and len(new_parent) > 0: if old_parent[0].valueCode != new_parent[0].valueCode: merge_errors.append( f"code '{code}' has different parent value, old '{old_parent.valueCode}' vs new '{new_parent.valueCode}'" ) if len(merge_errors) > 0: click.echo("there were differences detected in the concepts: \n " + "\n ".join(merge_errors), err=True, color=True) return None existing_cs.concept.extend(concept_missing_in_existing) return existing_cs