fix: rewrite decode_struct so struct decoding actually works#14
Merged
Conversation
The previous decode_struct never read the map header, never read field keys, fed each child decode call a re-sliced buffer without resetting d.pos, and assigned via val[field.name] = ... which does not compile on structs in current V. Decoding any struct produced "invalid integer descriptor byte 117" (the 'u' from a field name being treated as a value descriptor — see #12) or a compile error. Rewrite to mirror the encoder's wire layout: read the map header, iterate map_len entries, read each key as a msgpack string, look up the matching struct field by name (or by @[codec: 'alias'] attr), and decode the value into it. Extract the type dispatch from decode[T] into a private decode_value helper so containers can decode children without clobbering d.buffer / d.pos. Uncomment the struct round-trip assertion and add fresh round-trips that don't depend on the encoder's chosen integer width. Fixes #12.
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #12. Struct decoding never worked: the previous
decode_structd.decode(data[d.pos..], mut field_val)— re-slicing the buffer without resettingd.pos, sonext()read from the wrong offset,val[field.name] = field_val, which doesn't compile against current V (`type 'T' does not support indexing`).The user-visible symptom in #12 was `invalid integer descriptor byte 117` —
0x75 = 'u', the first byte of the key\"used_ids\"being misinterpreted as an integer descriptor.Rewrite to mirror the encoder's wire layout:
d.bdby the priornext()) →map_len.next()→ decode the key as a msgpack string →next()→ look up the matching field by name (honoring@[codec: 'alias']attrs the encoder writes) → decode the value into it via comptime field assignment.Extracted the type-dispatch out of
decode[T]into a privatedecode_valuehelper so containers can decode children without clobberingd.buffer/d.pos.Also uncomments the previously-disabled struct round-trip assertion in
decode_test.vand adds round-trips that don't depend on the encoder's chosen integer width.Test plan
v test .— all 3 test files pass (encode, decode, decode_to_json), including the previously-commented struct round-tripmsgpack.decode[Map](msgpack.encode(Map{used_ids: 1}))returnsMap{used_ids: 1}instead of erroring with byte 117Struct{'John', 30}andStruct{'', 0}