Skip to content

Commit

Permalink
Merge pull request #26 from gburca/master
Browse files Browse the repository at this point in the history
Fix handling of repeating groups with padding
  • Loading branch information
kizzx2 authored Dec 20, 2024
2 parents 0e7016e + b2bcec5 commit 8465c42
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/dist/
/sbe.egg-info/
__pycache__/
.venv
.eggs
6 changes: 5 additions & 1 deletion sbe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,12 @@ def _walk_fields_encode(schema: Schema, fields: List[Union[Group, Field]],
f.blockLength, Cursor(0))
if block_length is None:
block_length = struct.calcsize("<" + ''.join(fmt1))
if f.blockLength:
assert f.blockLength >= block_length
if f.blockLength and f.blockLength > block_length:
fmt1.append(str(f.blockLength - block_length) + 'x')

dimension = {"numInGroup": len(obj[f.name]), "blockLength": block_length or f.blockLength or 0}
dimension = {"numInGroup": len(obj[f.name]), "blockLength": f.blockLength or block_length or 0}
dimension_fmt = _pack_format(schema, f.dimensionType)

fmt.extend(dimension_fmt)
Expand Down
2 changes: 1 addition & 1 deletion tests/dat/example-schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
</sbe:message>
<sbe:message name="TestBlockLength" id="3" blockLength="4">
<field name="year" id="1" type="ModelYear"/>
<group name="AGroup" id="2" dimensionType="groupSizeEncoding" blbockLength="6">
<group name="AGroup" id="2" dimensionType="groupSizeEncoding" blockLength="6">
<field name="numbers" id="1" type="someNumbers"/>
</group>
</sbe:message>
Expand Down
5 changes: 3 additions & 2 deletions tests/test_sbe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def test_blockLength():
{'numbers': 456}]})
# BlockHeader = 8b
# Body = 2b year + 2b padding
# GroupHeader = 4b
# Repeating group = 2 * (4b numbers + 2b padding)
expLen = 8 + 4 + 2*6
assert len(encoded) == expLen, "Encoded SBE not padded properly"
expLen = 8 + 4 + 4 + 2*6
assert len(encoded) == expLen, f"Encoded SBE not padded properly. Expected {expLen} actual {len(encoded)}"

decoded = s.decode(encoded)
assert decoded.value['year'] == 1990
Expand Down

0 comments on commit 8465c42

Please sign in to comment.