From 39b458231690254704a504be16dc19922cd712fc Mon Sep 17 00:00:00 2001 From: marshallmain <55718608+marshallmain@users.noreply.github.com> Date: Mon, 6 Apr 2020 16:28:35 -0400 Subject: [PATCH] Merge reusable location lists when merging a custom schema with an ECS schema (#751) --- CHANGELOG.next.md | 2 ++ scripts/schema_reader.py | 12 ++++++++++-- scripts/tests/test_schema_reader.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.next.md b/CHANGELOG.next.md index c7b2eb9f30..f53d23abff 100644 --- a/CHANGELOG.next.md +++ b/CHANGELOG.next.md @@ -48,6 +48,8 @@ Thanks, you're awesome :-) --> #### Improvements +* Add support for reusing offical fieldsets in custom schemas. #751 + #### Deprecated diff --git a/scripts/schema_reader.py b/scripts/schema_reader.py index 806efcc54f..01a8766a29 100644 --- a/scripts/schema_reader.py +++ b/scripts/schema_reader.py @@ -85,7 +85,7 @@ def schema_set_fieldset_prefix(schema): def schema_fields_as_dictionary(schema): """Re-nest the array of field names as a dictionary of 'fieldname' => { field definition }""" - field_array = schema.pop('fields') + field_array = schema.pop('fields', []) schema['fields'] = {} for order, field in enumerate(field_array): field['order'] = order @@ -114,7 +114,15 @@ def merge_schema_fields(a, b): raise ValueError('Schemas unmergeable: type {} does not match type {}'.format(a_type, b_type)) elif a_type not in ['object', 'nested']: print('Warning: dropping field {}, already defined'.format(key)) - elif 'fields' in b[key]: + continue + # reusable should only be found at the top level of a fieldset + if 'reusable' in b[key]: + a[key].setdefault('reusable', {}) + a[key]['reusable']['top_level'] = a[key]['reusable'].get( + 'top_level', False) or b[key]['reusable']['top_level'] + a[key]['reusable'].setdefault('expected', []) + a[key]['reusable']['expected'].extend(b[key]['reusable']['expected']) + if 'fields' in b[key]: a[key].setdefault('fields', {}) merge_schema_fields(a[key]['fields'], b[key]['fields']) diff --git a/scripts/tests/test_schema_reader.py b/scripts/tests/test_schema_reader.py index 7cc6e2729f..f94a571adc 100644 --- a/scripts/tests/test_schema_reader.py +++ b/scripts/tests/test_schema_reader.py @@ -257,6 +257,10 @@ def test_merge_schema_fields(self): fieldset1 = { 'test_fieldset': { 'name': 'test_fieldset', + 'reusable': { + 'top_level': False, + 'expected': ['location1, location2'] + }, 'fields': { 'test_field1': { 'field_details': { @@ -278,6 +282,10 @@ def test_merge_schema_fields(self): fieldset2 = { 'test_fieldset': { 'name': 'test_fieldset', + 'reusable': { + 'top_level': True, + 'expected': ['location3, location4'] + }, 'fields': { 'test_field1': { 'field_details': { @@ -299,6 +307,10 @@ def test_merge_schema_fields(self): expected = { 'test_fieldset': { 'name': 'test_fieldset', + 'reusable': { + 'top_level': True, + 'expected': ['location1, location2', 'location3, location4'] + }, 'fields': { 'test_field1': { 'field_details': {