def _merge_option_dicts(source, options_a, options_b, ref_doc_a, ref_doc_b, errors=[]): # expand the options dicts collecting their replaced refs ref_hashes = {} inline_refs(options_a, ref_doc_a, source, ref_hashes) inline_refs(options_b, ref_doc_b, source, ref_hashes) # do the merge set_a = OptionsSet(options_a) set_b = OptionsSet(options_b) try: merged_options = set_a.merge(set_b).store except OptionsExceptionSetWithException as e: merged_options = e.set.store for ex_info in e.exception_infos: key = ex_info[0] values = ex_info[1] msg = """***** Merge Error **** Merging: type: {type_a} path: {path_a} with type: {type_b} path: {path_b} Gave an empty result for the key: {key} When using these values: {values} """.format(type_a=ref_doc_a.get("type"), type_b=ref_doc_b.get("type"), path_a=ref_doc_a.get("path"), path_b=ref_doc_b.get("path"), key=key, values="\n".join( [utils.json_dumps(v.as_json()) for v in values])) errors.append(msg) # un merge unchanged refs by looking at the ref_hashes restore_unchanged_refs(merged_options, ref_hashes) return merged_options
def _merge_option_dicts(source, options_a, options_b, ref_doc_a, ref_doc_b, errors=[]): # expand the options dicts collecting their replaced refs ref_hashes = {} inline_refs(options_a, ref_doc_a, source, ref_hashes) inline_refs(options_b, ref_doc_b, source, ref_hashes) # do the merge set_a = OptionsSet(options_a) set_b = OptionsSet(options_b) try: merged_options = set_a.merge(set_b).store except OptionsExceptionSetWithException as e: merged_options = e.set.store for ex_info in e.exception_infos: key = ex_info[0] values = ex_info[1] msg = """***** Merge Error **** Merging: type: {type_a} path: {path_a} with type: {type_b} path: {path_b} Gave an empty result for the key: {key} When using these values: {values} """.format(type_a=ref_doc_a.get("type"), type_b=ref_doc_b.get("type"), path_a=ref_doc_a.get("path"), path_b=ref_doc_b.get("path"), key=key, values = "\n".join([utils.json_dumps(v.as_json()) for v in values]) ) errors.append(msg) # un merge unchanged refs by looking at the ref_hashes restore_unchanged_refs(merged_options, ref_hashes) return merged_options
def test_wildcard(self): options_1 = { u"colour": { u"type": VALUE_TYPE_SET_STRING, u"name": u"colour", u"description": u"please choose one of the colours", VALUES_KEY_NAME: [ { u"type": VALUE_TYPE_SET_STRING, u"name": u"red", u"description": u"the colour red", u"image_uri": u"http://something.com/khgfdkyg.png", u"value": u"red", u"options":{ u"apples":{ u"type": VALUE_TYPE_SET_STRING, u"values": [u"cox", u"jazz", u"bramley"] } } }, { u"type": VALUE_TYPE_SET_STRING, u"name": u"blue", u"description": u"the colour blue", u"image_uri": u"http://something.com/khgfdkyg.png", u"value": u"blue" } ] } } options_2 = { u"*/apples": [u"cox", u"jazz"] } set1 = OptionsSet(options_1) set2 = OptionsSet(options_2) self.assertEqual(set1.matcher.options.keys(), [u"colour", u"colour/apples"]) self.assertEqual(set2.matcher.options.keys(), [u"*/apples"]) mega_store_1 = set1.mega_store(set2) mega_store_2 = set2.mega_store(set1) self.assertEqual(mega_store_1.keys(), [u"colour"]) merged = set1.merge(set2).store expected = [ { "description": "the colour blue", "image_uri": "http://something.com/khgfdkyg.png", "name": "blue", "type": "set::string", "value": "blue" }, { "description": "the colour red", "image_uri": "http://something.com/khgfdkyg.png", "name": "red", "options": { "apples": [ "cox", "jazz" ] }, "type": "set::string", "value": "red" } ] self.assertEqual(expected, merged["colour"]["values"])
def test_wildcard(self): options_1 = { u"colour": { u"type": VALUE_TYPE_SET_STRING, u"name": u"colour", u"description": u"please choose one of the colours", VALUES_KEY_NAME: [{ u"type": VALUE_TYPE_SET_STRING, u"name": u"red", u"description": u"the colour red", u"image_uri": u"http://something.com/khgfdkyg.png", u"value": u"red", u"options": { u"apples": { u"type": VALUE_TYPE_SET_STRING, u"values": [u"cox", u"jazz", u"bramley"] } } }, { u"type": VALUE_TYPE_SET_STRING, u"name": u"blue", u"description": u"the colour blue", u"image_uri": u"http://something.com/khgfdkyg.png", u"value": u"blue" }] } } options_2 = {u"*/apples": [u"cox", u"jazz"]} set1 = OptionsSet(options_1) set2 = OptionsSet(options_2) self.assertEqual(set1.matcher.options.keys(), [u"colour", u"colour/apples"]) self.assertEqual(set2.matcher.options.keys(), [u"*/apples"]) mega_store_1 = set1.mega_store(set2) mega_store_2 = set2.mega_store(set1) self.assertEqual(mega_store_1.keys(), [u"colour"]) merged = set1.merge(set2).store expected = [{ "description": "the colour blue", "image_uri": "http://something.com/khgfdkyg.png", "name": "blue", "type": "set::string", "value": "blue" }, { "description": "the colour red", "image_uri": "http://something.com/khgfdkyg.png", "name": "red", "options": { "apples": ["cox", "jazz"] }, "type": "set::string", "value": "red" }] self.assertEqual(expected, merged["colour"]["values"])