-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Adding support for handling YAML Merge Key (#41) - proper keys comparison for merging #1279
Open
azat
wants to merge
5
commits into
jbeder:master
Choose a base branch
from
azat-ch:merge-operator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6bbc603
Adding support for handling YAML Merge Key (#41)
nlescoua 6292971
Fix merge-key handling in case the dictionary contains a sub-dictionary
nlescoua bcf7604
Fix merge operator support (that can be visible by iterating through …
azat 2acec3d
Fix order for merging iterator
azat 8945297
Mark MergeMapCollection() static
azat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#include <algorithm> | ||
#include <cassert> | ||
|
||
#include "nodebuilder.h" | ||
|
@@ -15,6 +16,7 @@ NodeBuilder::NodeBuilder() | |
m_stack{}, | ||
m_anchors{}, | ||
m_keys{}, | ||
m_mergeDicts{}, | ||
m_mapDepth(0) { | ||
m_anchors.push_back(nullptr); // since the anchors start at 1 | ||
} | ||
|
@@ -69,11 +71,38 @@ void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag, | |
node.set_tag(tag); | ||
node.set_style(style); | ||
m_mapDepth++; | ||
m_mergeDicts.emplace_back(); | ||
} | ||
|
||
void MergeMapCollection(detail::node& map_to, detail::node& map_from, | ||
detail::shared_memory_holder& pMemory) { | ||
for (auto j = map_from.begin(); j != map_from.end(); j++) { | ||
const auto from_key = j->first; | ||
/// NOTE: const_map_to.get(*j->first) cannot be used here, since it | ||
/// compares only the shared_ptr's, while we need to compare the key | ||
/// itself. | ||
/// | ||
/// NOTE: get() also iterates over elements | ||
bool found = std::any_of(map_to.begin(), map_to.end(), [&](const detail::node_iterator_value<detail::node> & kv) | ||
{ | ||
const auto key_node = kv.first; | ||
return key_node->scalar() == from_key->scalar(); | ||
}); | ||
if (!found) | ||
map_to.insert(*from_key, *j->second, pMemory); | ||
} | ||
} | ||
|
||
void NodeBuilder::OnMapEnd() { | ||
assert(m_mapDepth > 0); | ||
detail::node& collection = *m_stack.back(); | ||
auto& toMerge = *m_mergeDicts.rbegin(); | ||
/// The elements for merging should be traversed in reverse order to prefer last values. | ||
for (auto it = toMerge.rbegin(); it != toMerge.rend(); ++it) { | ||
MergeMapCollection(collection, **it, m_pMemory); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
} | ||
m_mapDepth--; | ||
m_mergeDicts.pop_back(); | ||
Pop(); | ||
} | ||
|
||
|
@@ -107,15 +136,40 @@ void NodeBuilder::Pop() { | |
m_stack.pop_back(); | ||
|
||
detail::node& collection = *m_stack.back(); | ||
|
||
if (collection.type() == NodeType::Sequence) { | ||
collection.push_back(node, m_pMemory); | ||
} else if (collection.type() == NodeType::Map) { | ||
assert(!m_keys.empty()); | ||
PushedKey& key = m_keys.back(); | ||
if (key.second) { | ||
collection.insert(*key.first, node, m_pMemory); | ||
m_keys.pop_back(); | ||
detail::node& nk = *key.first; | ||
if (nk.type() == NodeType::Scalar && | ||
((nk.tag() == "tag:yaml.org,2002:merge" && nk.scalar() == "<<") || | ||
(nk.tag() == "?" && nk.scalar() == "<<"))) { | ||
if (node.type() == NodeType::Map) { | ||
m_mergeDicts.rbegin()->emplace_back(&node); | ||
m_keys.pop_back(); | ||
} else if (node.type() == NodeType::Sequence) { | ||
for (auto i = node.begin(); i != node.end(); i++) { | ||
auto v = *i; | ||
if ((*v).type() == NodeType::Map) { | ||
m_mergeDicts.rbegin()->emplace_back(&(*v)); | ||
} else { | ||
throw ParserException( | ||
node.mark(), | ||
ErrorMsg::MERGE_KEY_NEEDS_SINGLE_OR_SEQUENCE_OF_MAPS); | ||
} | ||
} | ||
m_keys.pop_back(); | ||
} else { | ||
throw ParserException( | ||
node.mark(), | ||
ErrorMsg::MERGE_KEY_NEEDS_SINGLE_OR_SEQUENCE_OF_MAPS); | ||
} | ||
} else { | ||
collection.insert(*key.first, node, m_pMemory); | ||
m_keys.pop_back(); | ||
} | ||
} else { | ||
key.second = true; | ||
} | ||
|
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be
static
or in an anonymous namespace to not leak any symbols to other translation units.I also believe this is also a good case for a r-value reference for
map_from
. So I would use a&&
to make clear thatmap_from
will be in an unclear state after this call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
I'm not sure about this one, since
map_from
is in valid state upon return fromMergeMapCollection
, what make you thinks the opposite?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what I was thinking last month....It seems fine way it is 😅