Skip to content

Commit

Permalink
feat(config): references to optional config resources, ending with "?"
Browse files Browse the repository at this point in the history
  • Loading branch information
kionz authored and lotem committed Sep 7, 2017
1 parent 2736f4b commit 14ec858
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
8 changes: 8 additions & 0 deletions data/test/config_compiler_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ dependency_priorities:
player: bisu
__include: /starcraft/protoss

optional_reference:
__include: nonexistent.yaml:/?
__patch:
- local/nonexistent_patch?
- config_test:/nonexistent_patch?
- nonexistent:/patch?
untouched: true

local:
patch:
battlefields/@next: match point
Expand Down
26 changes: 17 additions & 9 deletions src/rime/config/config_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct PendingChild : Dependency {
};

string Reference::repr() const {
return resource_id + ":" + local_path;
return resource_id + ":" + local_path + (optional ? " <optional>" : "");
}

template <class StreamT>
Expand Down Expand Up @@ -131,7 +131,7 @@ bool IncludeReference::Resolve(ConfigCompiler* compiler) {
DLOG(INFO) << "IncludeReference::Resolve(reference = " << reference << ")";
auto item = ResolveReference(compiler, reference);
if (!item) {
return false;
return reference.optional;
}
*target = item;
return true;
Expand All @@ -141,7 +141,7 @@ bool PatchReference::Resolve(ConfigCompiler* compiler) {
DLOG(INFO) << "PatchReference::Resolve(reference = " << reference << ")";
auto item = ResolveReference(compiler, reference);
if (!item) {
return false;
return reference.optional;
}
if (!Is<ConfigMap>(item)) {
LOG(ERROR) << "invalid patch at " << reference;
Expand Down Expand Up @@ -227,14 +227,18 @@ ConfigCompiler::~ConfigCompiler() {
}

Reference ConfigCompiler::CreateReference(const string& qualified_path) {
auto end = qualified_path.find_last_of("?");
bool optional = end != string::npos;
auto separator = qualified_path.find_first_of(":");
string resource_id = resource_resolver_->ToResourceId(
(separator == string::npos || separator == 0) ?
graph_->current_resource_id() : qualified_path.substr(0, separator));
graph_->current_resource_id() :
qualified_path.substr(0, separator));
string local_path = (separator == string::npos) ?
qualified_path :
qualified_path.substr(separator + 1);
return Reference{resource_id, local_path};
qualified_path.substr(0, end) :
qualified_path.substr(separator + 1,
optional ? end - separator - 1 : end);
return Reference{resource_id, local_path, optional};
}

void ConfigCompiler::AddDependency(an<Dependency> dependency) {
Expand Down Expand Up @@ -354,10 +358,14 @@ static an<ConfigItem> ResolveReference(ConfigCompiler* compiler,
const Reference& reference) {
auto resource = compiler->GetCompiledResource(reference.resource_id);
if (!resource) {
LOG(INFO) << "resource not loaded, compiling: " << reference.resource_id;
DLOG(INFO) << "resource not loaded, compiling: " << reference.resource_id;
resource = compiler->Compile(reference.resource_id);
if (!resource->loaded) {
LOG(ERROR) << "resource could not be loaded: " << reference.resource_id;
if (reference.optional) {
LOG(INFO) << "optional resource not loaded: " << reference.resource_id;
} else {
LOG(ERROR) << "resource could not be loaded: " << reference.resource_id;
}
return nullptr;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/rime/config/config_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct ConfigResource : ConfigItemRef {
struct Reference {
string resource_id;
string local_path;
bool optional;

string repr() const;
};
Expand Down
9 changes: 8 additions & 1 deletion test/config_compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,11 @@ TEST_F(RimeConfigCompilerTest, DependencyPriorities) {
EXPECT_EQ("bisu", player);
}

// TODO: test failure cases
TEST_F(RimeConfigCompilerTest, OptionalReference) {
const string& prefix = "optional_reference/";
EXPECT_TRUE(config_->IsNull(prefix + "__include"));
EXPECT_TRUE(config_->IsNull(prefix + "__patch"));
bool untouched;
EXPECT_TRUE(config_->GetBool(prefix + "untouched", &untouched));
EXPECT_TRUE(untouched);
}

0 comments on commit 14ec858

Please sign in to comment.