-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[octetsream-codec] fix serialization and deserialization of nested objects #1262
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1262 +/- ##
==========================================
+ Coverage 77.81% 77.91% +0.10%
==========================================
Files 84 84
Lines 17523 17602 +79
Branches 1781 1810 +29
==========================================
+ Hits 13635 13715 +80
- Misses 3853 3854 +1
+ Partials 35 33 -2 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in general it is fine...
Please find below a suggested improvement
@@ -542,6 +543,7 @@ export default class OctetstreamCodec implements ContentCodec { | |||
throw new Error("Missing 'length' parameter necessary for write"); | |||
} | |||
|
|||
const offset = schema["ex:bitOffset"] !== undefined ? parseInt(schema["ex:bitOffset"]) : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to to a more sophisticated check.. not only for undefined but also NaN and/or invalid parameters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a check in the last commit.
The bytesToValue
and valueTobytes
functions could probably check most of the DataSchema values and the length
parameter more thoroughly as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to introduce the checks it would be fine by me.. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some more checks (and tests making sure the checks work) in my last commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A "simpler" approach could have been a helper method that does the check (since I think the are almost all the same). Anyhow, fine like this as well 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to go, thanks for adding tests too!
After using the octetstream codec's object serialization and deserialization for a while now, I discovered a bug when handling nested objects:
When serializing values, the object's bit-offset would not be passed on to the nested serialization call.
For example (added to the tests in this PR, too):
Serializing an object like
with the following DataSchema
I would expect:
0000 1000 0001 0000
, i.e.Buffer[8, 16]
, but the current implementation returnsBuffer[24, 0]
because it does not passex:bitOffset
offlags2
and therefore sets the bits offlags2
in the same byte asflags1
writing0001 1000 0000 0000
.The issue when deserializing nested objects was that
bytesToValue
would only pass the bytes fromoffset
tooffset
+length
toobjectToValue
without adjusting thelength
parameter. Therefore, deserializing nested objects would throw an error:I added a test case and a fix for this, too.