Skip to content

Commit

Permalink
fix(config): user_config should not fall back to shared data
Browse files Browse the repository at this point in the history
Closes #271
  • Loading branch information
lotem committed Jan 4, 2019
1 parent a8ead41 commit 68c8a34
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
23 changes: 19 additions & 4 deletions src/rime/config/config_component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,30 @@ void Config::SetItem(an<ConfigItem> item) {
set_modified();
}

const ResourceType ConfigComponentBase::kConfigResourceType = {
const ResourceType ConfigResourceProvider::kDefaultResourceType = {
"config",
"",
".yaml",
};

ConfigComponentBase::ConfigComponentBase(const ResourceType& resource_type)
: resource_resolver_(
Service::instance().CreateResourceResolver(resource_type)) {
ResourceResolver* ConfigResourceProvider::CreateResourceResolver(
const ResourceType& resource_type) {
return Service::instance().CreateResourceResolver(resource_type);
}

const ResourceType UserConfigResourceProvider::kDefaultResourceType = {
"user_config",
"",
".yaml",
};

ResourceResolver* UserConfigResourceProvider::CreateResourceResolver(
const ResourceType& resource_type) {
return Service::instance().CreateUserSpecificResourceResolver(resource_type);
}

ConfigComponentBase::ConfigComponentBase(ResourceResolver* resource_resolver)
: resource_resolver_(resource_resolver) {
}

ConfigComponentBase::~ConfigComponentBase() {
Expand Down
29 changes: 22 additions & 7 deletions src/rime/config/config_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,22 @@ class ConfigCompiler;
class ConfigCompilerPlugin;
struct ConfigResource;

struct ConfigResourceProvider {
RIME_API static const ResourceType kDefaultResourceType;
RIME_API static ResourceResolver*
CreateResourceResolver(const ResourceType& resource_type);
};

struct UserConfigResourceProvider {
RIME_API static const ResourceType kDefaultResourceType;
RIME_API static ResourceResolver*
CreateResourceResolver(const ResourceType& resource_type);
};

class ConfigComponentBase : public Config::Component {
public:
RIME_API static const ResourceType kConfigResourceType;
RIME_API ConfigComponentBase(const ResourceType& resource_type);
RIME_API ~ConfigComponentBase();
RIME_API ConfigComponentBase(ResourceResolver* resource_resolver);
RIME_API virtual ~ConfigComponentBase();
RIME_API Config* Create(const string& file_name);

protected:
Expand All @@ -84,13 +95,17 @@ class ConfigComponentBase : public Config::Component {
map<string, weak<ConfigData>> cache_;
};

template <class Loader>
template <class Loader, class ResourceProvider = ConfigResourceProvider>
class ConfigComponent : public ConfigComponentBase {
public:
ConfigComponent(const ResourceType& resource_type = kConfigResourceType)
: ConfigComponentBase(resource_type) {}
ConfigComponent(const ResourceType& resource_type =
ResourceProvider::kDefaultResourceType)
: ConfigComponentBase(
ResourceProvider::CreateResourceResolver(resource_type)) {}
ConfigComponent(function<void (Loader* loader)> setup)
: ConfigComponentBase(kConfigResourceType) {
: ConfigComponentBase(
ResourceProvider::CreateResourceResolver(
ResourceProvider::kDefaultResourceType)) {
setup(&loader_);
}
private:
Expand Down
9 changes: 5 additions & 4 deletions src/rime/core_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ static void rime_core_initialize() {
r.Register("config", config_loader);
r.Register("schema", new SchemaComponent(config_loader));

auto user_config = new ConfigComponent<ConfigLoader>(
[](ConfigLoader* loader) {
loader->set_auto_save(true);
});
auto user_config =
new ConfigComponent<ConfigLoader, UserConfigResourceProvider>(
[](ConfigLoader* loader) {
loader->set_auto_save(true);
});
r.Register("user_config", user_config);
}

Expand Down
7 changes: 7 additions & 0 deletions src/rime/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ ResourceResolver* Service::CreateResourceResolver(const ResourceType& type) {
return resolver.release();
}

ResourceResolver* Service::CreateUserSpecificResourceResolver(
const ResourceType& type) {
the<ResourceResolver> resolver(new ResourceResolver(type));
resolver->set_root_path(deployer().user_data_dir);
return resolver.release();
}

Service& Service::instance() {
static the<Service> s_instance;
if (!s_instance) {
Expand Down
1 change: 1 addition & 0 deletions src/rime/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Service {
const string& message_value);

ResourceResolver* CreateResourceResolver(const ResourceType& type);
ResourceResolver* CreateUserSpecificResourceResolver(const ResourceType& type);

Deployer& deployer() { return deployer_; }
bool disabled() { return !started_ || deployer_.IsMaintenanceMode(); }
Expand Down
10 changes: 6 additions & 4 deletions test/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class RimeConfigTest : public ::testing::Test {
TEST(RimeConfigComponentTest, RoundTrip) {
// registration
Registry& r = Registry::instance();
r.Register("test_config", new ConfigComponent<ConfigLoader>(
[](ConfigLoader* loader) {
loader->set_auto_save(true);
}));
r.Register(
"test_config",
new ConfigComponent<ConfigLoader, UserConfigResourceProvider>(
[](ConfigLoader* loader) {
loader->set_auto_save(true);
}));
// find component
Config::Component* cc = Config::Require("test_config");
ASSERT_TRUE(cc != NULL);
Expand Down

0 comments on commit 68c8a34

Please sign in to comment.