From efd01781e2d64e1977f459ecc0123f44be33eeb1 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 15 Oct 2023 19:54:03 +0800 Subject: [PATCH] feat[golang]: compatible field of inside protocol class --- .../serializer/cpp/GenerateCppUtils.java | 4 +-- .../serializer/csharp/GenerateCsUtils.java | 4 +-- .../serializer/go/GenerateGoUtils.java | 31 ++++++------------- .../serializer/lua/GenerateLuaUtils.java | 4 +-- .../protobuf/GenerateProtobufUtils.java | 8 ++--- .../resources/go/ProtocolTemplateEmpty.go | 23 -------------- .../protocol/generate/GenerateTesting.java | 11 ------- 7 files changed, 19 insertions(+), 66 deletions(-) delete mode 100644 protocol/src/main/resources/go/ProtocolTemplateEmpty.go diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java index a16228c85..21e253e03 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/GenerateCppUtils.java @@ -141,11 +141,11 @@ public static void createCppProtocolFile(ProtocolRegistration registration) thro , valueOfMethod.getValue().trim(), protocolId, protocolClazzName, operator.trim(), protocolClazzName, protocolId, protocolClazzName, writeObject.trim(), protocolClazzName, readObject.trim()); - var protocolOutputPath = StringUtils.format("{}/{}/{}.h" + var outputPath = StringUtils.format("{}/{}/{}.h" , GenerateCppUtils.protocolOutputPath , GenerateProtocolPath.getProtocolPath(protocolId) , protocolClazzName); - FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true); + FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true); } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java index 53a8dd27d..3fffb59f1 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/GenerateCsUtils.java @@ -118,11 +118,11 @@ public static void createCsProtocolFile(ProtocolRegistration registration) throw , protocolId, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, writeObject.trim() , protocolClazzName, protocolClazzName, readObject.trim()); - var protocolOutputPath = StringUtils.format("{}/{}/{}.cs" + var outputPath = StringUtils.format("{}/{}/{}.cs" , protocolOutputRootPath , GenerateProtocolPath.getCapitalizeProtocolPath(protocolId) , protocolClazzName); - FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true); + FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true); } private static String fieldDefinition(ProtocolRegistration registration) { diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java index 5abc8d0c5..6bdba8430 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java @@ -23,6 +23,7 @@ import com.zfoo.protocol.registration.ProtocolRegistration; import com.zfoo.protocol.registration.field.IFieldRegistration; import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.cpp.GenerateCppUtils; import com.zfoo.protocol.serializer.reflect.*; import com.zfoo.protocol.util.ClassUtils; import com.zfoo.protocol.util.FileUtils; @@ -114,39 +115,25 @@ public static void createGoProtocolFile(ProtocolRegistration registration) throw var registrationConstructor = registration.getConstructor(); var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName(); - var protocolTemplate = ArrayUtils.isEmpty(registration.getFields()) - ? ClassUtils.getFileFromClassPathToString("go/ProtocolTemplateEmpty.go") - : ClassUtils.getFileFromClassPathToString("go/ProtocolTemplate.go"); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("go/ProtocolTemplate.go"); var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Go); var fieldDefinition = fieldDefinition(registration); var writeObject = writeObject(registration); var readObject = readObject(registration); - if (ArrayUtils.isEmpty(registration.getFields())) { - protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim() - , protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName); - } else { - protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim() - , protocolClazzName, protocolId, protocolClazzName, protocolClazzName - , writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim()); - } - var protocolPath = GenerateProtocolPath.getProtocolPath(protocolId); + protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, fieldDefinition.trim() + , protocolClazzName, protocolId, protocolClazzName, protocolClazzName + , writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim()); + + var protocolPath = GenerateProtocolPath.protocolAbsolutePath(protocolId); if (StringUtils.isEmpty(protocolPath)) { protocolPath = protocolClazzName; } else if (protocolPath.contains(StringUtils.SLASH)) { protocolPath = StringUtils.substringAfterLast(protocolPath, StringUtils.SLASH); } - - var protocolOutputPath = StringUtils.format("{}/{}.go" - , protocolOutputRootPath - , protocolPath); - var protocolFile = new File(protocolOutputPath); - if (protocolFile.exists()) { - FileUtils.writeStringToFile(protocolFile, StringUtils.substringAfterFirst(protocolTemplate, "package protocol"), true); - } else { - FileUtils.writeStringToFile(protocolFile, protocolTemplate, true); - } + var outputPath = StringUtils.format("{}/{}/{}.go", protocolOutputPath, protocolOutputRootPath, protocolClazzName); + FileUtils.writeStringToFile(outputPath, protocolTemplate, true); } private static String fieldDefinition(ProtocolRegistration registration) { diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java index 3764cad1b..ce4d9c021 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/lua/GenerateLuaUtils.java @@ -124,9 +124,9 @@ public static void createLuaProtocolFile(ProtocolRegistration registration) thro , valueOfMethod.getKey().trim(), valueOfMethod.getValue().trim(), protocolClazzName, protocolId , protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim(), protocolClazzName); - var protocolOutputPath = StringUtils.format("{}/{}/{}.lua" + var outputPath = StringUtils.format("{}/{}/{}.lua" , protocolOutputRootPath, GenerateProtocolPath.getCapitalizeProtocolPath(protocolId), protocolClazzName); - FileUtils.writeStringToFile(new File(protocolOutputPath), protocolTemplate, true); + FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true); } private static Pair valueOfMethod(ProtocolRegistration registration) { 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 index f0b00cb08..3cf94ec67 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GenerateProtobufUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/protobuf/GenerateProtobufUtils.java @@ -119,8 +119,8 @@ public static void createProtocolManager(List allGenerate builder.append("}").append(LS); - var protocolOutputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protocolManagerName); - FileUtils.writeStringToFile(new File(protocolOutputPath), builder.toString(), true); + var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protocolManagerName); + FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true); } public static void createProtocols(List allGenerateProtocols) { @@ -168,8 +168,8 @@ public static void createProtocols(List allGenerateProtoc } String protoName = StringUtils.substringAfterLast(protos.getKey(), StringUtils.PERIOD); - var protocolOutputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protoName); - FileUtils.writeStringToFile(new File(protocolOutputPath), builder.toString(), true); + var outputPath = StringUtils.format("{}/{}.proto", protocolOutputRootPath, protoName); + FileUtils.writeStringToFile(new File(outputPath), builder.toString(), true); } } diff --git a/protocol/src/main/resources/go/ProtocolTemplateEmpty.go b/protocol/src/main/resources/go/ProtocolTemplateEmpty.go deleted file mode 100644 index a4a80cd2f..000000000 --- a/protocol/src/main/resources/go/ProtocolTemplateEmpty.go +++ /dev/null @@ -1,23 +0,0 @@ -package protocol -{} -type {} struct { - {} -} - -func (protocol {}) ProtocolId() int16 { - return {} -} - -func (protocol {}) write(buffer *ByteBuffer, packet any) { - if buffer.WritePacketFlag(packet) { - return - } -} - -func (protocol {}) read(buffer *ByteBuffer) any { - var packet = new({}) - if !buffer.ReadBool() { - return packet - } - return packet -} 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..04ab1927b 100644 --- a/protocol/src/test/java/com/zfoo/protocol/generate/GenerateTesting.java +++ b/protocol/src/test/java/com/zfoo/protocol/generate/GenerateTesting.java @@ -34,18 +34,7 @@ public void generateAllProtocols() { var generateLanguages = op.getGenerateLanguages(); // generate the jsProtocol folder and its corresponding js protocol file in the current protocol directory - generateLanguages.add(CodeLanguage.Cpp); generateLanguages.add(CodeLanguage.Go); - generateLanguages.add(CodeLanguage.JavaScript); - generateLanguages.add(CodeLanguage.TypeScript); - generateLanguages.add(CodeLanguage.Lua); - generateLanguages.add(CodeLanguage.CSharp); - 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);