-
Notifications
You must be signed in to change notification settings - Fork 63
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
[FEATURE REQUEST] FieldAlignment to not just be stream based (but Parent / Field based) #208
Comments
I might be thinking about this wrong, but that feels a bit more like field "quantization" or something vs. alignment, no? Alignment to me is more like where you want the data to show up in the stream vs rules that the length should follow. Can you do this just using a value converter on the length binding that does a mod operation? I haven't actually tried it but it feels like that should work... |
I'll give it a try. My concern however is that if the Length says 25, the field content (ASCII'ish characters) is only 25 elements long. But because that's an odd number, it's padded with an additional byte. So it's definitely a padding / right alignment problem. But only within that particular field. It ignores the current offset into the stream. So my concerns with using FieldLength is: |
The Length tweaking didn't appear to work. But perhaps I was going about it wrong. It was easy enough to just create a custom IBinarySerializable implementation for it (just two fields). |
Yeah, you're right of course. It doesn't update the field. Back to your original point, doesn't the RelativePosition tell the position w.r.t. the parent? |
Yes. With respect to the Parent. |
Yeah I probably need to write some test code. I'm having trouble picturing it and why it's different. |
It's ok :) I did this by just reading the bytes, then mapping to Int16/32/64 as needed, and then using the BinaryPrimitives library to swap the endian if required. Not difficult, just adds a few extra lines of code which might have been clearer if just as read/written to stream. |
I have a situation in this CIP driver that I'm putting together where it wants to have a field padded to an even number of bytes. But this field might start at an odd offset in the stream, however the padding should still be only within the Field itself.
i.e.
If Data has 25 bytes, then it should be padded to be 26 bytes in total space (but the Length field should remain at 25).
In the current implementation, since the byte for Length provides an offset of 1 byte in the stream, the Data alignment is 'off'.
It's padding when it shouldn't.
I would propose to extend the FieldAlignmentAttribute with a FieldAlignmentScope which would have options of 'Stream' (default and current behaviour), 'Parent' which would align with regards to the offset inside the parent, 'Field' which would only align/pad internally to this Field (how I'd like it to operate).
Is there perhaps something that I'm missing that could already handle what I'm after? (without falling back to entirely custom serialization/deserialization).
It looks like the current Align(...) method only gets the BoundedStream, which has the stream.RelativePosition member.
The simplest approach I can think of would be to follow along with a .FieldRelativePosition, and then a .ParentRelativePosition along with a stack to hold previous .ParentRelativePositions (pushed each time descending into a class, and popped when coming back out).
This approach seems like it would be relatively expensive in performance though (3x increase in position tracking calculations).
The text was updated successfully, but these errors were encountered: