Skip to content

Fix encoder/decoder bugs when nested types are encoded as a flattened message#54

Open
fumoboy007 wants to merge 3 commits intohirotakan:masterfrom
fumoboy007:master
Open

Fix encoder/decoder bugs when nested types are encoded as a flattened message#54
fumoboy007 wants to merge 3 commits intohirotakan:masterfrom
fumoboy007:master

Conversation

@fumoboy007
Copy link
Copy Markdown

Commits

1. Add tests to verify encoding/decoding nested types.

Tested two cases of nested types:

  1. Nested type that is flattened in its encoded form.
  2. Flattened type that is nested in its encoded form.

Note that some tests for the first case are skipped via XCTSkip because they currently fail due to bugs in the encoder/decoder.

2. Fix encoder bug when flattening nested types.

For a nested type that is flattened during encoding, the outer type and the inner type both create a container at the same nesting level. However, MessagePackEncoder does not currently support this use case because it does the encoding when a container is deinitialized.

This change allows containers at the same nesting level to share the same underlying storage and defers the encoding until the latest point in the flow.

3. Fix decoder bug when unflattening unkeyed message into nested types.

Currently, MessagePackDecoder does the decoding when a caller requests a container. Decoding results are not cached.

For a flattened message that is decoded into nested types, the outer type and the inner type both create a container at the same nesting level. For an unkeyed message, the current approach does not work because the unkeyed container returned to the outer type and the inner type need to share the same underlying storage so that the current array index stays consistent.

This change allows containers at the same nesting level to share the same underlying storage, which includes the current array index for unkeyed containers. This approach also has the performance benefit of caching the result of the container split.

Tested two cases of nested types:
1. Nested type that is flattened in its encoded form.
2. Flattened type that is nested in its encoded form.

Note that some tests for the first case are skipped via `XCTSkip` because they currently fail due to bugs in the encoder/decoder.
For a nested type that is flattened during encoding, the outer type and the inner type both create a container at the same nesting level. However, `MessagePackEncoder` does not currently support this use case because it does the encoding when a container is deinitialized.

This change allows containers at the same nesting level to share the same underlying storage and defers the encoding until the latest point in the flow.
Currently, `MessagePackDecoder` does the decoding when a caller requests a container. Decoding results are not cached.

For a flattened message that is decoded into nested types, the outer type and the inner type both create a container at the same nesting level. For an unkeyed message, the current approach does not work because the unkeyed container returned to the outer type and the inner type need to share the same underlying storage so that the current array index stays consistent.

This change allows containers at the same nesting level to share the same underlying storage, which includes the current array index for unkeyed containers. This approach also has the performance benefit of caching the result of the container split.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant