ref[Golang]: refactor generate Golang protocol

This commit is contained in:
godotg
2024-05-30 17:28:32 +08:00
parent 6be58a928a
commit dbe1502a92
34 changed files with 445 additions and 426 deletions
@@ -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();
}
@@ -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<String>();
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<String> 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:
@@ -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<Field> getFields(Class<?> clazz) {
@@ -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),
@@ -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:
@@ -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;
@@ -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:
}
@@ -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;
@@ -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());
@@ -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<ISerializer, IGoSerializer> 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<ProtocolRegistration> 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();
}
}
@@ -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<ISerializer, IGoSerializer> 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<ProtocolRegistration> 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<ProtocolRegistration> registrations) throws IOException {
defaultProtocol(registrations);
}
@Override
public void defaultProtocol(List<ProtocolRegistration> 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<ProtocolRegistration> 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();
}
}
@@ -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));
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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);
@@ -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;
@@ -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);
@@ -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;
@@ -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);
@@ -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;
@@ -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;
@@ -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;
@@ -1,5 +0,0 @@
package protocol
func init() {
{}
}
@@ -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
}
@@ -1,4 +1,4 @@
package protocol
package zfoogo
import (
"bytes"
@@ -0,0 +1,4 @@
${protocol_note}
type ${protocol_name} struct {
${protocol_field_definition}
}
@@ -0,0 +1,5 @@
package zfoogo
func init() {
${protocol_manager_registrations}
}
@@ -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
}
@@ -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
@@ -0,0 +1,5 @@
package zfoogo
${protocol_class}
${protocol_registration}
@@ -0,0 +1,5 @@
package zfoogo
${protocol_class}
${protocol_registration}