feat[es]: es support

This commit is contained in:
godotg
2024-04-20 14:57:28 +08:00
parent c9e30ba116
commit 4c5fb4e2a1
26 changed files with 3881 additions and 6 deletions
@@ -20,6 +20,7 @@ import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.cpp.GenerateCppUtils;
import com.zfoo.protocol.serializer.csharp.GenerateCsUtils;
import com.zfoo.protocol.serializer.es.GenerateEsUtils;
import com.zfoo.protocol.serializer.gdscript.GenerateGdUtils;
import com.zfoo.protocol.serializer.go.GenerateGoUtils;
import com.zfoo.protocol.serializer.javascript.GenerateJsUtils;
@@ -165,6 +166,15 @@ public abstract class GenerateProtocolFile {
GenerateJsUtils.createProtocolManager(allSortedGenerateProtocols);
}
// 生成Javascript协议
if (generateLanguages.contains(CodeLanguage.ES)) {
GenerateEsUtils.init(generateOperation);
for (var protocolRegistration : allSortedGenerateProtocols) {
GenerateEsUtils.createJsProtocolFile((ProtocolRegistration) protocolRegistration);
}
GenerateEsUtils.createProtocolManager(allSortedGenerateProtocols);
}
// 生成TypeScript协议
if (generateLanguages.contains(CodeLanguage.TypeScript)) {
GenerateTsUtils.init(generateOperation);
@@ -28,17 +28,19 @@ public enum CodeLanguage {
JavaScript(1 << 3),
TypeScript(1 << 4),
ES(1 << 4),
Lua(1 << 5),
TypeScript(1 << 5),
CSharp(1 << 6),
Lua(1 << 10),
GdScript(1 << 7),
CSharp(1 << 11),
Python(1 << 8),
GdScript(1 << 12),
Protobuf(1 << 12);
Python(1 << 13),
Protobuf(1 << 30);
public final int id;
@@ -65,6 +65,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS);
break;
@@ -92,6 +93,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeBooleanArray({});", objectStr)).append(LS);
break;
@@ -119,6 +121,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS);
break;
@@ -146,6 +149,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeByteArray({});", objectStr)).append(LS);
break;
@@ -173,6 +177,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS);
break;
@@ -200,6 +205,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeShortArray({});", objectStr)).append(LS);
break;
@@ -227,6 +233,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS);
break;
@@ -254,6 +261,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntArray({});", objectStr)).append(LS);
break;
@@ -281,6 +289,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS);
break;
@@ -308,6 +317,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongArray({});", objectStr)).append(LS);
break;
@@ -335,6 +345,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS);
break;
@@ -362,6 +373,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeFloatArray({});", objectStr)).append(LS);
break;
@@ -389,6 +401,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS);
break;
@@ -416,6 +429,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeDoubleArray({});", objectStr)).append(LS);
break;
@@ -443,6 +457,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeStringArray({});", objectStr)).append(LS);
break;
@@ -471,6 +486,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("buffer.writePacketArray<{}>({}, {});", EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), objectStr, protocolId)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writePacketArray({}, {});", objectStr, protocolId)).append(LS);
break;
@@ -520,6 +536,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS);
break;
@@ -551,6 +568,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readBooleanArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readBooleanArray();", array)).append(LS);
break;
@@ -582,6 +600,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS);
break;
@@ -613,6 +632,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readByteArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readByteArray();", array)).append(LS);
break;
@@ -644,6 +664,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS);
break;
@@ -675,6 +696,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readShortArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readShortArray();", array)).append(LS);
break;
@@ -706,6 +728,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS);
break;
@@ -737,6 +760,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntArray();", array)).append(LS);
break;
@@ -768,6 +792,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS);
break;
@@ -799,6 +824,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongArray();", array)).append(LS);
break;
@@ -830,6 +856,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS);
break;
@@ -861,6 +888,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readFloatArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readFloatArray();", array)).append(LS);
break;
@@ -892,6 +920,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS);
break;
@@ -923,6 +952,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readDoubleArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readDoubleArray();", array)).append(LS);
break;
@@ -954,6 +984,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readStringArray();", array)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readStringArray();", array)).append(LS);
break;
@@ -982,6 +1013,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readPacketArray<{}>({});", array, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readPacketArray({});", array, protocolId)).append(LS);
break;
@@ -64,6 +64,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeBooleanList({});", objectStr)).append(LS);
break;
@@ -91,6 +92,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeByteList({});", objectStr)).append(LS);
break;
@@ -118,6 +120,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeShortList({});", objectStr)).append(LS);
break;
@@ -145,6 +148,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntList({});", objectStr)).append(LS);
break;
@@ -172,6 +176,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongList({});", objectStr)).append(LS);
break;
@@ -200,6 +205,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeFloatList({});", objectStr)).append(LS);
break;
@@ -228,6 +234,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeDoubleList({});", objectStr)).append(LS);
break;
@@ -256,6 +263,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeStringList({});", objectStr)).append(LS);
break;
@@ -283,6 +291,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writePacketList({}, {});", objectStr, protocolId)).append(LS);
break;
@@ -331,6 +340,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readBooleanList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readBooleanList();", list)).append(LS);
break;
@@ -362,6 +372,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readByteList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readByteList();", list)).append(LS);
break;
@@ -393,6 +404,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readShortList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readShortList();", list)).append(LS);
break;
@@ -424,6 +436,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntList();", list)).append(LS);
break;
@@ -455,6 +468,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongList();", list)).append(LS);
break;
@@ -486,6 +500,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readFloatList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readFloatList();", list)).append(LS);
break;
@@ -517,6 +532,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readDoubleList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readDoubleList();", list)).append(LS);
break;
@@ -548,6 +564,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readStringList();", list)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readStringList();", list)).append(LS);
break;
@@ -578,6 +595,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readPacketList({});", list, protocolId)).append(LS);
break;
@@ -71,6 +71,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntIntMap({});", objectStr)).append(LS);
return true;
@@ -95,6 +96,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntLongMap({});", objectStr)).append(LS);
return true;
@@ -119,6 +121,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntStringMap({});", objectStr)).append(LS);
return true;
@@ -140,6 +143,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
@@ -169,6 +173,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongIntMap({});", objectStr)).append(LS);
return true;
@@ -193,6 +198,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongLongMap({});", objectStr)).append(LS);
return true;
@@ -217,6 +223,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongStringMap({});", objectStr)).append(LS);
return true;
@@ -238,6 +245,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
@@ -267,6 +275,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeStringIntMap({});", objectStr)).append(LS);
return true;
@@ -291,6 +300,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeStringLongMap({});", objectStr)).append(LS);
return true;
@@ -315,6 +325,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeStringStringMap({});", objectStr)).append(LS);
return true;
@@ -336,6 +347,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
return true;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
@@ -385,6 +397,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntIntMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntIntMap();", map)).append(LS);
return map;
@@ -413,6 +426,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntLongMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntLongMap();", map)).append(LS);
return map;
@@ -441,6 +455,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntStringMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntStringMap();", map)).append(LS);
return map;
@@ -467,6 +482,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntPacketMap({});", map, protocolId)).append(LS);
return map;
@@ -499,6 +515,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongIntMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongIntMap();", map)).append(LS);
return map;
@@ -527,6 +544,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongLongMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongLongMap();", map)).append(LS);
return map;
@@ -555,6 +573,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongStringMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongStringMap();", map)).append(LS);
return map;
@@ -581,6 +600,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongPacketMap({});", map, protocolId)).append(LS);
return map;
@@ -614,6 +634,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readStringIntMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readStringIntMap();", map)).append(LS);
return map;
@@ -642,6 +663,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readStringLongMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readStringLongMap();", map)).append(LS);
return map;
@@ -670,6 +692,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readStringStringMap();", map)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readStringStringMap();", map)).append(LS);
return map;
@@ -696,6 +719,7 @@ public class CutDownMapSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readStringPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
return map;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readStringPacketMap({});", map, protocolId)).append(LS);
return map;
@@ -67,6 +67,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeBooleanSet({});", objectStr)).append(LS);
break;
@@ -96,6 +97,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeByteSet({});", objectStr)).append(LS);
break;
@@ -125,6 +127,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeShortSet({});", objectStr)).append(LS);
break;
@@ -154,6 +157,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeIntSet({});", objectStr)).append(LS);
break;
@@ -183,6 +187,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeLongSet({});", objectStr)).append(LS);
break;
@@ -212,6 +217,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeFloatSet({});", objectStr)).append(LS);
break;
@@ -241,6 +247,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeDoubleSet({});", objectStr)).append(LS);
break;
@@ -270,6 +277,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writeStringSet({});", objectStr)).append(LS);
break;
@@ -299,6 +307,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
break;
case Cpp:
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("buffer.writePacketSet({}, {});", objectStr, protocolId)).append(LS);
break;
@@ -347,6 +356,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray()", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readBooleanSet();", set)).append(LS);
break;
@@ -378,6 +388,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readByteSet();", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readByteSet();", set)).append(LS);
break;
@@ -409,6 +420,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readShortSet();", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readShortSet();", set)).append(LS);
break;
@@ -440,6 +452,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readIntSet();", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readIntSet();", set)).append(LS);
break;
@@ -471,6 +484,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readLongSet();", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readLongSet();", set)).append(LS);
break;
@@ -502,6 +516,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readFloatSet();", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readFloatSet();", set)).append(LS);
break;
@@ -533,6 +548,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readDoubleSet();", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readDoubleSet();", set)).append(LS);
break;
@@ -564,6 +580,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readStringSet();", set)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readStringSet();", set)).append(LS);
break;
@@ -594,6 +611,7 @@ public class CutDownSetSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("auto {} = buffer.readPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
break;
case JavaScript:
case ES:
case TypeScript:
builder.append(StringUtils.format("const {} = buffer.readPacketSet({});", set, protocolId)).append(LS);
break;
@@ -0,0 +1,101 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.ArrayField;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownArraySerializer;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsArraySerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
var type = StringUtils.format("Array<{}>", GenerateTsUtils.toTsClassName(field.getType().getComponentType().getSimpleName()));
return new Triple<>(type, field.getName(), "[]");
}
@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.ES)) {
return;
}
ArrayField arrayField = (ArrayField) fieldRegistration;
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0);").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("} else {").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS);
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("{}.forEach({} => {", objectStr, element)).append(LS);
GenerateEsUtils.esSerializer(arrayField.getArrayElementRegistration().serializer())
.writeObject(builder, element, deep + 2, field, arrayField.getArrayElementRegistration());
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("});").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
}
@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.ES);
if (cutDown != null) {
return cutDown;
}
ArrayField arrayField = (ArrayField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = [];", result)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
String readObject = GenerateEsUtils.esSerializer(arrayField.getArrayElementRegistration().serializer())
.readObject(builder, deep + 2, field, arrayField.getArrayElementRegistration());
GenerateProtocolFile.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}.push({});", result, readObject)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsBooleanSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("boolean", field.getName(), "false");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeBoolean({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readBoolean(); ", result)).append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsByteSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("number", field.getName(), "0");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeByte({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readByte();", result)).append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsDoubleSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("number", field.getName(), "0");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeDouble({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readDouble();", result)).append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsFloatSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("number", field.getName(), "0");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeFloat({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readFloat();", result)).append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsIntSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("number", field.getName(), "0");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeInt({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readInt();", result)).append(LS);
return result;
}
}
@@ -0,0 +1,100 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ListField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownListSerializer;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsListSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(GenerateTsUtils.toTsClassName(field.getGenericType().toString()), field.getName(), "[]");
}
@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.ES)) {
return;
}
ListField listField = (ListField) fieldRegistration;
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0);").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("} else {").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt({}.length);", objectStr)).append(LS);
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("{}.forEach({} => {", objectStr, element)).append(LS);
GenerateEsUtils.esSerializer(listField.getListElementRegistration().serializer())
.writeObject(builder, element, deep + 2, field, listField.getListElementRegistration());
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("});").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
}
@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.ES);
if (cutDown != null) {
return cutDown;
}
ListField listField = (ListField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = [];", result)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
String readObject = GenerateEsUtils.esSerializer(listField.getListElementRegistration().serializer())
.readObject(builder, deep + 2, field, listField.getListElementRegistration());
GenerateProtocolFile.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}.push({});", result, readObject)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsLongSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("number", field.getName(), "0");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeLong({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readLong();", result)).append(LS);
return result;
}
}
@@ -0,0 +1,110 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.MapField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownMapSerializer;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsMapSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(GenerateTsUtils.toTsClassName(field.getGenericType().toString()), field.getName(), "new Map()");
}
@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.ES)) {
return;
}
MapField mapField = (MapField) fieldRegistration;
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0);").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("} else {").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt({}.size);", objectStr)).append(LS);
String key = "key" + GenerateProtocolFile.index.getAndIncrement();
String value = "value" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("{}.forEach(({}, {}) => {", objectStr, value, key)).append(LS);
GenerateEsUtils.esSerializer(mapField.getMapKeyRegistration().serializer())
.writeObject(builder, key, deep + 2, field, mapField.getMapKeyRegistration());
GenerateEsUtils.esSerializer(mapField.getMapValueRegistration().serializer())
.writeObject(builder, value, deep + 2, field, mapField.getMapValueRegistration());
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("});").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
}
@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.ES);
if (cutDown != null) {
return cutDown;
}
MapField mapField = (MapField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = new Map();", result)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
String keyObject = GenerateEsUtils.esSerializer(mapField.getMapKeyRegistration().serializer())
.readObject(builder, deep + 2, field, mapField.getMapKeyRegistration());
String valueObject = GenerateEsUtils.esSerializer(mapField.getMapValueRegistration().serializer())
.readObject(builder, deep + 2, field, mapField.getMapValueRegistration());
GenerateProtocolFile.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}.set({}, {});", result, keyObject, valueObject)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -0,0 +1,54 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ObjectProtocolField;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsObjectProtocolSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
var protocolSimpleName = EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId());
var type = StringUtils.format("{} | null", protocolSimpleName);
return new Triple<>(type, field.getName(), "null");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writePacket({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
ObjectProtocolField objectProtocolField = (ObjectProtocolField) fieldRegistration;
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readPacket({});", result, objectProtocolField.getProtocolId())).append(LS);
return result;
}
}
@@ -0,0 +1,101 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.SetField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownSetSerializer;
import com.zfoo.protocol.serializer.typescript.GenerateTsUtils;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsSetSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>(GenerateTsUtils.toTsClassName(field.getGenericType().toString()), field.getName(), "new Set()");
}
@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.ES)) {
return;
}
SetField setField = (SetField) fieldRegistration;
builder.append(StringUtils.format("if ({} === null) {", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("buffer.writeInt(0);").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("} else {").append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("buffer.writeInt({}.size);", objectStr)).append(LS);
String element = "element" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append(StringUtils.format("{}.forEach({} => {", objectStr, element)).append(LS);
GenerateEsUtils.esSerializer(setField.getSetElementRegistration().serializer())
.writeObject(builder, element, deep + 2, field, setField.getSetElementRegistration());
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("});").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
}
@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.ES);
if (cutDown != null) {
return cutDown;
}
SetField setField = (SetField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = new Set();", result)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("const {} = buffer.readInt();", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} > 0) {", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
String i = "index" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("for (let {} = 0; {} < {}; {}++) {", i, i, size, i)).append(LS);
String readObject = GenerateEsUtils.esSerializer(setField.getSetElementRegistration().serializer())
.readObject(builder, deep + 2, field, setField.getSetElementRegistration());
GenerateProtocolFile.addTab(builder, deep + 2);
builder.append(StringUtils.format("{}.add({});", result, readObject)).append(LS);
GenerateProtocolFile.addTab(builder, deep + 1);
builder.append("}").append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("}").append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsShortSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("number", field.getName(), "0");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeShort({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readShort();", result)).append(LS);
return result;
}
}
@@ -0,0 +1,47 @@
/*
* 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.es;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import static com.zfoo.protocol.util.FileUtils.LS;
/**
* @author godotg
*/
public class EsStringSerializer implements IEsSerializer {
@Override
public Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration) {
return new Triple<>("string", field.getName(), "\"\"");
}
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeString({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readString();", result)).append(LS);
return result;
}
}
@@ -0,0 +1,199 @@
/*
* 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.es;
import com.zfoo.protocol.anno.Compatible;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.generate.GenerateProtocolNote;
import com.zfoo.protocol.generate.GenerateProtocolPath;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
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 static com.zfoo.protocol.util.FileUtils.LS;
import static com.zfoo.protocol.util.StringUtils.TAB;
/**
* @author godotg
*/
public abstract class GenerateEsUtils {
private static final Logger logger = LoggerFactory.getLogger(GenerateEsUtils.class);
// custom configuration
public static String protocolOutputRootPath = "zfooes";
private static String protocolOutputPath = StringUtils.EMPTY;
private static Map<ISerializer, IEsSerializer> esSerializerMap;
public static IEsSerializer esSerializer(ISerializer serializer) {
return esSerializerMap.get(serializer);
}
public static void init(GenerateOperation generateOperation) {
protocolOutputPath = FileUtils.joinPath(generateOperation.getProtocolPath(), protocolOutputRootPath);
FileUtils.deleteFile(new File(protocolOutputPath));
esSerializerMap = new HashMap<>();
esSerializerMap.put(BooleanSerializer.INSTANCE, new EsBooleanSerializer());
esSerializerMap.put(ByteSerializer.INSTANCE, new EsByteSerializer());
esSerializerMap.put(ShortSerializer.INSTANCE, new EsShortSerializer());
esSerializerMap.put(IntSerializer.INSTANCE, new EsIntSerializer());
esSerializerMap.put(LongSerializer.INSTANCE, new EsLongSerializer());
esSerializerMap.put(FloatSerializer.INSTANCE, new EsFloatSerializer());
esSerializerMap.put(DoubleSerializer.INSTANCE, new EsDoubleSerializer());
esSerializerMap.put(StringSerializer.INSTANCE, new EsStringSerializer());
esSerializerMap.put(ArraySerializer.INSTANCE, new EsArraySerializer());
esSerializerMap.put(ListSerializer.INSTANCE, new EsListSerializer());
esSerializerMap.put(SetSerializer.INSTANCE, new EsSetSerializer());
esSerializerMap.put(MapSerializer.INSTANCE, new EsMapSerializer());
esSerializerMap.put(ObjectProtocolSerializer.INSTANCE, new EsObjectProtocolSerializer());
}
public static void clear() {
protocolOutputRootPath = null;
protocolOutputPath = null;
esSerializerMap = null;
}
public static void createProtocolManager(List<IProtocolRegistration> protocolList) throws IOException {
var list = List.of("es/buffer/ByteBuffer.mjs", "es/buffer/long.mjs", "es/buffer/longbits.mjs");
for (var fileName : list) {
var fileInputStream = ClassUtils.getFileFromClassPath(fileName);
var outputPath = StringUtils.format("{}/{}", protocolOutputPath, StringUtils.substringAfterFirst(fileName, "es/"));
var createFile = new File(outputPath);
FileUtils.writeInputStreamToFile(createFile, fileInputStream);
}
// 生成ProtocolManager.js文件
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("es/ProtocolManagerTemplate.mjs");
var importBuilder = new StringBuilder();
var initProtocolBuilder = new StringBuilder();
for (var protocol : protocolList) {
var protocolId = protocol.protocolId();
var protocolName = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
var path = GenerateProtocolPath.protocolAbsolutePath(protocol.protocolId(), CodeLanguage.ES);
importBuilder.append(StringUtils.format("import {} from './{}';", protocolName, path)).append(LS);
initProtocolBuilder.append(StringUtils.format("protocols.set({}, {});", protocolId, protocolName)).append(LS);
}
protocolManagerTemplate = StringUtils.format(protocolManagerTemplate, importBuilder.toString().trim(), StringUtils.EMPTY_JSON, initProtocolBuilder.toString().trim());
var file = new File(StringUtils.format("{}/{}", protocolOutputPath, "ProtocolManager.mjs"));
FileUtils.writeStringToFile(file, protocolManagerTemplate, true);
logger.info("Generated ES protocol manager file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
}
public static void createJsProtocolFile(ProtocolRegistration registration) throws IOException {
// 初始化index
GenerateProtocolFile.index.set(0);
var protocolId = registration.protocolId();
var registrationConstructor = registration.getConstructor();
var protocolClazzName = registrationConstructor.getDeclaringClass().getSimpleName();
var protocolTemplate = ClassUtils.getFileFromClassPathToString("es/ProtocolTemplate.mjs");
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.ES, TAB, 0);
var fieldDefinition = fieldDefinition(registration);
var writeObject = writeObject(registration);
var readObject = readObject(registration);
protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName
, fieldDefinition.trim(), protocolClazzName, protocolId, protocolClazzName
, writeObject.trim(), protocolClazzName, protocolClazzName, readObject.trim(), protocolClazzName);
var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
var file = new File(outputPath);
FileUtils.writeStringToFile(file, protocolTemplate, true);
logger.info("Generated ES protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
}
private static String fieldDefinition(ProtocolRegistration registration) {
var protocolId = registration.getId();
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
// when generate source code fields, use origin fields sort
var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass());
var fieldDefinitionBuilder = new StringBuilder();
for (var field : sequencedFields) {
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
var fieldName = field.getName();
// 生成注释
var fieldNotes = GenerateProtocolNote.fieldNotes(protocolId, fieldName, CodeLanguage.ES);
for(var fieldNote : fieldNotes) {
fieldDefinitionBuilder.append(TAB).append(fieldNote).append(LS);
}
var triple = esSerializer(fieldRegistration.serializer()).field(field, fieldRegistration);
fieldDefinitionBuilder.append(TAB)
.append(StringUtils.format("this.{} = {}; // {}", fieldName, triple.getRight(), triple.getLeft()))
.append(LS);
}
return fieldDefinitionBuilder.toString();
}
private static String writeObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var jsBuilder = new StringBuilder();
if (registration.isCompatible()) {
jsBuilder.append("const beforeWriteIndex = buffer.getWriteOffset();").append(LS);
jsBuilder.append(TAB).append(StringUtils.format("buffer.writeInt({});", registration.getPredictionLength())).append(LS);
} else {
jsBuilder.append(TAB).append("buffer.writeInt(-1);").append(LS);
}
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
esSerializer(fieldRegistration.serializer()).writeObject(jsBuilder, "packet." + field.getName(), 1, field, fieldRegistration);
}
if (registration.isCompatible()) {
jsBuilder.append(TAB).append(StringUtils.format("buffer.adjustPadding({}, beforeWriteIndex);", registration.getPredictionLength())).append(LS);
}
return jsBuilder.toString();
}
private static String readObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var jsBuilder = new StringBuilder();
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
var fieldRegistration = fieldRegistrations[i];
if (field.isAnnotationPresent(Compatible.class)) {
jsBuilder.append(TAB).append("if (buffer.compatibleRead(beforeReadIndex, length)) {").append(LS);
var compatibleReadObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 2, field, fieldRegistration);
jsBuilder.append(TAB + TAB).append(StringUtils.format("packet.{} = {};", field.getName(), compatibleReadObject)).append(LS);
jsBuilder.append(TAB).append("}").append(LS);
continue;
}
var readObject = esSerializer(fieldRegistration.serializer()).readObject(jsBuilder, 1, field, fieldRegistration);
jsBuilder.append(TAB).append(StringUtils.format("packet.{} = {};", field.getName(), readObject)).append(LS);
}
return jsBuilder.toString();
}
}
@@ -0,0 +1,35 @@
/*
* 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.es;
import com.zfoo.protocol.model.Triple;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import java.lang.reflect.Field;
/**
* @author godotg
*/
public interface IEsSerializer {
/**
* 获取属性的类型,名称,默认值
*/
Triple<String, String, String> field(Field field, IFieldRegistration fieldRegistration);
void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration);
String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration);
}
@@ -0,0 +1,32 @@
{}
const protocols = new Map();
const ProtocolManager = {};
// initProtocol
{}
ProtocolManager.getProtocol = function getProtocol(protocolId) {
const protocol = protocols.get(protocolId);
if (protocol === null) {
throw new Error('[protocolId:' + protocolId + ']协议不存在');
}
return protocol;
};
ProtocolManager.write = function write(buffer, packet) {
const protocolId = packet.protocolId();
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
};
ProtocolManager.read = function read(buffer) {
const protocolId = buffer.readShort();
const protocol = ProtocolManager.getProtocol(protocolId);
const packet = protocol.read(buffer);
return packet;
};
export default ProtocolManager;
@@ -0,0 +1,32 @@
{}
const {} = function() {
{}
};
{}.prototype.protocolId = function() {
return {};
};
{}.write = function(buffer, packet) {
if (packet === null) {
buffer.writeInt(0);
return;
}
{}
};
{}.read = function(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new {}();
{}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
};
export default {};
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,184 @@
// from protobuf
import Long from './long';
/**
* Constructs new long bits.
* @classdesc Helper class for working with the low and high bits of a 64 bit value.
* @memberof util
* @constructor
* @param {number} lo Low 32 bits, unsigned
* @param {number} hi High 32 bits, unsigned
*/
function Longbits(lo, hi) {
// note that the casts below are theoretically unnecessary as of today, but older statically
// generated converter code might still call the ctor with signed 32bits. kept for compat.
/**
* Low bits.
* @type {number}
*/
this.lo = lo >>> 0;
/**
* High bits.
* @type {number}
*/
this.hi = hi >>> 0;
}
/**
* Zig-zag encodes this long bits.
* @returns {util.Longbits} `this`
*/
Longbits.prototype.zzEncode = function zzEncode() {
const mask = this.hi >> 31;
this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
this.lo = (this.lo << 1 ^ mask) >>> 0;
return this;
};
/**
* Zig-zag decodes this long bits.
* @returns {util.Longbits} `this`
*/
Longbits.prototype.zzDecode = function zzDecode() {
const mask = -(this.lo & 1);
this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
this.hi = (this.hi >>> 1 ^ mask) >>> 0;
return this;
};
/**
* Converts this long bits to a long.
* @param {boolean} [unsigned=false] Whether unsigned or not
* @returns {Long} Long
*/
Longbits.prototype.toLong = function toLong(unsigned) {
return new Long(this.lo | 0, this.hi | 0, Boolean(unsigned));
};
/**
* Zero bits.
* @memberof util.LongBits
* @type {util.Longbits}
*/
const zero = Longbits.zero = new Longbits(0, 0);
function from(value) {
if (typeof value === 'number') {
return fromNumber(value);
}
if (typeof value === 'string' || value instanceof String) {
value = Long.fromString(value);
}
return value.low || value.high ? new Longbits(value.low >>> 0, value.high >>> 0) : zero;
}
/**
* Constructs new long bits from the specified number.
* @param {number} value Value
* @returns {util.Longbits} Instance
*/
function fromNumber(value) {
if (value === 0) {
return zero;
}
const sign = value < 0;
if (sign) {
value = -value;
}
let lo = value >>> 0;
let hi = (value - lo) / 4294967296 >>> 0;
if (sign) {
hi = ~hi >>> 0;
lo = ~lo >>> 0;
if (++lo > 4294967295) {
lo = 0;
if (++hi > 4294967295) {
hi = 0;
}
}
}
return new Longbits(lo, hi);
}
function writeVarint64(byteBuffer, value) {
let count = 0;
while (value.hi) {
byteBuffer.writeByte(value.lo & 127 | 128);
value.lo = (value.lo >>> 7 | value.hi << 25) >>> 0;
value.hi >>>= 7;
count = count + 7;
}
while (value.lo > 127) {
if (count >= 56) {
byteBuffer.writeByte(value.lo);
return;
}
byteBuffer.writeByte(value.lo & 127 | 128);
value.lo = value.lo >>> 7;
count = count + 7;
}
byteBuffer.writeByte(value.lo);
}
function readLongVarint(buffer) {
// tends to deopt with local vars for octet etc.
const bits = new Longbits(0, 0);
let i = 0;
const len = buffer.length;
let pos = 0;
if (len - pos > 4) { // fast route (lo)
for (; i < 4; ++i) {
// 1st..4th
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
// 5th
bits.lo = (bits.lo | (buffer[pos] & 127) << 28) >>> 0;
bits.hi = (bits.hi | (buffer[pos] & 127) >> 4) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
i = 0;
} else {
for (; i < 3; ++i) {
// 1st..3th
bits.lo = (bits.lo | (buffer[pos] & 127) << i * 7) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
// 4th
bits.lo = (bits.lo | (buffer[pos++] & 127) << i * 7) >>> 0;
return bits;
}
// 6th..9th
for (; i < 4; ++i) {
// 最后一位直接写入
if (pos === 8) {
bits.hi = (bits.hi | buffer[pos] << i * 7 + 3) >>> 0;
return bits;
}
bits.hi = (bits.hi | (buffer[pos] & 127) << i * 7 + 3) >>> 0;
if (buffer[pos++] < 128) {
return bits;
}
}
return bits;
}
export function writeInt64(byteBuffer, value) {
const bits = from(value).zzEncode();
writeVarint64(byteBuffer, bits);
}
export function readInt64(buffer) {
return readLongVarint(buffer).zzDecode().toLong(false);
}