Skip to content

Commit 02151da

Browse files
committed
feat(resource_resolver): fallback root path
1 parent 03ee8b4 commit 02151da

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

src/rime/resource_resolver.cc

+14
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,18 @@ boost::filesystem::path ResourceResolver::ResolvePath(const string& resource_id)
1313
root_path_);
1414
}
1515

16+
boost::filesystem::path
17+
FallbackResourceResolver::ResolvePath(const string& resource_id) {
18+
auto default_path = ResourceResolver::ResolvePath(resource_id);
19+
if (!boost::filesystem::exists(default_path)) {
20+
auto fallback_path = boost::filesystem::absolute(
21+
boost::filesystem::path(type_.prefix + resource_id + type_.suffix),
22+
fallback_root_path_);
23+
if (boost::filesystem::exists(fallback_path)) {
24+
return fallback_path;
25+
}
26+
}
27+
return default_path;
28+
}
29+
1630
} // namespace rime

src/rime/resource_resolver.h

+18-2
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,34 @@ class ResourceResolver {
2323
public:
2424
explicit ResourceResolver(const ResourceType type) : type_(type) {
2525
}
26-
boost::filesystem::path ResolvePath(const string& resource_id);
26+
virtual ~ResourceResolver() {
27+
}
28+
virtual boost::filesystem::path ResolvePath(const string& resource_id);
2729
void set_root_path(const boost::filesystem::path& root_path) {
2830
root_path_ = root_path;
2931
}
3032
boost::filesystem::path root_path() const {
3133
return root_path_;
3234
}
33-
private:
35+
protected:
3436
const ResourceType type_;
3537
boost::filesystem::path root_path_;
3638
};
3739

40+
// try fallback path if target file doesn't exist in root path
41+
class FallbackResourceResolver : public ResourceResolver {
42+
public:
43+
explicit FallbackResourceResolver(const ResourceType& type)
44+
: ResourceResolver(type) {
45+
}
46+
boost::filesystem::path ResolvePath(const string& resource_id) override;
47+
void set_fallback_root_path(const boost::filesystem::path& fallback_root_path) {
48+
fallback_root_path_ = fallback_root_path;
49+
}
50+
private:
51+
boost::filesystem::path fallback_root_path_;
52+
};
53+
3854
} // namespace rime
3955

4056
#endif // RIME_RESOURCE_RESOLVER_H_

test/resource_resolver_test.cc

+41-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,48 @@
33

44
using namespace rime;
55

6+
static const ResourceType kMineralsType = ResourceType{
7+
"minerals",
8+
"not_",
9+
".minerals",
10+
};
11+
612
TEST(RimeResourceResolverTest, ResolvePath) {
7-
const auto type = ResourceType{
8-
"minerals",
9-
"not_",
10-
".minerals",
11-
};
12-
the<ResourceResolver> rr(new ResourceResolver(type));
13-
rr->set_root_path("/starcraft");
14-
auto actual = rr->ResolvePath("enough");
13+
ResourceResolver rr(kMineralsType);
14+
rr.set_root_path("/starcraft");
15+
auto actual = rr.ResolvePath("enough");
1516
boost::filesystem::path expected = "/starcraft/not_enough.minerals";
17+
EXPECT_TRUE(actual.is_absolute());
1618
EXPECT_TRUE(expected == actual);
1719
}
20+
21+
TEST(RimeResourceResolverTest, FallbackRootPath) {
22+
FallbackResourceResolver rr(kMineralsType);
23+
rr.set_fallback_root_path("fallback");
24+
boost::filesystem::create_directory("fallback");
25+
{
26+
boost::filesystem::path nonexistent_default = "not_present.minerals";
27+
boost::filesystem::remove(nonexistent_default);
28+
auto fallback = boost::filesystem::absolute("fallback/not_present.minerals");
29+
boost::filesystem::ofstream(fallback).close();
30+
auto actual = rr.ResolvePath("present");
31+
EXPECT_TRUE(fallback == actual);
32+
boost::filesystem::remove(fallback);
33+
}
34+
{
35+
auto existent_default = boost::filesystem::absolute("not_falling_back.minerals");
36+
boost::filesystem::ofstream(existent_default).close();
37+
auto actual = rr.ResolvePath("falling_back");
38+
EXPECT_TRUE(existent_default == actual);
39+
boost::filesystem::remove(existent_default);
40+
}
41+
{
42+
auto nonexistent_default = boost::filesystem::absolute("not_any.minerals");
43+
boost::filesystem::remove(nonexistent_default);
44+
auto nonexistent_fallback = boost::filesystem::absolute("fallback/not_any.minerals");
45+
boost::filesystem::remove(nonexistent_fallback);
46+
auto actual = rr.ResolvePath("any");
47+
EXPECT_TRUE(nonexistent_default == actual);
48+
}
49+
boost::filesystem::remove_all("fallback");
50+
}

0 commit comments

Comments
 (0)