del[protobuf]: not support proto enum in protobuf

This commit is contained in:
godotg
2023-12-02 22:44:29 +08:00
parent 827e30e899
commit bdc49b00c2
4 changed files with 1 additions and 286 deletions
@@ -18,7 +18,6 @@ import com.zfoo.protocol.serializer.protobuf.PbBuildOption;
import com.zfoo.protocol.serializer.protobuf.internal.CodeBuilder;
import com.zfoo.protocol.serializer.protobuf.wire.*;
import com.zfoo.protocol.serializer.protobuf.wire.Field.Type;
import com.zfoo.protocol.serializer.protobuf.wire.ProtoEnum.EnumEntry;
import com.zfoo.protocol.serializer.protobuf.wire.WireFormat.JavaType;
import com.zfoo.protocol.serializer.protobuf.wire.parser.Proto;
import com.zfoo.protocol.util.StringUtils;
@@ -276,90 +275,6 @@ public class JavaBuilder {
.forEach(e -> cb.c(buildMessage(proto, e, level + 1, defineMsgs, nestedOps, protos)));
}
private void nestMessageEnum(CodeBuilder cb, List<ProtoEnum> enums, int indent) {
if (enums == null || enums.isEmpty()) {
return;
}
cb.ln();
PbBuildOption nestedOps = new PbBuildOption();
nestedOps.setIsNested(true);
enums.stream()
.sorted(Comparator.comparing(ProtoEnum::getName))
.forEach(e -> cb.c(buildEnum(e, indent + 1, nestedOps)).ln(2));
}
private String getFillSpaces(int count) {
if (count <= 0) {
return StringUtils.EMPTY;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(" ");
}
return sb.toString();
}
public String buildEnum(ProtoEnum protoEnum, final int indent, PbBuildOption buildOps) {
int level = (indent < 1) ? 1 : indent;
CodeBuilder cb = new CodeBuilder();
cb.t(level - 1).e("public enum $name$ {").arg(protoEnum.getName()).ln();
List<EnumEntry> entries = protoEnum.getEntries();
if (entries != null && !entries.isEmpty()) {
Optional<EnumEntry> maxLen = entries.stream()
.max(Comparator.comparingInt(e -> e.getLabel().length()));
int len = maxLen.isPresent() ? maxLen.get().getLabel().length() : 1;
List<EnumEntry> es = new ArrayList<>();
entries.stream()
.sorted(Comparator.comparingInt(EnumEntry::getValue))
.forEach(es::add);
CodeBuilder vsb = new CodeBuilder();
CodeBuilder psb = new CodeBuilder();
for (int i = 0; i < es.size(); i++) {
EnumEntry o = es.get(i);
String sep = (i != es.size() - 1) ? "," : ";";
String spaces = getFillSpaces(len - o.getLabel().length());
String[] args = new String[]{o.getLabel(), spaces,
String.valueOf(o.getValue()), sep};
cb.t(level).e("$label$$spaces$($value$)$;$").arg(args).ln();
psb.t(level).e("public static final int $lable$_VALUE = $value$;")
.arg(o.getLabel(), String.valueOf(o.getValue())).ln();
vsb.t(level + 2).e("case $value$:").arg(String.valueOf(o.getValue())).ln();
vsb.t(level + 3).e("return $name$.$label$;")
.arg(protoEnum.getName(), o.getLabel()).ln();
}
cb.ln();
cb.c(psb.toString()).ln();
cb.t(level).c("private final int value;").ln(2);
cb.t(level).e("private $label$(int value) {").arg(protoEnum.getName()).ln();
cb.t(level + 1).c("this.value = value;").ln();
cb.t(level).c("}").ln();
cb.t(level).c("public int getValue() {").ln();
cb.t(level + 1).c("return this.value;").ln();
cb.t(level).c("}").ln(2);
cb.t(level).e("public static $name$ valueOf(int value) {")
.arg(protoEnum.getName()).ln();
cb.t(level + 1).c("switch (value) {").ln();
cb.c(vsb.toString());
cb.t(level + 2).c("default:").ln();
cb.t(level + 3).e("throw new IllegalArgumentException("
+ "\"no enum value $value$ \" + value);")
.arg(protoEnum.getName()).ln();
cb.t(level + 1).c("}").ln();
cb.t(level).c("}").ln();
}
cb.t(level - 1).c("}");
return cb.toString();
}
}
@@ -17,7 +17,6 @@ import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.serializer.protobuf.PbBuildOption;
import com.zfoo.protocol.serializer.protobuf.builder.JavaBuilder;
import com.zfoo.protocol.serializer.protobuf.wire.Option;
import com.zfoo.protocol.serializer.protobuf.wire.ProtoEnum;
import com.zfoo.protocol.serializer.protobuf.wire.ProtoMessage;
import com.zfoo.protocol.serializer.protobuf.wire.parser.Proto;
import com.zfoo.protocol.util.FileUtils;
@@ -1,196 +0,0 @@
/*
* Copyright 2021 The edap Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.serializer.protobuf.wire;
import java.util.List;
/**
* protocol buffer的枚举类型的定义
*/
public class ProtoEnum {
/**
* 枚举名称
*/
private String name;
/**
* 枚举包含的值的定义
*/
private List<EnumEntry> entries;
/**
* 枚举包含的选项列表
*/
private List<Option> options;
/**
* 枚举项的定义
*/
public static class EnumEntry {
/**
* 枚举值定义的名称
*/
private String label;
/**
* 枚举定义的值
*/
private int value;
/**
* 枚举的选项列表
*/
private List<Option> options;
/**
* 枚举值的注释
*/
private Comment comment;
/**
* 枚举值定义的名称
*
* @return the label
*/
public String getLabel() {
return label;
}
/**
* 枚举值定义的名称
*
* @param label the label to set
* @return
*/
public EnumEntry setLabel(String label) {
this.label = label;
return this;
}
/**
* 枚举定义的值
*
* @return the value
*/
public int getValue() {
return value;
}
/**
* 枚举定义的值
*
* @param value the value to set
* @return
*/
public EnumEntry setValue(int value) {
this.value = value;
return this;
}
/**
* 枚举的选项列表
*
* @return the options
*/
public List<Option> getOptions() {
return options;
}
/**
* 枚举的选项列表
*
* @param options the options to set
* @return
*/
public EnumEntry setOptions(List<Option> options) {
this.options = options;
return this;
}
/**
* 枚举值的注释
*
* @return the comment
*/
public Comment getComment() {
return comment;
}
/**
* 枚举值的注释
*
* @param comment the comment to set
* @return
*/
public EnumEntry setComment(Comment comment) {
this.comment = comment;
return this;
}
}
/**
* 枚举名称
*
* @return the name
*/
public String getName() {
return name;
}
/**
* 枚举名称
*
* @param name the name to set
* @return
*/
public ProtoEnum setName(String name) {
this.name = name;
return this;
}
/**
* 枚举包含的值的定义
*
* @return the entries
*/
public List<EnumEntry> getEntries() {
return entries;
}
/**
* 枚举包含的值的定义
*
* @param entries the entries to set
* @return
*/
public ProtoEnum setEntries(List<EnumEntry> entries) {
this.entries = entries;
return this;
}
/**
* 枚举包含的选项列表
*
* @return the options
*/
public List<Option> getOptions() {
return options;
}
/**
* 枚举包含的选项列表
*
* @param options the options to set
* @return
*/
public ProtoEnum setOptions(List<Option> options) {
this.options = options;
return this;
}
}
@@ -53,7 +53,6 @@ public class ProtoParser {
protected static List<String> fieldCardinalities = new ArrayList<>();
private static final String VALUE_END_ERROR = " value not end with \";\"";
private static final String ONEOF_MSG = "oneof ";
private static final String ROW_MSG = "row [";
private static final String START_MSG = " start...";
@@ -411,9 +410,7 @@ public class ProtoParser {
addCommentLine(CommentType.INLINE, readSingleLineComment());
nextLine();
} else if (fieldCardinalities.contains(token)) {
Field field = parseField(
Cardinality.valueOf(token.toUpperCase(Locale.ENGLISH)),
null);
Field field = parseField(Cardinality.valueOf(token.toUpperCase(Locale.ENGLISH)), null);
msg.addField(field);
} else if ("enum".equals(token)) {
notSupportEnum();