Skip to content

Commit

Permalink
fix(config): auto save modified config data; fixes #144
Browse files Browse the repository at this point in the history
  • Loading branch information
kionz authored and lotem committed Sep 7, 2017
1 parent bcc4d10 commit 2736f4b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
15 changes: 10 additions & 5 deletions src/rime/config/config_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,8 @@ an<ConfigResource> ConfigCompiler::Compile(const string& file_name) {
auto resource = New<ConfigResource>(resource_id, New<ConfigData>());
graph_->resources[resource_id] = resource;
graph_->Push(resource, resource_id + ":");
if (!resource->data->LoadFromFile(
resource_resolver_->ResolvePath(resource_id).string(), this)) {
resource.reset();
}
resource->loaded = resource->data->LoadFromFile(
resource_resolver_->ResolvePath(resource_id).string(), this);
graph_->Pop();
return resource;
}
Expand Down Expand Up @@ -358,6 +356,10 @@ static an<ConfigItem> ResolveReference(ConfigCompiler* compiler,
if (!resource) {
LOG(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;
return nullptr;
}
}
return GetResolvedItem(compiler, resource, reference.local_path);
}
Expand Down Expand Up @@ -439,6 +441,9 @@ bool ConfigCompiler::Link(an<ConfigResource> target) {

bool ConfigCompiler::ResolveDependencies(const string& path) {
DLOG(INFO) << "ResolveDependencies(" << path << ")";
if (!graph_->deps.count(path)) {
return true;
}
auto& deps = graph_->deps[path];
for (auto iter = deps.begin(); iter != deps.end(); ) {
if (!(*iter)->Resolve(this)) {
Expand All @@ -448,7 +453,7 @@ bool ConfigCompiler::ResolveDependencies(const string& path) {
LOG(INFO) << "resolved: " << **iter;
iter = deps.erase(iter);
}
LOG(INFO) << "all dependencies resolved.";
DLOG(INFO) << "all dependencies resolved.";
return true;
}

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 @@ -14,6 +14,7 @@ namespace rime {
struct ConfigResource : ConfigItemRef {
string resource_id;
an<ConfigData> data;
bool loaded = false;

ConfigResource(const string& _id, an<ConfigData> _data)
: ConfigItemRef(nullptr), resource_id(_id), data(_data) {
Expand Down
5 changes: 1 addition & 4 deletions src/rime/config/config_component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,9 @@ an<ConfigData> ConfigComponent::GetConfigData(const string& file_name) {
if (wp.expired()) { // create a new copy and load it
ConfigCompiler compiler(resource_resolver_.get());
auto resource = compiler.Compile(file_name);
if (!resource || !compiler.Link(resource)) {
if (resource->loaded && !compiler.Link(resource)) {
LOG(ERROR) << "error loading config from: " << file_name;
}
if (!resource) {
return New<ConfigData>();
}
wp = resource->data;
return resource->data;
}
Expand Down
19 changes: 15 additions & 4 deletions test/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,27 @@ class RimeConfigTest : public ::testing::Test {
the<Config> config_;
};

TEST(RimeConfigComponentTest, RealCreationWorkflow) {
TEST(RimeConfigComponentTest, RoundTrip) {
// registration
Registry& r = Registry::instance();
r.Register("test_config", new ConfigComponent);
// find component
Config::Component* cc = Config::Require("test_config");
ASSERT_TRUE(cc != NULL);
// create Config with id
the<Config> config(cc->Create("config_test"));
EXPECT_TRUE(bool(config));
// create config and write modifications to file
{
the<Config> config(cc->Create("config_round_trip_test"));
EXPECT_TRUE(bool(config));
EXPECT_TRUE(config->SetString("key", "value"));
}
// read from file and verify contents
{
the<Config> config(cc->Create("config_round_trip_test"));
EXPECT_TRUE(bool(config));
string value;
EXPECT_TRUE(config->GetString("key", &value));
EXPECT_EQ("value", value);
}
r.Unregister("test_config");
}

Expand Down

0 comments on commit 2736f4b

Please sign in to comment.