Comments (1)
Thanks for the quick update, but unfortunately, since it still doesn't unpack the value upon a non-existing field, the next iterations will be unpacking the value as if it was the name, and if this value and the rest of the values are not strings, it will skip the rest of the map (while leaving the last value in MsgStream). If there are string values, it will try to use the current map value as name
, and if it fails to find name
in val again, it will return using name
as name, correctly, except i
will be one over what it should be, thus truncating the end of the map (but still leaving the leftover pairs in the MsgStream), and possibly wrecking havoc on later values.
proc unpack_type*[ByteStream; T: tuple|object](s: ByteStream, val: var T) =
template dry_and_wet(): untyped =
when defined(msgpack_obj_to_map):
let len = s.unpack_map()
var name: string
for i in 0..len-1:
if not s.is_string:
s.skip_msg()
s.skip_msg()
continue
unpack_proxy(name) # it will try to unpack the map value here on next iteration
for field, value in fieldPairs(val):
if field == name:
unpack_proxy(value) # It will still skip this line if name is not found in val
What should be done is, if unpack_proxy(value)
is not called, s.skip_msg()
must be called.
Additional info: the reason your test passed is because your map has length 4, your first non-existing field's value is a string, and you don't check the last value of the map (since this last value will be skipped/truncated).
var a = anyMap()
a[anyInt(123)] = anyString("non-string-field") # this will be skipped, but will offset i to one over, which will ultimately, stop without unpacking the last item in map
a["name"] = anyString("apple")
a["color"] = anyInt(1001)
a["someInt"] = anyInt(123) # this last item will not be unpacked
var s = MsgStream.init(fromAny(a), MSGPACK_OBJ_TO_MAP)
var x = s.unpack(Fruit)
check x.name == "apple"
check x.color == 1001
# No someInt check, which would have failed with the default value, since unpacking was not performed
Edit: Created a PR, along with a test that should catch this scenario.
from msgpack4nim.
Related Issues (20)
- Error: undeclared field: 'data' HOT 2
- Custom pack_type handlers for distinct types HOT 5
- Error: undeclared field: 'data' HOT 9
- using nim-serialization framework
- implement compile time test
- Support unpacking object variants HOT 2
- please tag a release in github HOT 3
- Unable to create helper proc HOT 2
- Package 'msgpack4nim' has an incorrect structure HOT 1
- Defining pack_type and unpack_type for templated type does not call the specialized version HOT 1
- [Question] Reading from a file HOT 2
- Add the implementation to the msgpack website HOT 3
- Can't use `pack` from inside a template in another module without import msgpack4nim HOT 3
- outputStream(p: Process) in osproc in standard library cannot call peekChar HOT 4
- Exception for int64 when a field is DateTime HOT 3
- Warnings when unpacking to an object with range properties HOT 1
- unpack_items_imp uses same address
- Make MsgStream.writeData public, please HOT 2
- Unpack a seq of ref Object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack4nim.