Skip to content

Commit

Permalink
增加 --enable-alias-mapping 选项,用于在映射Excel列到目标数据结构时,开启别名匹配。
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Oct 15, 2020
1 parent 47775e0 commit a5fd946
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 15 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
更新记录
==========

2.10.0
------

1. 增加 ```--enable-alias-mapping``` 选项,用于在映射Excel列到目标数据结构时,开启别名匹配。
2. protobuf的枚举类型解析支持message内嵌enum。

2.9.3
------

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ echo "
| -v --version | 打印版本号 | |
| -n --rename | 重命名输出文件名 | 正则表达式 (如:/(?i)\\.bin$/\\.lua/) |
| --require-mapping-all | 开启所有字段映射检查 | 开启所有字段映射检查后,转出结构中所有的字段都必须配置映射关系,数组字段至少要有一个元素 |
| --enable-alias-mapping | 开启别名匹配 | 映射Excel列到目标数据结构,允许使用别名 |
| -c --const-print | 输出协议描述中的常量 | 参数为字符串,表示输出的文件名 |
| -i --option-print | 输出协议描述中的选项 | 参数为字符串,表示输出的文件名 |
| -a --data-version | 设置数据版本号 | 参数为字符串,表示输出的数据的data_ver字段。如果不设置将按执行时间自动生成一个 |
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.xresloader</groupId>
<artifactId>xresloader</artifactId>
<version>2.9.3</version>
<version>2.10.0</version>
<packaging>jar</packaging>
<name>xresloader</name>

Expand Down
8 changes: 8 additions & 0 deletions src/org/xresloader/core/ProgramOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class RenameRule {
public String dataSourceMetaDelimiter = "\\|";
public RenameRule renameRule = null;
public boolean requireMappingAllFields = false;
public boolean enableAliasMapping = false;
public boolean enableFormular = true;
public boolean enbleEmptyList = false;
public int prettyIndent = 0;
Expand All @@ -70,6 +71,7 @@ private ProgramOptions() {
dataSourceDirectory = outputDirectory;
dataSourceType = FileType.BIN;
requireMappingAllFields = false;
enableAliasMapping = false;
}

public static ProgramOptions getInstance() {
Expand All @@ -94,6 +96,7 @@ public void reset() {
dataSourceMetaDelimiter = "\\|";
renameRule = null;
requireMappingAllFields = false;
enableAliasMapping = false;
enableFormular = true;
enbleEmptyList = false;
prettyIndent = 0;
Expand Down Expand Up @@ -161,6 +164,7 @@ private static Options get_options_group() {
.build());
options.addOption(null, "require-mapping-all", false,
"require all fields in protocol message to be mapped from data source");
options.addOption(null, "enable-alias-mapping", false, "allow to use alias when mapping fields");

options.addOption(Option.builder("a").longOpt("data-version").desc("set data version").hasArg()
.argName("DATA VERSION").build());
Expand Down Expand Up @@ -405,6 +409,10 @@ public int init(String[] args) {
requireMappingAllFields = true;
}

if (cmd.hasOption("enable-alias-mapping")) {
enableAliasMapping = true;
}

// special functions
if (cmd.hasOption("disable-excel-formular")) {
enableFormular = false;
Expand Down
132 changes: 118 additions & 14 deletions src/org/xresloader/core/data/dst/DataDstPb.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ static <T> T get_alias_list_element(String name, HashMap<String, PbAliasNode<T>>
return null;
}

static <T> String get_alias_list_element_full_name(String name, HashMap<String, PbAliasNode<T>> hashmap, String type_name) {
static <T> String get_alias_list_element_full_name(String name, HashMap<String, PbAliasNode<T>> hashmap,
String type_name) {
while (name.length() > 0 && name.charAt(0) == '.') {
name = name.substring(1);
}
Expand All @@ -165,7 +166,8 @@ static <T> String get_alias_list_element_full_name(String name, HashMap<String,

if (null != ls.names && ls.names.size() > 1) {
ProgramOptions.getLoger().error(
"there is more than one %s \"%s\" matched, please use full name. available names:", type_name, name);
"there is more than one %s \"%s\" matched, please use full name. available names:", type_name,
name);
for (String full_name : ls.names) {
ProgramOptions.getLoger().error("\t%s", full_name);
}
Expand Down Expand Up @@ -195,10 +197,17 @@ static void load_pb_message(PbInfoSet pbs, DescriptorProtos.DescriptorProto mdp,
HashMap<String, PbAliasNode<DescriptorProtos.DescriptorProto>> hashmap) {
String full_name = String.format("%s.%s", package_name, mdp.getName());
append_alias_list(mdp.getName(), full_name, pbs.messages, mdp);

// nest messages
for (DescriptorProtos.DescriptorProto sub_mdp : mdp.getNestedTypeList()) {
load_pb_message(pbs, sub_mdp, full_name, hashmap);
}

// enums
for (DescriptorProtos.EnumDescriptorProto edp : mdp.getEnumTypeList()) {
append_alias_list(edp.getName(), String.format("%s.%s", full_name, edp.getName()), pbs.enums, edp);
}

// oneof
for (DescriptorProtos.OneofDescriptorProto oneof_desc : mdp.getOneofDeclList()) {
append_alias_list(oneof_desc.getName(),
Expand Down Expand Up @@ -498,7 +507,7 @@ static private LinkedList<DataVerifyImpl> setup_verifier(String verifier, Descri
if (enum_desc != null) {
vfy = new DataVerifyPbEnum(enum_desc);
}

if (null == vfy) {
DescriptorProtos.DescriptorProto msg_desc = get_alias_list_element(rule, cachePbs.messages,
"message type");
Expand All @@ -508,21 +517,24 @@ static private LinkedList<DataVerifyImpl> setup_verifier(String verifier, Descri
}

if (null == vfy) {
DescriptorProtos.OneofDescriptorProto oneof_desc = get_alias_list_element(rule, cachePbs.oneofs,
"oneof type");
DescriptorProtos.OneofDescriptorProto oneof_desc = get_alias_list_element(rule,
cachePbs.oneofs, "oneof type");
if (oneof_desc != null) {
DescriptorProtos.DescriptorProto msg_desc = null;
int message_bound = rule.lastIndexOf('.');
if (message_bound > 0 && message_bound < rule.length()) {
msg_desc = get_alias_list_element(rule.substring(0, message_bound), cachePbs.messages, "message type");
msg_desc = get_alias_list_element(rule.substring(0, message_bound),
cachePbs.messages, "message type");
} else {
String oneof_full_name = get_alias_list_element_full_name(rule, cachePbs.oneofs, "oneof type");
String oneof_full_name = get_alias_list_element_full_name(rule, cachePbs.oneofs,
"oneof type");
message_bound = oneof_full_name.lastIndexOf('.');
if (message_bound > 0 && message_bound < oneof_full_name.length()) {
msg_desc = get_alias_list_element(oneof_full_name.substring(0, message_bound), cachePbs.messages, "message type");
msg_desc = get_alias_list_element(oneof_full_name.substring(0, message_bound),
cachePbs.messages, "message type");
}
}

if (oneof_desc != null && msg_desc != null) {
vfy = new DataVerifyPbOneof(oneof_desc, msg_desc);
}
Expand Down Expand Up @@ -949,29 +961,66 @@ private boolean testMessage(DataDstWriterNode node, LinkedList<String> name_list
for (Descriptors.OneofDescriptor oneof : desc.getOneofs()) {
oneofField.put(oneof.getFullName(), null);
}
boolean enable_alias_mapping = ProgramOptions.getInstance().enableAliasMapping;

DataSrcImpl data_src = DataSrcImpl.getOurInstance();
for (Descriptors.FieldDescriptor fd : desc.getFields()) {
DataDstChildrenNode child = null;
String field_alias = null;
if (enable_alias_mapping && fd.getOptions().hasExtension(Xresloader.fieldAlias)
&& !fd.getOptions().getExtension(Xresloader.fieldAlias).isEmpty()) {
field_alias = fd.getOptions().getExtension(Xresloader.fieldAlias);
}
switch (fd.getType()) {
// 复杂类型还需要检测子节点
case MESSAGE:
if (fd.isRepeated()) {
int count = 0;
String repeated_test_name = null;

name_list.addLast("");
for (;; ++count) {
if (0 == count) {
repeated_test_name = fd.getName();
}

DataDstWriterNode c = createMessageWriterNode(cachePbs, fd.getMessageType(),
DataDstWriterNode.JAVA_TYPE.MESSAGE);
boolean test_passed = false;

name_list.removeLast();
name_list.addLast(DataDstWriterNode.makeNodeName(fd.getName(), count));
name_list.addLast(DataDstWriterNode.makeNodeName(repeated_test_name, count));
if (test(c, name_list)) {
child = node.addChild(fd.getName(), c, fd, DataDstWriterNode.CHILD_NODE_TYPE.STANDARD);
ret = true;
} else {
test_passed = true;
} else if (0 == count && null != field_alias) {
repeated_test_name = field_alias;
name_list.removeLast();
name_list.addLast(DataDstWriterNode.makeNodeName(repeated_test_name, count));
if (test(c, name_list)) {
child = node.addChild(fd.getName(), c, fd,
DataDstWriterNode.CHILD_NODE_TYPE.STANDARD);
ret = true;
test_passed = true;
}
}

if (!test_passed) {
// Reset to field name when test first element
if (0 == count) {
repeated_test_name = fd.getName();
}
// try plain mode - array item
String real_name = DataDstWriterNode.makeChildPath(prefix, fd.getName(), count);
String real_name = DataDstWriterNode.makeChildPath(prefix, repeated_test_name, count);
IdentifyDescriptor col = data_src.getColumnByName(real_name);
if (null == col && 0 == count && null != field_alias) {
repeated_test_name = field_alias;
String alias_name = DataDstWriterNode.makeChildPath(prefix, repeated_test_name,
count);
col = data_src.getColumnByName(alias_name);
}

if (null != col) {
child = node.addChild(fd.getName(), c, fd, DataDstWriterNode.CHILD_NODE_TYPE.PLAIN);
setup_node_identify(c, child, col, fd);
Expand All @@ -989,6 +1038,12 @@ private boolean testMessage(DataDstWriterNode node, LinkedList<String> name_list
// try plain mode - the whole array
String real_name = DataDstWriterNode.makeChildPath(prefix, fd.getName());
IdentifyDescriptor col = data_src.getColumnByName(real_name);

if (null == col && null != field_alias) {
String alias_name = DataDstWriterNode.makeChildPath(prefix, field_alias);
col = data_src.getColumnByName(alias_name);
}

if (null != col) {
DataDstWriterNode c = createMessageWriterNode(cachePbs, fd.getMessageType(),
DataDstWriterNode.JAVA_TYPE.MESSAGE);
Expand All @@ -1004,17 +1059,36 @@ private boolean testMessage(DataDstWriterNode node, LinkedList<String> name_list
} else {
DataDstWriterNode c = createMessageWriterNode(cachePbs, fd.getMessageType(),
DataDstWriterNode.JAVA_TYPE.MESSAGE);
boolean test_passed = false;

name_list.addLast(DataDstWriterNode.makeNodeName(fd.getName()));
if (test(c, name_list)) {
filterMissingFields(missingFields, oneofField, fd, false);
child = node.addChild(fd.getName(), c, fd, DataDstWriterNode.CHILD_NODE_TYPE.STANDARD);
ret = true;
} else {
test_passed = true;
} else if (null != field_alias) {
name_list.removeLast();
name_list.addLast(DataDstWriterNode.makeNodeName(field_alias));
if (test(c, name_list)) {
filterMissingFields(missingFields, oneofField, fd, false);
child = node.addChild(fd.getName(), c, fd, DataDstWriterNode.CHILD_NODE_TYPE.STANDARD);
ret = true;
test_passed = true;
}
}

if (!test_passed) {
filterMissingFields(missingFields, oneofField, fd, true);

// try plain mode
String real_name = DataDstWriterNode.makeChildPath(prefix, fd.getName());
IdentifyDescriptor col = data_src.getColumnByName(real_name);
if (null == col && null != field_alias) {
String alias_name = DataDstWriterNode.makeChildPath(prefix, field_alias);
col = data_src.getColumnByName(alias_name);
}

if (null != col) {
child = node.addChild(fd.getName(), c, fd, DataDstWriterNode.CHILD_NODE_TYPE.PLAIN);
setup_node_identify(c, child, col, fd);
Expand All @@ -1039,9 +1113,20 @@ private boolean testMessage(DataDstWriterNode node, LinkedList<String> name_list
DataDstWriterNode.JAVA_TYPE inner_type = pbTypeToInnerType(fd.getType());
if (fd.isRepeated()) {
int count = 0;
String repeated_test_name = null;
for (;; ++count) {
String real_name = DataDstWriterNode.makeChildPath(prefix, fd.getName(), count);
if (0 == count) {
repeated_test_name = fd.getName();
}

String real_name = DataDstWriterNode.makeChildPath(prefix, repeated_test_name, count);
IdentifyDescriptor col = data_src.getColumnByName(real_name);
if (null == col && 0 == count && null != field_alias) {
repeated_test_name = field_alias;
String alias_name = DataDstWriterNode.makeChildPath(prefix, repeated_test_name, count);
col = data_src.getColumnByName(alias_name);
}

if (null != col) {
DataDstWriterNode c = createMessageWriterNode(cachePbs, inner_type);
child = node.addChild(fd.getName(), c, fd, DataDstWriterNode.CHILD_NODE_TYPE.STANDARD);
Expand All @@ -1058,6 +1143,11 @@ private boolean testMessage(DataDstWriterNode node, LinkedList<String> name_list
// try plain mode
String real_name = DataDstWriterNode.makeChildPath(prefix, fd.getName());
IdentifyDescriptor col = data_src.getColumnByName(real_name);
if (null == col && null != field_alias) {
String alias_name = DataDstWriterNode.makeChildPath(prefix, field_alias);
col = data_src.getColumnByName(alias_name);
}

if (null != col) {
DataDstWriterNode c = createMessageWriterNode(cachePbs, inner_type);
child = node.addChild(fd.getName(), c, fd, DataDstWriterNode.CHILD_NODE_TYPE.PLAIN);
Expand All @@ -1073,6 +1163,11 @@ private boolean testMessage(DataDstWriterNode node, LinkedList<String> name_list
// 非 list 类型
String real_name = DataDstWriterNode.makeChildPath(prefix, fd.getName());
IdentifyDescriptor col = data_src.getColumnByName(real_name);
if (null == col && null != field_alias) {
String alias_name = DataDstWriterNode.makeChildPath(prefix, field_alias);
col = data_src.getColumnByName(alias_name);
}

if (null != col) {
filterMissingFields(missingFields, oneofField, fd, false);
DataDstWriterNode c = createMessageWriterNode(cachePbs, inner_type);
Expand Down Expand Up @@ -1113,6 +1208,15 @@ private boolean testMessage(DataDstWriterNode node, LinkedList<String> name_list
String real_name = DataDstWriterNode.makeChildPath(prefix, fd.getName());
IdentifyDescriptor col = data_src.getColumnByName(real_name);

// if (null == col && enable_alias_mapping) {
// if (fd.getOptions().hasExtension(Xresloader.oneofAlias)
// && !fd.getOptions().getExtension(Xresloader.oneofAlias).isEmpty()) {
// String alias_name = DataDstWriterNode.makeChildPath(prefix,
// fd.getOptions().getExtension(Xresloader.oneofAlias));
// col = data_src.getColumnByName(alias_name);
// }
// }

if (null == col) {
continue;
}
Expand Down

0 comments on commit a5fd946

Please sign in to comment.