Skip to content

Commit 2736f4b

Browse files
kionzlotem
authored andcommitted
fix(config): auto save modified config data; fixes #144
1 parent bcc4d10 commit 2736f4b

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/rime/config/config_compiler.cc

+10-5
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,8 @@ an<ConfigResource> ConfigCompiler::Compile(const string& file_name) {
267267
auto resource = New<ConfigResource>(resource_id, New<ConfigData>());
268268
graph_->resources[resource_id] = resource;
269269
graph_->Push(resource, resource_id + ":");
270-
if (!resource->data->LoadFromFile(
271-
resource_resolver_->ResolvePath(resource_id).string(), this)) {
272-
resource.reset();
273-
}
270+
resource->loaded = resource->data->LoadFromFile(
271+
resource_resolver_->ResolvePath(resource_id).string(), this);
274272
graph_->Pop();
275273
return resource;
276274
}
@@ -358,6 +356,10 @@ static an<ConfigItem> ResolveReference(ConfigCompiler* compiler,
358356
if (!resource) {
359357
LOG(INFO) << "resource not loaded, compiling: " << reference.resource_id;
360358
resource = compiler->Compile(reference.resource_id);
359+
if (!resource->loaded) {
360+
LOG(ERROR) << "resource could not be loaded: " << reference.resource_id;
361+
return nullptr;
362+
}
361363
}
362364
return GetResolvedItem(compiler, resource, reference.local_path);
363365
}
@@ -439,6 +441,9 @@ bool ConfigCompiler::Link(an<ConfigResource> target) {
439441

440442
bool ConfigCompiler::ResolveDependencies(const string& path) {
441443
DLOG(INFO) << "ResolveDependencies(" << path << ")";
444+
if (!graph_->deps.count(path)) {
445+
return true;
446+
}
442447
auto& deps = graph_->deps[path];
443448
for (auto iter = deps.begin(); iter != deps.end(); ) {
444449
if (!(*iter)->Resolve(this)) {
@@ -448,7 +453,7 @@ bool ConfigCompiler::ResolveDependencies(const string& path) {
448453
LOG(INFO) << "resolved: " << **iter;
449454
iter = deps.erase(iter);
450455
}
451-
LOG(INFO) << "all dependencies resolved.";
456+
DLOG(INFO) << "all dependencies resolved.";
452457
return true;
453458
}
454459

src/rime/config/config_compiler.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace rime {
1414
struct ConfigResource : ConfigItemRef {
1515
string resource_id;
1616
an<ConfigData> data;
17+
bool loaded = false;
1718

1819
ConfigResource(const string& _id, an<ConfigData> _data)
1920
: ConfigItemRef(nullptr), resource_id(_id), data(_data) {

src/rime/config/config_component.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,9 @@ an<ConfigData> ConfigComponent::GetConfigData(const string& file_name) {
167167
if (wp.expired()) { // create a new copy and load it
168168
ConfigCompiler compiler(resource_resolver_.get());
169169
auto resource = compiler.Compile(file_name);
170-
if (!resource || !compiler.Link(resource)) {
170+
if (resource->loaded && !compiler.Link(resource)) {
171171
LOG(ERROR) << "error loading config from: " << file_name;
172172
}
173-
if (!resource) {
174-
return New<ConfigData>();
175-
}
176173
wp = resource->data;
177174
return resource->data;
178175
}

test/config_test.cc

+15-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,27 @@ class RimeConfigTest : public ::testing::Test {
2727
the<Config> config_;
2828
};
2929

30-
TEST(RimeConfigComponentTest, RealCreationWorkflow) {
30+
TEST(RimeConfigComponentTest, RoundTrip) {
3131
// registration
3232
Registry& r = Registry::instance();
3333
r.Register("test_config", new ConfigComponent);
3434
// find component
3535
Config::Component* cc = Config::Require("test_config");
3636
ASSERT_TRUE(cc != NULL);
37-
// create Config with id
38-
the<Config> config(cc->Create("config_test"));
39-
EXPECT_TRUE(bool(config));
37+
// create config and write modifications to file
38+
{
39+
the<Config> config(cc->Create("config_round_trip_test"));
40+
EXPECT_TRUE(bool(config));
41+
EXPECT_TRUE(config->SetString("key", "value"));
42+
}
43+
// read from file and verify contents
44+
{
45+
the<Config> config(cc->Create("config_round_trip_test"));
46+
EXPECT_TRUE(bool(config));
47+
string value;
48+
EXPECT_TRUE(config->GetString("key", &value));
49+
EXPECT_EQ("value", value);
50+
}
4051
r.Unregister("test_config");
4152
}
4253

0 commit comments

Comments
 (0)