Skip to content

Commit

Permalink
Merge reusable location lists when merging a custom schema with an EC…
Browse files Browse the repository at this point in the history
…S schema (#751)
  • Loading branch information
marshallmain authored Apr 6, 2020
1 parent 596538f commit 39b4582
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Thanks, you're awesome :-) -->

#### Improvements

* Add support for reusing offical fieldsets in custom schemas. #751

#### Deprecated


Expand Down
12 changes: 10 additions & 2 deletions scripts/schema_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])

Expand Down
12 changes: 12 additions & 0 deletions scripts/tests/test_schema_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -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': {
Expand All @@ -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': {
Expand Down

0 comments on commit 39b4582

Please sign in to comment.