From dbe1502a922e082354c5c64ffe37a684890c39c9 Mon Sep 17 00:00:00 2001 From: godotg Date: Thu, 30 May 2024 17:28:32 +0800 Subject: [PATCH] ref[Golang]: refactor generate Golang protocol --- .../generate/GenerateProtocolFile.java | 19 +- .../generate/GenerateProtocolNote.java | 17 +- .../registration/ProtocolAnalysis.java | 9 - .../protocol/serializer/CodeLanguage.java | 3 +- .../serializer/CutDownArraySerializer.java | 64 ++--- .../serializer/CutDownListSerializer.java | 36 +-- .../serializer/CutDownMapSerializer.java | 48 ++-- .../serializer/CutDownSetSerializer.java | 36 +-- .../serializer/csharp/CodeGenerateCsharp.java | 6 +- .../serializer/go/GenerateGoUtils.java | 203 -------------- .../serializer/golang/CodeGenerateGolang.java | 256 ++++++++++++++++++ .../{go => golang}/GoArraySerializer.java | 12 +- .../{go => golang}/GoBooleanSerializer.java | 2 +- .../{go => golang}/GoByteSerializer.java | 2 +- .../{go => golang}/GoDoubleSerializer.java | 2 +- .../{go => golang}/GoFloatSerializer.java | 2 +- .../{go => golang}/GoIntSerializer.java | 2 +- .../{go => golang}/GoListSerializer.java | 12 +- .../{go => golang}/GoLongSerializer.java | 2 +- .../{go => golang}/GoMapSerializer.java | 18 +- .../GoObjectProtocolSerializer.java | 2 +- .../{go => golang}/GoSetSerializer.java | 12 +- .../{go => golang}/GoShortSerializer.java | 2 +- .../{go => golang}/GoStringSerializer.java | 2 +- .../{go => golang}/IGoSerializer.java | 2 +- .../resources/go/ProtocolManagerTemplate.go | 5 - .../src/main/resources/go/ProtocolTemplate.go | 32 --- .../resources/{go => golang}/ByteBuffer.go | 2 +- .../resources/golang/ProtocolClassTemplate.go | 4 + .../golang/ProtocolManagerTemplate.go | 5 + .../golang/ProtocolRegistrationTemplate.go | 26 ++ .../ProtocolRegistrationTemplateEmpty.go} | 16 +- .../main/resources/golang/ProtocolTemplate.go | 5 + .../resources/golang/ProtocolsTemplate.go | 5 + 34 files changed, 445 insertions(+), 426 deletions(-) delete mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java create mode 100644 protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoArraySerializer.java (90%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoBooleanSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoByteSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoDoubleSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoFloatSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoIntSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoListSerializer.java (90%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoLongSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoMapSerializer.java (85%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoObjectProtocolSerializer.java (98%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoSetSerializer.java (90%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoShortSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/GoStringSerializer.java (97%) rename protocol/src/main/java/com/zfoo/protocol/serializer/{go => golang}/IGoSerializer.java (96%) delete mode 100644 protocol/src/main/resources/go/ProtocolManagerTemplate.go delete mode 100644 protocol/src/main/resources/go/ProtocolTemplate.go rename protocol/src/main/resources/{go => golang}/ByteBuffer.go (99%) create mode 100644 protocol/src/main/resources/golang/ProtocolClassTemplate.go create mode 100644 protocol/src/main/resources/golang/ProtocolManagerTemplate.go create mode 100644 protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go rename protocol/src/main/resources/{go/ProtocolTemplateEmpty.go => golang/ProtocolRegistrationTemplateEmpty.go} (53%) create mode 100644 protocol/src/main/resources/golang/ProtocolTemplate.go create mode 100644 protocol/src/main/resources/golang/ProtocolsTemplate.go diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java index 634e4ef8..46b42769 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolFile.java @@ -18,8 +18,6 @@ import com.zfoo.protocol.exception.UnknownException; import com.zfoo.protocol.registration.IProtocolRegistration; import com.zfoo.protocol.registration.ProtocolAnalysis; import com.zfoo.protocol.registration.ProtocolRegistration; -import com.zfoo.protocol.serializer.CodeLanguage; -import com.zfoo.protocol.serializer.go.GenerateGoUtils; import com.zfoo.protocol.util.FileUtils; import com.zfoo.protocol.util.ReflectionUtils; import com.zfoo.protocol.util.StringUtils; @@ -113,21 +111,7 @@ public abstract class GenerateProtocolFile { // 计算协议生成的路径 GenerateProtocolPath.initProtocolPath(generateProtocols); - var generateLanguages = generateOperation.getGenerateLanguages(); - - // 生成Golang协议 - if (generateLanguages.contains(CodeLanguage.Go)) { - GenerateGoUtils.init(generateOperation); - GenerateGoUtils.createProtocolManager(generateProtocols); - for (var protocolRegistration : generateProtocols) { - GenerateGoUtils.createGoProtocolFile((ProtocolRegistration) protocolRegistration); - } - } - for (var language : generateOperation.getGenerateLanguages()) { - if (language.codeGenerateClass == null) { - continue; - } var codeGenerate = ReflectionUtils.newInstance(language.codeGenerateClass); codeGenerate.init(generateOperation); if (generateOperation.isMergeProtocol()) { @@ -141,6 +125,9 @@ public abstract class GenerateProtocolFile { // 预留参数,以后可能会用,比如给Lua修改一个后缀名称 var protocolParam = generateOperation.getProtocolParam(); + + GenerateProtocolNote.clear(); + GenerateProtocolPath.clear(); } diff --git a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java index a33c23e6..34847b09 100644 --- a/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java +++ b/protocol/src/main/java/com/zfoo/protocol/generate/GenerateProtocolNote.java @@ -63,21 +63,6 @@ public abstract class GenerateProtocolNote { return formatNote(codeLanguage, classNote); } - public static String classNote(short protocolId, CodeLanguage language, String tab, int deep) { - var protocolNote = protocolNoteMap.get(protocolId); - var classNote = protocolNote.getKey(); - if (StringUtils.isBlank(classNote)) { - return StringUtils.EMPTY; - } - var multipleLineNotes = classNote.split(FileUtils.LS_REGEX); - var notes = new ArrayList(); - for(var oneLineNote : multipleLineNotes) { - var formatFieldNote = formatNote(language, oneLineNote); - notes.add(tab.repeat(Math.max(0, deep)) + formatFieldNote); - } - return StringUtils.joinWith(FileUtils.LS, notes.toArray()); - } - public static List fieldNotes(short protocolId, String fieldName, CodeLanguage language) { var protocolNote = protocolNoteMap.get(protocolId); var fieldNoteMap = protocolNote.getValue(); @@ -97,7 +82,7 @@ public abstract class GenerateProtocolNote { private static String formatNote(CodeLanguage language, String note) { switch (language) { case Cpp: - case Go: + case Golang: case JavaScript: case EcmaScript: case TypeScript: diff --git a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java index eec907ca..a5ce78f5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java +++ b/protocol/src/main/java/com/zfoo/protocol/registration/ProtocolAnalysis.java @@ -25,7 +25,6 @@ import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.generate.GenerateProtocolNote; import com.zfoo.protocol.generate.GenerateProtocolPath; import com.zfoo.protocol.registration.field.*; -import com.zfoo.protocol.serializer.go.GenerateGoUtils; import com.zfoo.protocol.serializer.reflect.*; import com.zfoo.protocol.util.*; import com.zfoo.protocol.xml.XmlProtocols; @@ -386,14 +385,6 @@ public class ProtocolAnalysis { unsupportedTypes = null; EnhanceUtils.clear(); - - if (CollectionUtils.isEmpty(generateOperation.getGenerateLanguages())) { - return; - } - - GenerateProtocolNote.clear(); - GenerateProtocolPath.clear(); - GenerateGoUtils.clear(); } public static List getFields(Class clazz) { diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java index 981039fb..d10a962d 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CodeLanguage.java @@ -16,6 +16,7 @@ import com.zfoo.protocol.serializer.cpp.CodeGenerateCpp; import com.zfoo.protocol.serializer.csharp.CodeGenerateCsharp; import com.zfoo.protocol.serializer.ecmascript.CodeGenerateEcmaScript; import com.zfoo.protocol.serializer.gdscript.CodeGenerateGdScript; +import com.zfoo.protocol.serializer.golang.CodeGenerateGolang; import com.zfoo.protocol.serializer.javascript.CodeGenerateJavaScript; import com.zfoo.protocol.serializer.lua.CodeGenerateLua; import com.zfoo.protocol.serializer.python.CodeGeneratePython; @@ -33,7 +34,7 @@ public enum CodeLanguage { Cpp(1 << 1, CodeGenerateCpp.class), - Go(1 << 2, null), + Golang(1 << 2, CodeGenerateGolang.class), JavaScript(1 << 3, CodeGenerateJavaScript.class), diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java index a8e86a2e..d73f1d3c 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownArraySerializer.java @@ -60,7 +60,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); break; case Cpp: @@ -88,7 +88,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); break; case Cpp: @@ -116,7 +116,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); break; case Cpp: @@ -144,7 +144,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); break; case Cpp: @@ -172,7 +172,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); break; case Cpp: @@ -200,7 +200,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); break; case Cpp: @@ -228,7 +228,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); break; case Cpp: @@ -256,7 +256,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); break; case Cpp: @@ -284,7 +284,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); break; case Cpp: @@ -312,7 +312,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); break; case Cpp: @@ -340,7 +340,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); break; case Cpp: @@ -368,7 +368,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); break; case Cpp: @@ -396,7 +396,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); break; case Cpp: @@ -424,7 +424,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); break; case Cpp: @@ -452,7 +452,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringArray({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteStringArray({})", objectStr)).append(LS); break; case Cpp: @@ -490,7 +490,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS); break; - case Go: + case Golang: case Protobuf: default: flag = false; @@ -529,7 +529,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", array)).append(LS); break; case Cpp: @@ -561,7 +561,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", array)).append(LS); break; case Cpp: @@ -593,7 +593,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", array)).append(LS); break; case Cpp: @@ -625,7 +625,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", array)).append(LS); break; case Cpp: @@ -657,7 +657,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", array)).append(LS); break; case Cpp: @@ -689,7 +689,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", array)).append(LS); break; case Cpp: @@ -721,7 +721,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", array)).append(LS); break; case Cpp: @@ -753,7 +753,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", array)).append(LS); break; case Cpp: @@ -785,7 +785,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", array)).append(LS); break; case Cpp: @@ -817,7 +817,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", array)).append(LS); break; case Cpp: @@ -849,7 +849,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", array)).append(LS); break; case Cpp: @@ -881,7 +881,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", array)).append(LS); break; case Cpp: @@ -913,7 +913,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", array)).append(LS); break; case Cpp: @@ -945,7 +945,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", array)).append(LS); break; case Cpp: @@ -977,7 +977,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringArray();", array)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadStringArray()", array)).append(LS); break; case Cpp: @@ -1017,7 +1017,7 @@ public class CutDownArraySerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, protocolId)).append(LS); break; - case Go: + case Golang: case Protobuf: case Enhance: default: diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java index 0f0b9f96..06af44f3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownListSerializer.java @@ -59,7 +59,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); break; case Cpp: @@ -87,7 +87,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); break; case Cpp: @@ -115,7 +115,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); break; case Cpp: @@ -143,7 +143,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); break; case Cpp: @@ -171,7 +171,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); break; case Cpp: @@ -200,7 +200,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); break; case Cpp: @@ -229,7 +229,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); break; case Cpp: @@ -258,7 +258,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringList({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteStringArray({})", objectStr)).append(LS); break; case Cpp: @@ -295,7 +295,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writePacketList({}, {});", objectStr, protocolId)).append(LS); break; - case Go: + case Golang: case Protobuf: default: flag = false; @@ -333,7 +333,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadBooleanList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", list)).append(LS); break; case Cpp: @@ -365,7 +365,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", list)).append(LS); break; case Cpp: @@ -397,7 +397,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", list)).append(LS); break; case Cpp: @@ -429,7 +429,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", list)).append(LS); break; case Cpp: @@ -461,7 +461,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", list)).append(LS); break; case Cpp: @@ -493,7 +493,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", list)).append(LS); break; case Cpp: @@ -525,7 +525,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", list)).append(LS); break; case Cpp: @@ -557,7 +557,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringList();", list)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadStringArray()", list)).append(LS); break; case Cpp: @@ -599,7 +599,7 @@ public class CutDownListSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketList({});", list, protocolId)).append(LS); break; - case Go: + case Golang: case Protobuf: default: flag = false; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java index 46a4c8e9..9edc5d7c 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownMapSerializer.java @@ -66,7 +66,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntIntMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteIntIntMap({})", objectStr)).append(LS); return true; case Cpp: @@ -91,7 +91,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntLongMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteIntLongMap({})", objectStr)).append(LS); return true; case Cpp: @@ -116,7 +116,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntStringMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteIntStringMap({})", objectStr)).append(LS); return true; case Cpp: @@ -147,7 +147,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; - case Go: + case Golang: case Protobuf: default: } @@ -168,7 +168,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongIntMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteLongIntMap({})", objectStr)).append(LS); return true; case Cpp: @@ -193,7 +193,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongLongMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteLongLongMap({})", objectStr)).append(LS); return true; case Cpp: @@ -218,7 +218,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongStringMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteLongStringMap({})", objectStr)).append(LS); return true; case Cpp: @@ -249,7 +249,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; - case Go: + case Golang: case Protobuf: default: } @@ -270,7 +270,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringIntMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteStringIntMap({})", objectStr)).append(LS); return true; case Cpp: @@ -295,7 +295,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringLongMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteStringLongMap({})", objectStr)).append(LS); return true; case Cpp: @@ -320,7 +320,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringStringMap({});", objectStr)).append(LS); return true; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteStringStringMap({})", objectStr)).append(LS); return true; case Cpp: @@ -351,7 +351,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS); return true; - case Go: + case Golang: case Protobuf: default: } @@ -390,7 +390,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntIntMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadIntIntMap()", map)).append(LS); return map; case Cpp: @@ -419,7 +419,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntLongMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadIntLongMap()", map)).append(LS); return map; case Cpp: @@ -448,7 +448,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntStringMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadIntStringMap()", map)).append(LS); return map; case Cpp: @@ -486,7 +486,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readIntPacketMap({});", map, protocolId)).append(LS); return map; - case Go: + case Golang: case Protobuf: } } @@ -508,7 +508,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongIntMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadLongIntMap()", map)).append(LS); return map; case Cpp: @@ -537,7 +537,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongLongMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadLongLongMap()", map)).append(LS); return map; case Cpp: @@ -566,7 +566,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongStringMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadLongStringMap()", map)).append(LS); return map; case Cpp: @@ -604,7 +604,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readLongPacketMap({});", map, protocolId)).append(LS); return map; - case Go: + case Golang: case Protobuf: default: } @@ -627,7 +627,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringIntMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadStringIntMap()", map)).append(LS); return map; case Cpp: @@ -656,7 +656,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringLongMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadStringLongMap()", map)).append(LS); return map; case Cpp: @@ -685,7 +685,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringStringMap();", map)).append(LS); return map; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadStringStringMap()", map)).append(LS); return map; case Cpp: @@ -723,7 +723,7 @@ public class CutDownMapSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readStringPacketMap({});", map, protocolId)).append(LS); return map; - case Go: + case Golang: case Protobuf: default: } diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java index 50b2c6c2..28f06850 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/CutDownSetSerializer.java @@ -62,7 +62,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteBooleanSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteBooleanArray({})", objectStr)).append(LS); break; case Cpp: @@ -92,7 +92,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteByteSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteByteArray({})", objectStr)).append(LS); break; case Cpp: @@ -122,7 +122,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteShortSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteShortArray({})", objectStr)).append(LS); break; case Cpp: @@ -152,7 +152,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteIntSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteIntArray({})", objectStr)).append(LS); break; case Cpp: @@ -182,7 +182,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteLongSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteLongArray({})", objectStr)).append(LS); break; case Cpp: @@ -212,7 +212,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteFloatSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteFloatArray({})", objectStr)).append(LS); break; case Cpp: @@ -242,7 +242,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteDoubleSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteDoubleArray({})", objectStr)).append(LS); break; case Cpp: @@ -272,7 +272,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("buffer.WriteStringSet({});", objectStr)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("buffer.WriteStringArray({})", objectStr)).append(LS); break; case Cpp: @@ -311,7 +311,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("buffer.writePacketSet({}, {});", objectStr, protocolId)).append(LS); break; - case Go: + case Golang: case Protobuf: default: flag = false; @@ -352,7 +352,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case Cpp: builder.append(StringUtils.format("auto {} = buffer.readBooleanSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", set)).append(LS); break; case JavaScript: @@ -381,7 +381,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadByteSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadByteArray()", set)).append(LS); break; case Cpp: @@ -413,7 +413,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadShortSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadShortArray()", set)).append(LS); break; case Cpp: @@ -445,7 +445,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadIntSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadIntArray()", set)).append(LS); break; case Cpp: @@ -477,7 +477,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadLongSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadLongArray()", set)).append(LS); break; case Cpp: @@ -509,7 +509,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadFloatSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadFloatArray()", set)).append(LS); break; case Cpp: @@ -541,7 +541,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadDoubleSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray()", set)).append(LS); break; case Cpp: @@ -573,7 +573,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case CSharp: builder.append(StringUtils.format("var {} = buffer.ReadStringSet();", set)).append(LS); break; - case Go: + case Golang: builder.append(StringUtils.format("var {} = buffer.ReadStringArray()", set)).append(LS); break; case Cpp: @@ -615,7 +615,7 @@ public class CutDownSetSerializer implements ICutDownSerializer { case TypeScript: builder.append(StringUtils.format("const {} = buffer.readPacketSet({});", set, protocolId)).append(LS); break; - case Go: + case Golang: case Protobuf: default: flag = false; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java index ee9ffc7c..c031abb4 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/csharp/CodeGenerateCsharp.java @@ -115,10 +115,10 @@ public class CodeGenerateCsharp implements ICodeGenerate { for (var registration : registrations) { - var protocolId = registration.protocolId(); - var protocolClazzName = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var protocol_id = registration.protocolId(); + var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); var formatProtocolTemplate = formatProtocolTemplate(registration); - var outputPath = StringUtils.format("{}/{}/{}.cs", protocolOutputPath, GenerateProtocolPath.capitalizeProtocolPathFold(protocolId), protocolClazzName); + var outputPath = StringUtils.format("{}/{}/{}.cs", protocolOutputPath, GenerateProtocolPath.capitalizeProtocolPathFold(protocol_id), protocol_name); var file = new File(outputPath); FileUtils.writeStringToFile(file, formatProtocolTemplate, true); logger.info("Generated C# protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); 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 deleted file mode 100644 index 333bde08..00000000 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GenerateGoUtils.java +++ /dev/null @@ -1,203 +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.go; - -import com.zfoo.protocol.anno.Compatible; -import com.zfoo.protocol.collection.ArrayUtils; -import com.zfoo.protocol.generate.GenerateOperation; -import com.zfoo.protocol.generate.GenerateProtocolFile; -import com.zfoo.protocol.generate.GenerateProtocolNote; -import com.zfoo.protocol.registration.ProtocolRegistration; -import com.zfoo.protocol.registration.field.IFieldRegistration; -import com.zfoo.protocol.serializer.CodeLanguage; -import com.zfoo.protocol.serializer.reflect.*; -import com.zfoo.protocol.util.ClassUtils; -import com.zfoo.protocol.util.FileUtils; -import com.zfoo.protocol.util.ReflectionUtils; -import com.zfoo.protocol.util.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import static com.zfoo.protocol.util.FileUtils.LS; -import static com.zfoo.protocol.util.StringUtils.TAB; - -/** - * @author godotg - */ -public abstract class GenerateGoUtils { - private static final Logger logger = LoggerFactory.getLogger(GenerateGoUtils.class); - // custom configuration - public static String protocolOutputRootPath = "zfoogo"; - private static String protocolOutputPath = StringUtils.EMPTY; - - private static Map goSerializerMap; - - public static IGoSerializer goSerializer(ISerializer serializer) { - return goSerializerMap.get(serializer); - } - - public static void init(GenerateOperation generateOperation) { - protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); - FileUtils.deleteFile(new File(protocolOutputPath)); - - goSerializerMap = new HashMap<>(); - goSerializerMap.put(BooleanSerializer.INSTANCE, new GoBooleanSerializer()); - goSerializerMap.put(ByteSerializer.INSTANCE, new GoByteSerializer()); - goSerializerMap.put(ShortSerializer.INSTANCE, new GoShortSerializer()); - goSerializerMap.put(IntSerializer.INSTANCE, new GoIntSerializer()); - goSerializerMap.put(LongSerializer.INSTANCE, new GoLongSerializer()); - goSerializerMap.put(FloatSerializer.INSTANCE, new GoFloatSerializer()); - goSerializerMap.put(DoubleSerializer.INSTANCE, new GoDoubleSerializer()); - goSerializerMap.put(StringSerializer.INSTANCE, new GoStringSerializer()); - goSerializerMap.put(ArraySerializer.INSTANCE, new GoArraySerializer()); - goSerializerMap.put(ListSerializer.INSTANCE, new GoListSerializer()); - goSerializerMap.put(SetSerializer.INSTANCE, new GoSetSerializer()); - goSerializerMap.put(MapSerializer.INSTANCE, new GoMapSerializer()); - goSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new GoObjectProtocolSerializer()); - } - - public static void clear() { - goSerializerMap = null; - protocolOutputRootPath = null; - protocolOutputPath = null; - } - - /** - * 生成协议依赖的工具类 - */ - public static void createProtocolManager(List protocolList) throws IOException { - var list = List.of("go/ByteBuffer.go"); - - for (var fileName : list) { - var fileInputStream = ClassUtils.getFileFromClassPath(fileName); - var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "go/"))); - FileUtils.writeInputStreamToFile(createFile, fileInputStream); - } - - var initProtocolBuilder = new StringBuilder(); - protocolList.stream() - .filter(it -> Objects.nonNull(it)) - .forEach(it -> initProtocolBuilder.append(TAB).append(StringUtils.format("Protocols[{}] = new({})", it.protocolId(), it.protocolConstructor().getDeclaringClass().getSimpleName())).append(LS)); - - var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("go/ProtocolManagerTemplate.go"); - protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, initProtocolBuilder.toString().trim()); - var file = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.go")); - FileUtils.writeStringToFile(file, protocolManagerTemplate, true); - logger.info("Generated Golang protocol manager file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); - } - - public static void createGoProtocolFile(ProtocolRegistration registration) throws IOException { - var protocolId = registration.protocolId(); - 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 classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Go, TAB, 0); - 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 outputPath = StringUtils.format("{}/{}.go", protocolOutputPath, protocolClazzName); - var file = new File(outputPath); - FileUtils.writeStringToFile(file, protocolTemplate, true); - logger.info("Generated Golang protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); - } - - private static String fieldDefinition(ProtocolRegistration registration) { - var protocolId = registration.protocolId(); - var fields = registration.getFields(); - var fieldRegistrations = registration.getFieldRegistrations(); - var goBuilder = new StringBuilder(); - var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass()); - for (int i = 0; i < sequencedFields.size(); i++) { - var field = sequencedFields.get(i); - IFieldRegistration fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)]; - var fieldName = StringUtils.capitalize(field.getName()); - var fieldType = goSerializer(fieldRegistration.serializer()).fieldType(field, fieldRegistration); - - var propertyFullName = StringUtils.format("{} {}", fieldName, fieldType); - // 生成注释 - var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.Go); - for(var fieldNote : fieldNotes) { - goBuilder.append(TAB).append(fieldNote).append(LS); - } - goBuilder.append(TAB).append(propertyFullName).append(LS); - } - return goBuilder.toString(); - } - - private static String writeObject(ProtocolRegistration registration) { - GenerateProtocolFile.localVariableId = 0; - var fields = registration.getFields(); - var fieldRegistrations = registration.getFieldRegistrations(); - var goBuilder = new StringBuilder(); - if (registration.isCompatible()) { - goBuilder.append(TAB).append("var beforeWriteIndex = buffer.WriteOffset()").append(LS); - goBuilder.append(TAB ).append(StringUtils.format("buffer.WriteInt({})", registration.getPredictionLength())).append(LS); - } else { - goBuilder.append(TAB).append("buffer.WriteInt(-1)").append(LS); - } - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - var fieldRegistration = fieldRegistrations[i]; - goSerializer(fieldRegistration.serializer()).writeObject(goBuilder, "message." + StringUtils.capitalize(field.getName()), 1, field, fieldRegistration); - } - if (registration.isCompatible()) { - goBuilder.append(TAB).append(StringUtils.format("buffer.AdjustPadding({}, beforeWriteIndex)", registration.getPredictionLength())).append(LS); - } - return goBuilder.toString(); - } - - private static String readObject(ProtocolRegistration registration) { - GenerateProtocolFile.localVariableId = 0; - var fields = registration.getFields(); - var fieldRegistrations = registration.getFieldRegistrations(); - var goBuilder = new StringBuilder(); - for (var i = 0; i < fields.length; i++) { - var field = fields[i]; - var fieldRegistration = fieldRegistrations[i]; - if (field.isAnnotationPresent(Compatible.class)) { - goBuilder.append(TAB ).append("if buffer.CompatibleRead(beforeReadIndex, length) {").append(LS); - var compatibleReadObject = goSerializer(fieldRegistration.serializer()).readObject(goBuilder, 2, field, fieldRegistration); - goBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {}", StringUtils.capitalize(field.getName()), compatibleReadObject)).append(LS); - goBuilder.append(TAB).append("}").append(LS); - continue; - } - var readObject = goSerializer(fieldRegistration.serializer()).readObject(goBuilder, 1, field, fieldRegistration); - goBuilder.append(TAB).append(StringUtils.format("packet.{} = {}", StringUtils.capitalize(field.getName()), readObject)).append(LS); - } - return goBuilder.toString(); - } - -} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java new file mode 100644 index 00000000..1f48c7c5 --- /dev/null +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/CodeGenerateGolang.java @@ -0,0 +1,256 @@ +/* + * 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.golang; + +import com.zfoo.protocol.anno.Compatible; +import com.zfoo.protocol.collection.ArrayUtils; +import com.zfoo.protocol.generate.GenerateOperation; +import com.zfoo.protocol.generate.GenerateProtocolFile; +import com.zfoo.protocol.generate.GenerateProtocolNote; +import com.zfoo.protocol.registration.ProtocolRegistration; +import com.zfoo.protocol.registration.field.IFieldRegistration; +import com.zfoo.protocol.serializer.CodeLanguage; +import com.zfoo.protocol.serializer.CodeTemplatePlaceholder; +import com.zfoo.protocol.serializer.ICodeGenerate; +import com.zfoo.protocol.serializer.reflect.*; +import com.zfoo.protocol.util.ClassUtils; +import com.zfoo.protocol.util.FileUtils; +import com.zfoo.protocol.util.ReflectionUtils; +import com.zfoo.protocol.util.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.zfoo.protocol.util.FileUtils.LS; +import static com.zfoo.protocol.util.StringUtils.TAB; + +/** + * @author godotg + */ +public class CodeGenerateGolang implements ICodeGenerate { + private static final Logger logger = LoggerFactory.getLogger(CodeGenerateGolang.class); + // custom configuration + public static String protocolOutputRootPath = "zfoogo"; + private static String protocolOutputPath = StringUtils.EMPTY; + + private static final Map goSerializerMap = new HashMap<>(); + + public static IGoSerializer goSerializer(ISerializer serializer) { + return goSerializerMap.get(serializer); + } + + @Override + public void init(GenerateOperation generateOperation) { + protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath); + FileUtils.deleteFile(new File(protocolOutputPath)); + + goSerializerMap.put(BooleanSerializer.INSTANCE, new GoBooleanSerializer()); + goSerializerMap.put(ByteSerializer.INSTANCE, new GoByteSerializer()); + goSerializerMap.put(ShortSerializer.INSTANCE, new GoShortSerializer()); + goSerializerMap.put(IntSerializer.INSTANCE, new GoIntSerializer()); + goSerializerMap.put(LongSerializer.INSTANCE, new GoLongSerializer()); + goSerializerMap.put(FloatSerializer.INSTANCE, new GoFloatSerializer()); + goSerializerMap.put(DoubleSerializer.INSTANCE, new GoDoubleSerializer()); + goSerializerMap.put(StringSerializer.INSTANCE, new GoStringSerializer()); + goSerializerMap.put(ArraySerializer.INSTANCE, new GoArraySerializer()); + goSerializerMap.put(ListSerializer.INSTANCE, new GoListSerializer()); + goSerializerMap.put(SetSerializer.INSTANCE, new GoSetSerializer()); + goSerializerMap.put(MapSerializer.INSTANCE, new GoMapSerializer()); + goSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new GoObjectProtocolSerializer()); + } + + @Override + public void mergerProtocol(List registrations) throws IOException { + createTemplateFile(); + createProtocolManagerFile(registrations); + + + var protocol_class = new StringBuilder(); + var protocol_registration = new StringBuilder(); + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + // protocol + protocol_class.append(protocol_class(registration)).append(LS); + // registration + protocol_registration.append(protocol_registration(registration)).append(LS); + } + + var protocolTemplate = ClassUtils.getFileFromClassPathToString("golang/ProtocolsTemplate.go"); + var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_class, protocol_class.toString() + , CodeTemplatePlaceholder.protocol_registration, protocol_registration.toString() + )); + var outputPath = StringUtils.format("{}/Protocols.go", protocolOutputPath); + var file = new File(outputPath); + FileUtils.writeStringToFile(file, formatProtocolTemplate, true); + logger.info("Generated Golang protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + } + + @Override + public void foldProtocol(List registrations) throws IOException { + defaultProtocol(registrations); + } + + @Override + public void defaultProtocol(List registrations) throws IOException { + createTemplateFile(); + createProtocolManagerFile(registrations); + + + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + var protocolTemplate = ClassUtils.getFileFromClassPathToString("golang/ProtocolTemplate.go"); + var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of( + CodeTemplatePlaceholder.protocol_class, protocol_class(registration) + , CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration) + )); + + var outputPath = StringUtils.format("{}/{}.go", protocolOutputPath, protocol_name); + var file = new File(outputPath); + FileUtils.writeStringToFile(file, formatProtocolTemplate, true); + logger.info("Generated Golang protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath()); + } + } + + private void createTemplateFile() throws IOException { + var list = List.of("golang/ByteBuffer.go"); + for (var fileName : list) { + var fileInputStream = ClassUtils.getFileFromClassPath(fileName); + var createFile = new File(StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "golang/"))); + FileUtils.writeInputStreamToFile(createFile, fileInputStream); + } + } + + private void createProtocolManagerFile(List registrations) throws IOException { + var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("golang/ProtocolManagerTemplate.go"); + var protocol_imports = new StringBuilder(); + var protocol_manager_registrations = new StringBuilder(); + for (var registration : registrations) { + var protocol_id = registration.protocolId(); + var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName(); + protocol_manager_registrations.append(StringUtils.format("Protocols[{}] = new({})", protocol_id, protocol_name)).append(LS); + } + var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString() + , CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString()); + var formatProtocolManagerTemplate = CodeTemplatePlaceholder.formatTemplate(protocolManagerTemplate, placeholderMap); + var protocolManagerFile = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.go")); + FileUtils.writeStringToFile(protocolManagerFile, formatProtocolManagerTemplate, true); + logger.info("Generated Golang protocol manager file:[{}] is in path:[{}]", protocolManagerFile.getName(), protocolManagerFile.getAbsolutePath()); + } + + private String protocol_class(ProtocolRegistration registration) { + var protocol_id = registration.protocolId(); + var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName(); + + var protocolClassTemplate = ClassUtils.getFileFromClassPathToString("golang/ProtocolClassTemplate.go"); + var placeholderMap = Map.of( + CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Golang) + , CodeTemplatePlaceholder.protocol_name, protocol_name + , CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) + , CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration) + ); + return CodeTemplatePlaceholder.formatTemplate(protocolClassTemplate, placeholderMap); + } + + private String protocol_registration(ProtocolRegistration registration) { + var protocol_id = registration.protocolId(); + var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName(); + + var protocolRegistrationTemplate = ArrayUtils.isEmpty(registration.getFields()) + ? ClassUtils.getFileFromClassPathToString("golang/ProtocolRegistrationTemplateEmpty.go") + : ClassUtils.getFileFromClassPathToString("golang/ProtocolRegistrationTemplate.go"); + + var placeholderMap = Map.of( + CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Golang) + , CodeTemplatePlaceholder.protocol_name, protocol_name + , CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id) + , CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration) + , CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration) + ); + return CodeTemplatePlaceholder.formatTemplate(protocolRegistrationTemplate, placeholderMap); + } + + private String protocol_field_definition(ProtocolRegistration registration) { + var protocolId = registration.protocolId(); + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var goBuilder = new StringBuilder(); + var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass()); + for (int i = 0; i < sequencedFields.size(); i++) { + var field = sequencedFields.get(i); + IFieldRegistration fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)]; + var fieldName = StringUtils.capitalize(field.getName()); + var fieldType = goSerializer(fieldRegistration.serializer()).fieldType(field, fieldRegistration); + + var propertyFullName = StringUtils.format("{} {}", fieldName, fieldType); + // 生成注释 + var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, field.getName(), CodeLanguage.Golang); + for (var fieldNote : fieldNotes) { + goBuilder.append(fieldNote).append(LS); + } + goBuilder.append(propertyFullName).append(LS); + } + return goBuilder.toString(); + } + + private String protocol_write_serialization(ProtocolRegistration registration) { + GenerateProtocolFile.localVariableId = 0; + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var goBuilder = new StringBuilder(); + if (registration.isCompatible()) { + goBuilder.append("var beforeWriteIndex = buffer.WriteOffset()").append(LS); + goBuilder.append(StringUtils.format("buffer.WriteInt({})", registration.getPredictionLength())).append(LS); + } else { + goBuilder.append("buffer.WriteInt(-1)").append(LS); + } + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + goSerializer(fieldRegistration.serializer()).writeObject(goBuilder, "message." + StringUtils.capitalize(field.getName()), 0, field, fieldRegistration); + } + if (registration.isCompatible()) { + goBuilder.append(StringUtils.format("buffer.AdjustPadding({}, beforeWriteIndex)", registration.getPredictionLength())).append(LS); + } + return goBuilder.toString(); + } + + private String protocol_read_deserialization(ProtocolRegistration registration) { + GenerateProtocolFile.localVariableId = 0; + var fields = registration.getFields(); + var fieldRegistrations = registration.getFieldRegistrations(); + var goBuilder = new StringBuilder(); + for (var i = 0; i < fields.length; i++) { + var field = fields[i]; + var fieldRegistration = fieldRegistrations[i]; + if (field.isAnnotationPresent(Compatible.class)) { + goBuilder.append("if buffer.CompatibleRead(beforeReadIndex, length) {").append(LS); + var compatibleReadObject = goSerializer(fieldRegistration.serializer()).readObject(goBuilder, 1, field, fieldRegistration); + goBuilder.append(TAB).append(StringUtils.format("packet.{} = {}", StringUtils.capitalize(field.getName()), compatibleReadObject)).append(LS); + goBuilder.append("}").append(LS); + continue; + } + var readObject = goSerializer(fieldRegistration.serializer()).readObject(goBuilder, 0, field, fieldRegistration); + goBuilder.append(StringUtils.format("packet.{} = {}", StringUtils.capitalize(field.getName()), readObject)).append(LS); + } + return goBuilder.toString(); + } + +} diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoArraySerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoArraySerializer.java similarity index 90% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoArraySerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoArraySerializer.java index 9196be6c..fc174c3c 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoArraySerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoArraySerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.ArrayField; @@ -33,14 +33,14 @@ public class GoArraySerializer implements IGoSerializer { public String fieldType(Field field, IFieldRegistration fieldRegistration) { var arrayField = (ArrayField) fieldRegistration; var registration = arrayField.getArrayElementRegistration(); - var type = GenerateGoUtils.goSerializer(registration.serializer()).fieldType(field, registration); + var type = CodeGenerateGolang.goSerializer(registration.serializer()).fieldType(field, registration); return StringUtils.format("[]{}", type); } @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Go)) { + if (CutDownArraySerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Golang)) { return; } @@ -66,7 +66,7 @@ public class GoArraySerializer implements IGoSerializer { String element = "element" + GenerateProtocolFile.localVariableId++; builder.append(StringUtils.format("var {} = {}[{}]", element, objectStr, i)).append(LS); - GenerateGoUtils.goSerializer(arrayField.getArrayElementRegistration().serializer()) + CodeGenerateGolang.goSerializer(arrayField.getArrayElementRegistration().serializer()) .writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); @@ -78,7 +78,7 @@ public class GoArraySerializer implements IGoSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Go); + var cutDown = CutDownArraySerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Golang); if (cutDown != null) { return cutDown; } @@ -101,7 +101,7 @@ public class GoArraySerializer implements IGoSerializer { GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); - var readObject = GenerateGoUtils.goSerializer(arrayField.getArrayElementRegistration().serializer()) + var readObject = CodeGenerateGolang.goSerializer(arrayField.getArrayElementRegistration().serializer()) .readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); builder.append(StringUtils.format("{}[{}] = {}", result, i, readObject)); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoBooleanSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoBooleanSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoBooleanSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoBooleanSerializer.java index 85ed7b9b..f1eed1b1 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoBooleanSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoBooleanSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoByteSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoByteSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoByteSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoByteSerializer.java index c5762560..fcdcbea8 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoByteSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoByteSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoDoubleSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoDoubleSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoDoubleSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoDoubleSerializer.java index f3b697f8..3845c437 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoDoubleSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoDoubleSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoFloatSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoFloatSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoFloatSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoFloatSerializer.java index adcd385a..c9073510 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoFloatSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoFloatSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoIntSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoIntSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoIntSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoIntSerializer.java index 5db76ffd..e81a36ff 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoIntSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoIntSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoListSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoListSerializer.java similarity index 90% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoListSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoListSerializer.java index c969e816..37ad4021 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoListSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoListSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; @@ -33,14 +33,14 @@ public class GoListSerializer implements IGoSerializer { public String fieldType(Field field, IFieldRegistration fieldRegistration) { var listField = (ListField) fieldRegistration; var registration = listField.getListElementRegistration(); - var type = GenerateGoUtils.goSerializer(registration.serializer()).fieldType(field, registration); + var type = CodeGenerateGolang.goSerializer(registration.serializer()).fieldType(field, registration); return StringUtils.format("[]{}", type); } @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Go)) { + if (CutDownListSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Golang)) { return; } @@ -67,7 +67,7 @@ public class GoListSerializer implements IGoSerializer { String element = "element" + GenerateProtocolFile.localVariableId++; builder.append(StringUtils.format("var {} = {}[{}]", element, objectStr, i)).append(LS); - GenerateGoUtils.goSerializer(listField.getListElementRegistration().serializer()) + CodeGenerateGolang.goSerializer(listField.getListElementRegistration().serializer()) .writeObject(builder, element, deep + 2, field, listField.getListElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); @@ -79,7 +79,7 @@ public class GoListSerializer implements IGoSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Go); + var cutDown = CutDownListSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Golang); if (cutDown != null) { return cutDown; } @@ -101,7 +101,7 @@ public class GoListSerializer implements IGoSerializer { GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); - var readObject = GenerateGoUtils.goSerializer(listField.getListElementRegistration().serializer()) + var readObject = CodeGenerateGolang.goSerializer(listField.getListElementRegistration().serializer()) .readObject(builder, deep + 2, field, listField.getListElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); builder.append(StringUtils.format("{}[{}] = {}", result, i, readObject)).append(LS); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoLongSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoLongSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoLongSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoLongSerializer.java index fd518308..df0083e3 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoLongSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoLongSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoMapSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoMapSerializer.java similarity index 85% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoMapSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoMapSerializer.java index 88fcc815..302cadd5 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoMapSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoMapSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; @@ -35,8 +35,8 @@ public class GoMapSerializer implements IGoSerializer { var keyRegistration = mapField.getMapKeyRegistration(); var valueRegistration = mapField.getMapValueRegistration(); - var keyType = GenerateGoUtils.goSerializer(keyRegistration.serializer()).fieldType(field, keyRegistration); - var valueType = GenerateGoUtils.goSerializer(valueRegistration.serializer()).fieldType(field, valueRegistration); + var keyType = CodeGenerateGolang.goSerializer(keyRegistration.serializer()).fieldType(field, keyRegistration); + var valueType = CodeGenerateGolang.goSerializer(valueRegistration.serializer()).fieldType(field, valueRegistration); return StringUtils.format("map[{}]{}", keyType, valueType); } @@ -44,7 +44,7 @@ public class GoMapSerializer implements IGoSerializer { @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Go)) { + if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Golang)) { return; } @@ -70,9 +70,9 @@ public class GoMapSerializer implements IGoSerializer { String value = "valueElement" + GenerateProtocolFile.localVariableId++; builder.append(StringUtils.format("for {}, {} := range {} {", key, value, objectStr)).append(LS); - GenerateGoUtils.goSerializer(mapField.getMapKeyRegistration().serializer()) + CodeGenerateGolang.goSerializer(mapField.getMapKeyRegistration().serializer()) .writeObject(builder, key, deep + 2, field, mapField.getMapKeyRegistration()); - GenerateGoUtils.goSerializer(mapField.getMapValueRegistration().serializer()) + CodeGenerateGolang.goSerializer(mapField.getMapValueRegistration().serializer()) .writeObject(builder, value, deep + 2, field, mapField.getMapValueRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); builder.append("}").append(LS); @@ -84,7 +84,7 @@ public class GoMapSerializer implements IGoSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Go); + var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Golang); if (cutDown != null) { return cutDown; } @@ -108,11 +108,11 @@ public class GoMapSerializer implements IGoSerializer { GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); - String keyObject = GenerateGoUtils.goSerializer(mapField.getMapKeyRegistration().serializer()) + String keyObject = CodeGenerateGolang.goSerializer(mapField.getMapKeyRegistration().serializer()) .readObject(builder, deep + 2, field, mapField.getMapKeyRegistration()); - String valueObject = GenerateGoUtils.goSerializer(mapField.getMapValueRegistration().serializer()) + String valueObject = CodeGenerateGolang.goSerializer(mapField.getMapValueRegistration().serializer()) .readObject(builder, deep + 2, field, mapField.getMapValueRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoObjectProtocolSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoObjectProtocolSerializer.java similarity index 98% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoObjectProtocolSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoObjectProtocolSerializer.java index 2475402b..8cee1321 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoObjectProtocolSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoObjectProtocolSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoSetSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoSetSerializer.java similarity index 90% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoSetSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoSetSerializer.java index 1b05949a..594541f8 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoSetSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoSetSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; @@ -33,14 +33,14 @@ public class GoSetSerializer implements IGoSerializer { public String fieldType(Field field, IFieldRegistration fieldRegistration) { var setField = (SetField) fieldRegistration; var registration = setField.getSetElementRegistration(); - var type = GenerateGoUtils.goSerializer(registration.serializer()).fieldType(field, registration); + var type = CodeGenerateGolang.goSerializer(registration.serializer()).fieldType(field, registration); return StringUtils.format("[]{}", type); } @Override public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Go)) { + if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.Golang)) { return; } @@ -67,7 +67,7 @@ public class GoSetSerializer implements IGoSerializer { String element = "element" + GenerateProtocolFile.localVariableId++; builder.append(StringUtils.format("var {} = {}[{}]", element, objectStr, i)).append(LS); - GenerateGoUtils.goSerializer(setField.getSetElementRegistration().serializer()) + CodeGenerateGolang.goSerializer(setField.getSetElementRegistration().serializer()) .writeObject(builder, element, deep + 2, field, setField.getSetElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 1); @@ -79,7 +79,7 @@ public class GoSetSerializer implements IGoSerializer { @Override public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) { GenerateProtocolFile.addTab(builder, deep); - var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Go); + var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.Golang); if (cutDown != null) { return cutDown; } @@ -101,7 +101,7 @@ public class GoSetSerializer implements IGoSerializer { GenerateProtocolFile.addTab(builder, deep + 1); builder.append(StringUtils.format("for {} := 0; {} < {}; {}++ {", i, i, size, i)).append(LS); - var readObject = GenerateGoUtils.goSerializer(setField.getSetElementRegistration().serializer()) + var readObject = CodeGenerateGolang.goSerializer(setField.getSetElementRegistration().serializer()) .readObject(builder, deep + 2, field, setField.getSetElementRegistration()); GenerateProtocolFile.addTab(builder, deep + 2); builder.append(StringUtils.format("{}[{}] = {}", result, i, readObject)).append(LS); diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoShortSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoShortSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoShortSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoShortSerializer.java index ad1e4fa4..5afc11ea 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoShortSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoShortSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoStringSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoStringSerializer.java similarity index 97% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/GoStringSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoStringSerializer.java index 02bc8ed8..d6353483 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/GoStringSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/GoStringSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.generate.GenerateProtocolFile; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/go/IGoSerializer.java b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/IGoSerializer.java similarity index 96% rename from protocol/src/main/java/com/zfoo/protocol/serializer/go/IGoSerializer.java rename to protocol/src/main/java/com/zfoo/protocol/serializer/golang/IGoSerializer.java index 68a09ff6..0a336198 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/go/IGoSerializer.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/golang/IGoSerializer.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and limitations under the License. */ -package com.zfoo.protocol.serializer.go; +package com.zfoo.protocol.serializer.golang; import com.zfoo.protocol.registration.field.IFieldRegistration; diff --git a/protocol/src/main/resources/go/ProtocolManagerTemplate.go b/protocol/src/main/resources/go/ProtocolManagerTemplate.go deleted file mode 100644 index f5f54697..00000000 --- a/protocol/src/main/resources/go/ProtocolManagerTemplate.go +++ /dev/null @@ -1,5 +0,0 @@ -package protocol - -func init() { - {} -} diff --git a/protocol/src/main/resources/go/ProtocolTemplate.go b/protocol/src/main/resources/go/ProtocolTemplate.go deleted file mode 100644 index 5b873100..00000000 --- a/protocol/src/main/resources/go/ProtocolTemplate.go +++ /dev/null @@ -1,32 +0,0 @@ -package protocol -{} -type {} struct { - {} -} - -func (protocol {}) ProtocolId() int16 { - return {} -} - -func (protocol {}) write(buffer *ByteBuffer, packet any) { - if packet == nil { - buffer.WriteInt(0) - return - } - var message = packet.(*{}) - {} -} - -func (protocol {}) read(buffer *ByteBuffer) any { - var packet = new({}) - var length = buffer.ReadInt() - if length == 0 { - return packet - } - var beforeReadIndex = buffer.ReadOffset() - {} - if length > 0 { - buffer.SetReadOffset(beforeReadIndex + length) - } - return packet -} diff --git a/protocol/src/main/resources/go/ByteBuffer.go b/protocol/src/main/resources/golang/ByteBuffer.go similarity index 99% rename from protocol/src/main/resources/go/ByteBuffer.go rename to protocol/src/main/resources/golang/ByteBuffer.go index b3be52d7..f99c9040 100644 --- a/protocol/src/main/resources/go/ByteBuffer.go +++ b/protocol/src/main/resources/golang/ByteBuffer.go @@ -1,4 +1,4 @@ -package protocol +package zfoogo import ( "bytes" diff --git a/protocol/src/main/resources/golang/ProtocolClassTemplate.go b/protocol/src/main/resources/golang/ProtocolClassTemplate.go new file mode 100644 index 00000000..e89f8839 --- /dev/null +++ b/protocol/src/main/resources/golang/ProtocolClassTemplate.go @@ -0,0 +1,4 @@ +${protocol_note} +type ${protocol_name} struct { + ${protocol_field_definition} +} \ No newline at end of file diff --git a/protocol/src/main/resources/golang/ProtocolManagerTemplate.go b/protocol/src/main/resources/golang/ProtocolManagerTemplate.go new file mode 100644 index 00000000..da318b69 --- /dev/null +++ b/protocol/src/main/resources/golang/ProtocolManagerTemplate.go @@ -0,0 +1,5 @@ +package zfoogo + +func init() { + ${protocol_manager_registrations} +} diff --git a/protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go b/protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go new file mode 100644 index 00000000..5cc712f2 --- /dev/null +++ b/protocol/src/main/resources/golang/ProtocolRegistrationTemplate.go @@ -0,0 +1,26 @@ +func (protocol ${protocol_name}) ProtocolId() int16 { + return ${protocol_id} +} + +func (protocol ${protocol_name}) write(buffer *ByteBuffer, packet any) { + if packet == nil { + buffer.WriteInt(0) + return + } + var message = packet.(*${protocol_name}) + ${protocol_write_serialization} +} + +func (protocol ${protocol_name}) read(buffer *ByteBuffer) any { + var packet = new(${protocol_name}) + var length = buffer.ReadInt() + if length == 0 { + return packet + } + var beforeReadIndex = buffer.ReadOffset() + ${protocol_read_deserialization} + if length > 0 { + buffer.SetReadOffset(beforeReadIndex + length) + } + return packet +} diff --git a/protocol/src/main/resources/go/ProtocolTemplateEmpty.go b/protocol/src/main/resources/golang/ProtocolRegistrationTemplateEmpty.go similarity index 53% rename from protocol/src/main/resources/go/ProtocolTemplateEmpty.go rename to protocol/src/main/resources/golang/ProtocolRegistrationTemplateEmpty.go index da779094..dc47d199 100644 --- a/protocol/src/main/resources/go/ProtocolTemplateEmpty.go +++ b/protocol/src/main/resources/golang/ProtocolRegistrationTemplateEmpty.go @@ -1,14 +1,8 @@ -package protocol -{} -type {} struct { - {} +func (protocol ${protocol_name}) ProtocolId() int16 { + return ${protocol_id} } -func (protocol {}) ProtocolId() int16 { - return {} -} - -func (protocol {}) write(buffer *ByteBuffer, packet any) { +func (protocol ${protocol_name}) write(buffer *ByteBuffer, packet any) { if packet == nil { buffer.WriteInt(0) return @@ -16,8 +10,8 @@ func (protocol {}) write(buffer *ByteBuffer, packet any) { buffer.WriteInt(-1) } -func (protocol {}) read(buffer *ByteBuffer) any { - var packet = new({}) +func (protocol ${protocol_name}) read(buffer *ByteBuffer) any { + var packet = new(${protocol_name}) var length = buffer.ReadInt() if length == 0 { return packet diff --git a/protocol/src/main/resources/golang/ProtocolTemplate.go b/protocol/src/main/resources/golang/ProtocolTemplate.go new file mode 100644 index 00000000..83d90460 --- /dev/null +++ b/protocol/src/main/resources/golang/ProtocolTemplate.go @@ -0,0 +1,5 @@ +package zfoogo + +${protocol_class} + +${protocol_registration} \ No newline at end of file diff --git a/protocol/src/main/resources/golang/ProtocolsTemplate.go b/protocol/src/main/resources/golang/ProtocolsTemplate.go new file mode 100644 index 00000000..83d90460 --- /dev/null +++ b/protocol/src/main/resources/golang/ProtocolsTemplate.go @@ -0,0 +1,5 @@ +package zfoogo + +${protocol_class} + +${protocol_registration} \ No newline at end of file