-
Notifications
You must be signed in to change notification settings - Fork 4
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: setting lengths and field defaults #12
Comments
Well, the couple of points in this proposal still look unclear to me.
|
Here is a case where a compile time error should be raised because the default is not at the end of a struct: serializable:
type
DefaultFail* = object
bulletId*: int8
ownerId*: int32 as {default: 40}
bulletType*: int8 Here is what correct usage should look like because the defaults are the last fields: serializable:
type
DefaultFail* = object
bulletId*: int8
ownerId*: int32 as {default: 40}
bulletType*: int8 as {default: 87} I Hope that this makes sense regarding my second feature request, its been itching me to have something like this in NESM, I think that the usability would outweigh how expensive the feature itself would be. |
Another QoL thing that I thought about and would love to have implemented would be setting a field to be the Currently if you want to set a field to be the size of a field you must do something like this: serializable:
type
customdata = object
thing: int8
thing2: int32
serializablethingwdata = object
length: int32
data: customdata
var test: customdata
test.length = test.data.size But just imagine how slick NESM would be if you could reduce those extra lines of defining an object's length to just An example of how this might be used is this: serializable:
type
customdata = object
thing: int8
thing2: int32
serializablethingwdata = object
length: int32 as {sizeof: {}.data}
data: customdata
serializablethingallsize = object
length: int32 as {sizeof: {}}
data: customdata You don't have to implement this but this is a really cool thing that I'm sure others will truly enjoy. This really makes me want to learn how to write macros, I feel bad requesting all this stuff without the ability to help :L |
Well, NESM was designed as a tool to [de]serialize structures known at compile time. I wonder that kind of non-periodic(like seq) structure requires its size to be placed in header. Which protocol are you trying to implement? |
I'm currently trying to implement the bittorrent protocol for fun and its led me to think of QoL features such as the ones I outlined above. Originally I was remaking the protocol that www.realmofthemadgod.com uses for the game but I ended up being up unable to do that due to some packets having data that might have a custom value or a default value. For bittorrent the header is the length, int32, of the packet which has the value of just the size of the packet's data while realmofthemadgod's header has a length, int32, which is the total size of the packet. |
@xomachine I also initially thought that NESM was meant to be used for writing parsers for existing protocols (network and other). |
related to #12 Adds the deserialize proc overload that accepts mutable object to fill with deserialized content. If the deserialization was not complete (due to lack of data in the stream) the part of the object passed for which data has not obtained will be left untouched
I implemented the default values but in slightly different way. I think it is better to pass a prebaked object with default values to the deserialize function instead of setting them in the type declaration. Try it in the devel branch! @FedericoCeratto That is true, but I did not expect cases like this when started the developement of the library. So it is not quite easy to add the features related to fields autofilling without creating a mess in the source code (it is already quite complicated in my opinion). |
@xomachine Thank you for this I will check it out! |
Here are some enhancements that I feel would greatly improve the quality of life of those implementing certain protocols. It would be greatly beneficial if we were able to set the type used to prefix a string/seq natively without making a new type custom type. An example of how this could be implemented would be like this:
This would provide the user with a level of flexibility that is honestly so nice due to the simplicity of toggling said option.
Another feature which would be nice would be the ability to set the default value of a field, at least for me I'm re-implementing a certain games protocol and some packets might have additional bytes at the end which might get parsed into their respective variables. An example of this would be this:
This would give us the flexibility to give fields a default value other than 0 as is currently implemented in nim. This would allow us to parse packets which have the possibility of containing optional/additional data.
The text was updated successfully, but these errors were encountered: