From b380c80b98081014b1e319148cfad0492c3c07d7 Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 21 Oct 2023 09:37:03 +0800 Subject: [PATCH] del[protobuf]: delete protobuf generate --- .../protobuf/GenerateProtobufUtils.java | 304 ------------------ .../serializer/protobuf/XmlProtobuf.java | 46 --- .../serializer/protobuf/XmlProtobufProto.java | 55 ---- .../protobuf/XmlProtobufProtocol.java | 27 -- .../protocol/generate/GenerateTesting.java | 4 - .../protocol/jprotobuf/JProtobufTesting.java | 11 - protocol/src/test/resources/protobuf.xml | 14 - 7 files changed, 461 deletions(-) delete mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GenerateProtobufUtils.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobuf.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProto.java delete mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProtocol.java delete mode 100644 protocol/src/test/resources/protobuf.xml diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GenerateProtobufUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GenerateProtobufUtils.java deleted file mode 100644 index 41fd09256..000000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GenerateProtobufUtils.java +++ /dev/null @@ -1,304 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * 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; - -import com.baidu.bjf.remoting.protobuf.annotation.Protobuf; -import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass; -import com.zfoo.protocol.ProtocolManager; -import com.zfoo.protocol.collection.ArrayUtils; -import com.zfoo.protocol.collection.CollectionUtils; -import com.zfoo.protocol.exception.RunException; -import com.zfoo.protocol.generate.GenerateOperation; -import com.zfoo.protocol.generate.GenerateProtocolNote; -import com.zfoo.protocol.generate.GenerateProtocolPath; -import com.zfoo.protocol.model.Pair; -import com.zfoo.protocol.registration.IProtocolRegistration; -import com.zfoo.protocol.registration.ProtocolRegistration; -import com.zfoo.protocol.registration.field.*; -import com.zfoo.protocol.serializer.CodeLanguage; -import com.zfoo.protocol.serializer.reflect.*; -import com.zfoo.protocol.util.ClassUtils; -import com.zfoo.protocol.util.DomUtils; -import com.zfoo.protocol.util.FileUtils; -import com.zfoo.protocol.util.StringUtils; - -import java.io.File; -import java.io.IOException; -import java.util.*; -import java.util.stream.Collectors; - -import static com.zfoo.protocol.util.FileUtils.LS; -import static com.zfoo.protocol.util.StringUtils.TAB; - -/** - * @author godotg - */ -public abstract class GenerateProtobufUtils { - - private static String protocolOutputRootPath = "protos/"; - private static String protocolManagerName = "protocols"; - public static final String NET_COMMON_MODULE = "common"; - - private static String protocolParam = null; - - public static String syntax() { - return StringUtils.format("syntax = {}{}{};", StringUtils.QUOTATION_MARK, "proto3", StringUtils.QUOTATION_MARK); - } - - public static String option(String optionKey, String optionValue) { - return StringUtils.format("option {} = {}{}{};", optionKey, StringUtils.QUOTATION_MARK, optionValue, StringUtils.QUOTATION_MARK); - } - - public static String importProto(String importValue) { - return StringUtils.format("import {}{}.proto{};", StringUtils.QUOTATION_MARK, importValue, StringUtils.QUOTATION_MARK); - } - - public static Map parseParam(String param) { - if (StringUtils.isEmpty(param)) { - return Map.of(); - } - var params = param.trim().split(StringUtils.SEMICOLON_REGEX); - - if (ArrayUtils.isEmpty(params)) { - throw new RunException("参数格式错误,格式protobuf=xxx;java=xxx"); - } - - var map = Arrays.stream(params) - .map(it -> it.trim()) - .map(it -> new Pair<>(StringUtils.substringBeforeFirst(it, StringUtils.EQUAL), StringUtils.substringAfterFirst(it, StringUtils.EQUAL))) - .collect(Collectors.toMap(key -> key.getKey(), value -> value.getValue())); - - return map; - } - - public static void init(GenerateOperation generateOperation) throws IOException { - protocolOutputRootPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); - - var protocolParam = generateOperation.getProtocolParam(); - - GenerateProtobufUtils.protocolParam = protocolParam; - - FileUtils.deleteFile(new File(protocolOutputRootPath)); - FileUtils.createDirectory(protocolOutputRootPath); - - } - - public static void createProtocolManager(List allGenerateProtocols) { - var builder = new StringBuilder(); - builder.append(syntax()); - builder.append(LS).append(LS); - - var protocolParam = GenerateProtobufUtils.protocolParam; - var optionMap = parseParam(protocolParam); - if (CollectionUtils.isNotEmpty(optionMap)) { - for (var option : optionMap.entrySet()) { - builder.append(option(option.getKey(), option.getValue())).append(LS); - } - } - builder.append(LS); - - builder.append("enum ProtocolManager {").append(LS); - - if (allGenerateProtocols.stream().noneMatch(it -> it.protocolId() == 0)) { - builder.append(TAB).append(StringUtils.format("{} = 0;", "ZFOO_AUTO_GENERATE_ZERO_ENUM_VALUE")).append(LS).append(LS); - } - - allGenerateProtocols.stream() - .sorted((a, b) -> a.protocolId() - b.protocolId()) - .forEach(it -> builder.append(TAB).append(StringUtils.format("{} = {};", it.protocolConstructor().getDeclaringClass().getSimpleName(), it.protocolId())).append(LS)); - - builder.append("}").append(LS); - - var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protocolManagerName); - FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true); - } - - public static void createProtocols(List allGenerateProtocols) { - var packageMap = new HashMap>(); - for (var protocolRegistration : allGenerateProtocols) { - var protocolId = protocolRegistration.protocolId(); - String packageName = GenerateProtocolPath.getProtocolPath(protocolId); - var packagList = packageMap.computeIfAbsent(packageName, k -> new ArrayList<>()); - packagList.add(protocolRegistration); - } - for (var protos : packageMap.entrySet()) { - var builder = new StringBuilder(); - builder.append(syntax()); - builder.append(LS).append(LS); - - var protocolParam = GenerateProtobufUtils.protocolParam; - var optionMap = parseParam(protocolParam); - if (CollectionUtils.isNotEmpty(optionMap)) { - for (var option : optionMap.entrySet()) { - builder.append(option(option.getKey(), option.getValue())).append(LS); - } - builder.append(LS); - } - builder.append(importProto(NET_COMMON_MODULE)).append(LS); - builder.append(LS); - - for (var protocolRegistration : protos.getValue()) { - var protocolClass = protocolRegistration.protocolConstructor().getDeclaringClass(); - var protocolId = protocolRegistration.protocolId(); - - if (!isProtobufProtocol((ProtocolRegistration) protocolRegistration)) { - continue; - } - - builder.append("// id = ").append(protocolId).append(LS); - - var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Protobuf); - if (StringUtils.isNotBlank(classNote)) { - builder.append(classNote).append(LS); - } - - builder.append(StringUtils.format("message {} {", protocolClass.getSimpleName())).append(LS); - builder.append(protobufFiled((ProtocolRegistration) protocolRegistration)); - builder.append("}").append(LS).append(LS); - } - - String protoName = StringUtils.substringAfterLast(protos.getKey(), StringUtils.PERIOD); - var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protoName); - FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true); - } - } - - /** - * 判断是否是protobuf协议 - * @param registration - * @return - */ - private static boolean isProtobufProtocol(ProtocolRegistration registration) { - var protocolClass = registration.protocolConstructor().getDeclaringClass(); - if (protocolClass.isAnnotationPresent(ProtobufClass.class)) { - return true; - } - var fields = registration.getFields(); - for (var field : fields) { - if (!field.isAnnotationPresent(Protobuf.class)) { - return false; - } - } - return true; - } - - /** - * 优化一下生成协议顺序 - * - * @param registration - * @return - */ - private static String protobufFiled(ProtocolRegistration registration) { - var protocolId = registration.getId(); - var fields = registration.getFields(); - var fieldRegistrations = registration.getFieldRegistrations(); - - // 先检查注解id顺序是否有重复 - var orderMap = new TreeMap(); - for (int i = 0, length = fields.length; i < length; i++) { - var field = fields[i]; - var fieldRegistration = fieldRegistrations[i]; - - var protobuf = field.getDeclaredAnnotation(Protobuf.class); - if (protobuf == null) { - throw new RunException("protobuf协议类必须加上注解[{}],并且标识order的顺序", Protobuf.class.getSimpleName()); - } - var order = protobuf.order(); - if (orderMap.containsKey(order)) { - throw new RunException("protobuf协议类注解[{}],order的顺序重复[{}]", Protobuf.class.getSimpleName(), order); - } - var builder = new StringBuilder(); - var fieldName = field.getName(); - // 生成注释 - var fieldNote = GenerateProtocolNote.fieldNote(protocolId, fieldName, CodeLanguage.Protobuf); - if (StringUtils.isNotBlank(fieldNote)) { - builder.append(TAB).append(fieldNote).append(LS); - } - - var singleFieldStr = toFieldTypeName(fieldRegistration); - if (StringUtils.isNotBlank(singleFieldStr)) { - builder.append(TAB).append(StringUtils.format("{} {} = {};", singleFieldStr, fieldName, order)).append(LS); - orderMap.put(order, builder.toString()); - continue; - } - - if (fieldRegistration instanceof ArrayField) { - var arrayField = (ArrayField) fieldRegistration; - var arrayFieldStr = toFieldTypeName(arrayField.getArrayElementRegistration()); - builder.append(TAB).append(StringUtils.format("repeated {} {} = {};", arrayFieldStr, fieldName, order)).append(LS); - } else if (fieldRegistration instanceof ListField) { - var listField = (ListField) fieldRegistration; - var listFieldStr = toFieldTypeName(listField.getListElementRegistration()); - builder.append(TAB).append(StringUtils.format("repeated {} {} = {};", listFieldStr, fieldName, order)).append(LS); - } else if (fieldRegistration instanceof SetField) { - var setField = (SetField) fieldRegistration; - var setFieldStr = toFieldTypeName(setField.getSetElementRegistration()); - builder.append(TAB).append(StringUtils.format("repeated {} {} = {};", setFieldStr, fieldName, order)).append(LS); - } else if (fieldRegistration instanceof MapField) { - var mapField = (MapField) fieldRegistration; - var keyFieldStr = toFieldTypeName(mapField.getMapKeyRegistration()); - var valueFieldStr = toFieldTypeName(mapField.getMapValueRegistration()); - builder.append(TAB).append(StringUtils.format("map<{}, {}> {} = {};", keyFieldStr, valueFieldStr, fieldName, order)).append(LS); - } else { - throw new RunException("无法识别的protobuf类型[{}]", field.getName()); - } - orderMap.put(order, builder.toString()); - } - - var builder = new StringBuilder(); - for (var entry : orderMap.entrySet()) { - builder.append(entry.getValue()); - } - return builder.toString(); - } - - - /** - * 注意protobuf不支持byte,short,char - */ - private static String toFieldTypeName(IFieldRegistration fieldRegistration) { - if (fieldRegistration instanceof BaseField) { - var serializer = fieldRegistration.serializer(); - if (serializer instanceof BooleanSerializer) { - return "bool"; - } else if (serializer instanceof ByteSerializer) { - return "byte"; - } else if (serializer instanceof ShortSerializer) { - return "int16"; - } else if (serializer instanceof IntSerializer) { - return "int32"; - } else if (serializer instanceof LongSerializer) { - return "int64"; - } else if (serializer instanceof FloatSerializer) { - return "float"; - } else if (serializer instanceof DoubleSerializer) { - return "double"; - } else if (serializer instanceof StringSerializer) { - return "string"; - } - } else if (fieldRegistration instanceof ObjectProtocolField) { - var objectProtocolField = (ObjectProtocolField) fieldRegistration; - return ProtocolManager.getProtocol(objectProtocolField.getProtocolId()).protocolConstructor().getDeclaringClass().getSimpleName(); - } - - return null; - } - - public static void clear() { - protocolOutputRootPath = null; - protocolManagerName = null; - protocolParam = null; - } - -} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobuf.java b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobuf.java deleted file mode 100644 index 23118e79f..000000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobuf.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * 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; - -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; - -import java.util.List; - -@JacksonXmlRootElement(localName = "protocols") -public class XmlProtobuf { - - @JacksonXmlProperty(isAttribute = true, localName = "syntax") - private String syntax; - - @JacksonXmlProperty(isAttribute = true, localName = "option") - private String option; - - @JacksonXmlProperty(localName = "proto") - @JacksonXmlElementWrapper(useWrapping = false) - private List protos; - - public String getSyntax() { - return syntax; - } - - public String getOption() { - return option; - } - - public List getProtos() { - return protos; - } -} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProto.java b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProto.java deleted file mode 100644 index 252b6596f..000000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProto.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * 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; - -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; - -import java.util.List; - -/** - * @author godotg - */ -public class XmlProtobufProto { - - - @JacksonXmlProperty(isAttribute = true, localName = "name") - private String name; - - @JacksonXmlProperty(isAttribute = true, localName = "import") - private String importProto; - - @JacksonXmlProperty(isAttribute = true, localName = "option") - private String option; - - @JacksonXmlProperty(localName = "protocol") - @JacksonXmlElementWrapper(useWrapping = false) - private List protocols; - - public String getName() { - return name; - } - - public String getImportProto() { - return importProto; - } - - public String getOption() { - return option; - } - - public List getProtocols() { - return protocols; - } -} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProtocol.java b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProtocol.java deleted file mode 100644 index 12b48ec7f..000000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/XmlProtobufProtocol.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2020 The zfoo Authors - * - * 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; - -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; - -public class XmlProtobufProtocol { - - @JacksonXmlProperty(isAttribute = true, localName = "location") - private String location; - - public String getLocation() { - return location; - } - -} diff --git a/protocol/src/test/java/com/zfoo/protocol/generate/GenerateTesting.java b/protocol/src/test/java/com/zfoo/protocol/generate/GenerateTesting.java index 6365311d7..c3a176c85 100644 --- a/protocol/src/test/java/com/zfoo/protocol/generate/GenerateTesting.java +++ b/protocol/src/test/java/com/zfoo/protocol/generate/GenerateTesting.java @@ -43,10 +43,6 @@ public void generateAllProtocols() { generateLanguages.add(CodeLanguage.GdScript); generateLanguages.add(CodeLanguage.Python); - // Protobuf needs to specify protobuf.xml to generate a protocol - op.setProtocolParam("protobuf=protobuf.xml"); - generateLanguages.add(CodeLanguage.Protobuf); - // Initialize and then generate the protocol ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), op); } diff --git a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java b/protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java index 0359aa6c7..b4db7c62b 100644 --- a/protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java +++ b/protocol/src/test/java/com/zfoo/protocol/jprotobuf/JProtobufTesting.java @@ -69,15 +69,4 @@ public void serializerTest() throws IOException { var newObj = simpleTypeCodec.decode(bytes); System.out.println(JsonUtils.object2String(newObj)); } - - @Test - public void generateTest() throws IOException { - var op = GenerateOperation.NO_OPERATION; - op.getGenerateLanguages().add(CodeLanguage.Protobuf); - op.setFoldProtocol(true); -// op.setProtocolParam("protobuf=protobuf.xml"); - - ProtocolManager.initProtocol(Set.of(ObjectA.class, ObjectB.class, ObjectC.class), op); - } - } diff --git a/protocol/src/test/resources/protobuf.xml b/protocol/src/test/resources/protobuf.xml deleted file mode 100644 index f075b2a47..000000000 --- a/protocol/src/test/resources/protobuf.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - -