Skip to content

Commit 0784eb0

Browse files
committed
fix(config_compiler): support creating list in-place by __patch and __merge
1 parent 253d8d1 commit 0784eb0

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

data/test/config_merge_test.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ merge_tree:
4646
zerg:
4747
# overwrite existing list
4848
ground_units: []
49+
50+
create_list_with_inplace_patch:
51+
# map node without data key-value (exclude compiler directives) can be converted to list
52+
all_ground_units:
53+
__patch:
54+
- __append: [scv, marine, firebat, vulture, tank]
55+
- __append: {__include: starcraft/protoss/ground_units}
56+
- __append: {__include: starcraft/zerg/ground_units}

src/rime/config/config_compiler.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ static bool AppendToList(an<ConfigItemRef> target, an<ConfigList> list) {
9797
return false;
9898
auto existing_list = As<ConfigList>(**target);
9999
if (!existing_list) {
100-
LOG(ERROR) << "trying to append list to other value";
101-
return false;
100+
if (!(**target)->empty()) {
101+
LOG(ERROR) << "trying to append list to incompatible node type";
102+
return false;
103+
}
104+
// convert empty node (usually map with only compiler directives) to list;
105+
// refer to test case RimeConfigMergeTest.CreateListWithInplacePatch
106+
existing_list = target->AsList();
102107
}
103108
if (list->empty())
104109
return true;

test/config_compiler_test.cc

+6
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ class RimeConfigCircularDependencyTest : public RimeConfigCompilerTestBase {
251251
}
252252
};
253253

254+
TEST_F(RimeConfigMergeTest, CreateListWithInplacePatch) {
255+
const string& prefix = "create_list_with_inplace_patch/";
256+
EXPECT_TRUE(config_->IsList(prefix + "all_ground_units"));
257+
EXPECT_EQ(16, config_->GetListSize(prefix + "all_ground_units"));
258+
}
259+
254260
TEST_F(RimeConfigCircularDependencyTest, BestEffortResolution) {
255261
const string& prefix = "test/";
256262
EXPECT_TRUE(config_->IsNull(prefix + "__patch"));

0 commit comments

Comments
 (0)