Skip to content

Commit c0efb5b

Browse files
committed
[#90] Match identifiers correctly when their type has changed
e.g. schema says string, input is integer, output is string. Previously merging would break in this case.
1 parent 83a32fa commit c0efb5b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

flattentool/input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def unflatten(self):
167167
root_id_or_none = line[self.root_id] if self.root_id else None
168168
unflattened = unflatten_main_with_parser(self.parser, line, self.timezone)
169169
try:
170-
merge(main_sheet_by_ocid[root_id_or_none][line.get('id')], unflattened)
170+
merge(main_sheet_by_ocid[root_id_or_none][unflattened.get('id')], unflattened)
171171
except KeyError:
172172
pass
173173
# FIXME add an appropriate warning here

flattentool/tests/test_input_SpreadsheetInput_unflatten_mulitplesheets.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,58 @@ def test_basic_two_sub_sheets(self):
486486
]
487487
}
488488
]
489+
490+
491+
from flattentool.schema import SchemaParser
492+
493+
def test_with_schema():
494+
spreadsheet_input = ListInput(
495+
sheets={
496+
'custom_main': [
497+
{
498+
'ocid': 1,
499+
'id': 2,
500+
'testA': 3
501+
}
502+
],
503+
'sub': [
504+
{
505+
'ocid': 1,
506+
'id': 2,
507+
'testR/testB': 4 # test that we can infer this an array from schema
508+
}
509+
]
510+
},
511+
main_sheet_name='custom_main')
512+
spreadsheet_input.read_sheets()
513+
514+
parser = SchemaParser(
515+
root_schema_dict={
516+
'properties': {
517+
'id': {
518+
'type': 'string',
519+
},
520+
'testR': {
521+
'type': 'array',
522+
'items': {
523+
'type': 'object'
524+
}
525+
},
526+
}
527+
},
528+
main_sheet_name='custom_main',
529+
root_id='ocid',
530+
rollup=True
531+
)
532+
parser.parse()
533+
spreadsheet_input.parser = parser
534+
assert list(spreadsheet_input.unflatten()) == [{
535+
'ocid': 1,
536+
'id': '2', # check that we join correctly when this gets converted to a
537+
# string because of the schema type
538+
'testA': 3,
539+
'testR': [{
540+
'testB': 4
541+
}]
542+
}]
543+

0 commit comments

Comments
 (0)