Skip to content

Commit

Permalink
Only add orderings when there is a type string field
Browse files Browse the repository at this point in the history
  • Loading branch information
knokko committed Dec 12, 2024
1 parent b3d9af7 commit a65161a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 3 additions & 2 deletions helpor/dict_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ def add_orderings(target: dict) -> dict:
next_ordering = 0
for value in target.values():
if isinstance(value, dict):
value['ordering'] = next_ordering
next_ordering += 1
if 'type' in value and isinstance(value['type'], str):
value['ordering'] = next_ordering
next_ordering += 1
add_orderings(value)
return target
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ build-backend = "hatchling.build"

[project]
name = 'helpor'
version = '0.1.3'
version = '0.1.4'
requires-python = '>= 3.7'

21 changes: 13 additions & 8 deletions tests/test_dict_ordering.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ def test_dict_with_one_level(self):
original = {
'nope': None,
'first': {
'go': 'not deep'
'type': 'string'
},
'second': {},
'last': 1234
}
expected = {
'nope': None,
'first': {
'go': 'not deep',
'type': 'string',
'ordering': 0
},
'second': {
'ordering': 1
},
'second': {},
'last': 1234
}
self.assertEqual(expected, add_orderings(original))
Expand All @@ -45,7 +43,11 @@ def test_deeper_dict(self):
'hello': 'world'
},
'object2': {
'type': 'string',
'this is': 'deep enough'
},
'type': {
'type': 'number'
}
}
}
Expand All @@ -71,13 +73,16 @@ def test_deeper_dict(self):
'deeper': {
'object1': {
'hello': 'world',
'ordering': 0
},
'object2': {
'type': 'string',
'this is': 'deep enough',
'ordering': 1
'ordering': 0
},
'ordering': 0
'type': {
'type': 'number',
'ordering': 1
}
},
'ordering': 1
},
Expand Down

0 comments on commit a65161a

Please sign in to comment.