-
Notifications
You must be signed in to change notification settings - Fork 553
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
feat(config): best effort resolution for circurlar dependencies #154
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test: | ||
__include: sometimes? | ||
home: excited | ||
work: | ||
__include: /test/home | ||
|
||
sometimes: | ||
work: naive |
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 |
---|---|---|
|
@@ -14,6 +14,8 @@ struct ConfigDependencyGraph { | |
vector<of<ConfigItemRef>> node_stack; | ||
vector<string> key_stack; | ||
map<string, vector<of<Dependency>>> deps; | ||
// paths for checking circular dependencies | ||
vector<string> resolve_chain; | ||
|
||
void Add(an<Dependency> dependency); | ||
|
||
|
@@ -311,12 +313,6 @@ an<ConfigResource> ConfigCompiler::Compile(const string& file_name) { | |
return resource; | ||
} | ||
|
||
static inline an<ConfigItem> if_resolved(ConfigCompiler* compiler, | ||
an<ConfigItem> item, | ||
const string& path) { | ||
return item && compiler->resolved(path) ? item : nullptr; | ||
} | ||
|
||
static bool ResolveBlockingDependencies(ConfigCompiler* compiler, | ||
const string& path) { | ||
if (!compiler->blocking(path)) { | ||
|
@@ -335,42 +331,42 @@ static an<ConfigItem> GetResolvedItem(ConfigCompiler* compiler, | |
const string& path) { | ||
DLOG(INFO) << "GetResolvedItem(" << resource->resource_id << ":" << path << ")"; | ||
string node_path = resource->resource_id + ":"; | ||
if (!resource || compiler->blocking(node_path)) { | ||
return nullptr; | ||
} | ||
an<ConfigItem> result = *resource; | ||
an<ConfigItemRef> node = resource; | ||
if (path.empty() || path == "/") { | ||
return if_resolved(compiler, result, node_path); | ||
return compiler->ResolveDependencies(node_path) ? **node : nullptr; | ||
} | ||
vector<string> keys = ConfigData::SplitPath(path); | ||
for (const auto& key : keys) { | ||
if (Is<ConfigList>(result)) { | ||
if (!ResolveBlockingDependencies(compiler, node_path)) { | ||
LOG(WARNING) << "accessing blocking node with unresolved dependencies: " | ||
<< node_path; | ||
// CAVEAT: continuing accessing subtree with this failure may result in | ||
// referencing outdated data - sometimes an expected behavior. | ||
// relaxing this requires checking for circular dependencies. | ||
//return nullptr; | ||
} | ||
an<ConfigItem> item = **node; | ||
if (Is<ConfigList>(item)) { | ||
if (ConfigData::IsListItemReference(key)) { | ||
size_t index = ConfigData::ResolveListIndex(result, key, true); | ||
size_t index = ConfigData::ResolveListIndex(item, key, true); | ||
(node_path += "/") += ConfigData::FormatListIndex(index); | ||
if (!ResolveBlockingDependencies(compiler, node_path)) { | ||
return nullptr; | ||
} | ||
result = As<ConfigList>(result)->GetAt(index); | ||
node = New<ConfigListEntryRef>(nullptr, As<ConfigList>(item), index); | ||
} else { | ||
result.reset(); | ||
node.reset(); | ||
} | ||
} else if (Is<ConfigMap>(result)) { | ||
} else if (Is<ConfigMap>(item)) { | ||
DLOG(INFO) << "advance with key: " << key; | ||
(node_path += "/") += key; | ||
if (!ResolveBlockingDependencies(compiler, node_path)) { | ||
return nullptr; | ||
} | ||
result = As<ConfigMap>(result)->Get(key); | ||
node = New<ConfigMapEntryRef>(nullptr, As<ConfigMap>(item), key); | ||
} else { | ||
result.reset(); | ||
node.reset(); | ||
} | ||
if (!result) { | ||
LOG(INFO) << "missing node: " << node_path; | ||
if (!node) { | ||
LOG(WARNING) << "inaccessible node: " << node_path << "/" << key; | ||
return nullptr; | ||
} | ||
} | ||
return if_resolved(compiler, result, node_path); | ||
return compiler->ResolveDependencies(node_path) ? **node : nullptr; | ||
} | ||
|
||
bool ConfigCompiler::blocking(const string& full_path) const { | ||
|
@@ -399,11 +395,6 @@ static an<ConfigItem> ResolveReference(ConfigCompiler* compiler, | |
if (!resource) { | ||
DLOG(INFO) << "resource not loaded, compiling: " << reference.resource_id; | ||
resource = compiler->Compile(reference.resource_id); | ||
// dependency doesn't require full resolution, this allows non conflicting | ||
// mutual references between config files. | ||
// this call is made even if resource is not loaded because plugins can | ||
// edit the empty config data, adding new dependencies. | ||
ResolveBlockingDependencies(compiler, reference.resource_id + ":"); | ||
if (!resource->loaded) { | ||
if (reference.optional) { | ||
LOG(INFO) << "optional resource not loaded: " << reference.resource_id; | ||
|
@@ -492,12 +483,27 @@ bool ConfigCompiler::Link(an<ConfigResource> target) { | |
(plugin_ ? plugin_->ReviewLinkOutput(this, target) : true); | ||
} | ||
|
||
static bool HasCircularDependencies(ConfigDependencyGraph* graph, | ||
const string& path) { | ||
for (const auto& x : graph->resolve_chain) { | ||
if (boost::starts_with(x, path) && | ||
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. path 的字符串比較,對 map 有效,list index 則除了數值還有關鍵字寫法,如 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. 暫時想不出來萬全的做法。先用用看。 |
||
(x.length() == path.length() || x[path.length()] == '/')) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool ConfigCompiler::ResolveDependencies(const string& path) { | ||
DLOG(INFO) << "ResolveDependencies(" << path << ")"; | ||
auto found = graph_->deps.find(path); | ||
if (found == graph_->deps.end()) { | ||
return true; | ||
} | ||
if (HasCircularDependencies(graph_.get(), path)) { | ||
LOG(WARNING) << "circular dependencies detected in " << path; | ||
return false; | ||
} | ||
graph_->resolve_chain.push_back(path); | ||
auto& deps = found->second; | ||
for (auto iter = deps.begin(); iter != deps.end(); ) { | ||
if (!(*iter)->Resolve(this)) { | ||
|
@@ -507,6 +513,7 @@ bool ConfigCompiler::ResolveDependencies(const string& path) { | |
LOG(INFO) << "resolved: " << **iter; | ||
iter = deps.erase(iter); | ||
} | ||
graph_->resolve_chain.pop_back(); | ||
DLOG(INFO) << "all dependencies resolved."; | ||
return 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
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.
我覺得例子這樣寫可以更好地描述行爲:
通過 patch 修改
test/home
的值,然後測試test/work
引用了 patch 之前的值。