Add YamlReader.CurrentKey for converter context during deserialization#136
Closed
fdcastel wants to merge 1 commit into
Closed
Add YamlReader.CurrentKey for converter context during deserialization#136fdcastel wants to merge 1 commit into
fdcastel wants to merge 1 commit into
Conversation
Expose the most recent mapping key via YamlReader.CurrentKey so that custom YamlConverter implementations can perform context-dependent transformations. Built-in dictionary and object converters now set CurrentKey immediately before reading each value and restore the previous key afterward, ensuring proper nesting behavior for nested structures. This enables scenarios like: - Replacing placeholders (\) with the actual dictionary key - Context-aware validation based on the property name - Key-dependent deserialization logic in custom converters
Contributor
Author
|
Another idea that initially seemed good, but now I’m starting to reconsider. Thoughts? |
Owner
|
I have a couple of remarks:
So, it's probably not the right approach. I would think a more generalized approach of having a "stack" of path items accessible from reader/writer. A "path item" would be a lightweight struct with a kind and an object value or integer value. For example:
Problem with this approach is that it could add quite some overhead. It would need to be benchmarked before/after to see if it's acceptable. |
Contributor
Author
|
Thanks! Fully agreed. I’ll disregard this one and explore a post-processing solution instead; it should be cleaner and involve fewer moving parts. |
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.
Motivation
Custom
YamlConverter<T>implementations sometimes need to know which mapping key their value is being deserialized under. For example:${KEY}placeholders with the actual dictionary key nameCurrently, converters have no way to access this context.
Example
Solution
Add a
string? CurrentKeyproperty toYamlReaderthat tracks the most recent mapping key:YamlReader.CurrentKey— Public read/write property. Returnsnullat the top level.CurrentKeybefore reading each value, restore previous key afterward (proper nesting).CurrentKeybefore reading each property value across all 6 key-reading sites.Key design decisions
CurrentKeyand restores it after reading, ensuring nested structures see the correct innermost key.Tests
7 tests covering:
CurrentKeyCurrentKeyCurrentKeynullCurrentKeyCurrentKeycorrectlyCurrentKeyrestoration after nested mapping reads