Skip to content

Commit

Permalink
perf[lua]: serialize table to json
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jan 29, 2024
1 parent f94a89a commit 6dbb718
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,23 @@
package com.zfoo.protocol.serializer.lua;

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.generate.GenerateProtocolPath;
import com.zfoo.protocol.registration.IProtocolRegistration;
import com.zfoo.protocol.registration.ProtocolRegistration;
import com.zfoo.protocol.registration.field.ArrayField;
import com.zfoo.protocol.registration.field.ListField;
import com.zfoo.protocol.registration.field.MapField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.csharp.GenerateCsUtils;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -132,13 +126,11 @@ public static void createLuaProtocolsInOneFile(List<IProtocolRegistration> regis

var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Lua);
var valueOfMethod = valueOfMethod(registration);
var toStringJsonTemplate = toStringJsonTemplate(registration);
var toStringParams = toStringParams(registration);
var writePacket = writePacket(registration);
var readPacket = readPacket(registration);

var protocol = StringUtils.format(protocolTemplate, classNote, protocolClazzName, StringUtils.EMPTY_JSON, protocolClazzName, valueOfMethod.trim());
var protocolBase = StringUtils.format(protocolBaseTemplate, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName, toStringJsonTemplate, toStringParams);
var protocolBase = StringUtils.format(protocolBaseTemplate, protocolClazzName, protocolId, protocolClazzName, protocolClazzName, protocolClazzName);
var protocolWriter = StringUtils.format(protocolWriterTemplate, protocolClazzName, writePacket.trim());
var protocolReader = StringUtils.format(protocolReaderTemplate, protocolClazzName, protocolClazzName, readPacket.trim());

Expand Down Expand Up @@ -221,15 +213,12 @@ public static void createLuaProtocolFile(ProtocolRegistration registration) {

var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.Lua);
var valueOfMethod = valueOfMethod(registration);
var toStringJsonTemplate = toStringJsonTemplate(registration);
var toStringParams = toStringParams(registration);
var writePacket = writePacket(registration);
var readPacket = readPacket(registration);

protocolTemplate = StringUtils.format(protocolTemplate, classNote, protocolClazzName, StringUtils.EMPTY_JSON, protocolClazzName
, valueOfMethod.trim(), protocolClazzName, protocolId, protocolClazzName, protocolClazzName
, protocolClazzName, toStringJsonTemplate, toStringParams
, protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim(), protocolClazzName);
, protocolClazzName, protocolClazzName, writePacket.trim(), protocolClazzName, protocolClazzName, readPacket.trim(), protocolClazzName);

var outputPath = StringUtils.format("{}/{}/{}.lua"
, protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
Expand Down Expand Up @@ -266,48 +255,6 @@ private static String valueOfMethod(ProtocolRegistration registration) {
return luaBuilder.toString();
}

private static String toStringJsonTemplate(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var luaBuilder = new StringBuilder();
luaBuilder.append("{");
// when generate source code fields, use origin fields sort
var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass());
var params = new ArrayList<String>();
for (var field : sequencedFields) {
var fieldName = field.getName();
params.add(StringUtils.format("{}:%s", fieldName));
}
luaBuilder.append(StringUtils.joinWith(", ", params.toArray()));
luaBuilder.append("}");
return luaBuilder.toString();
}

private static String toStringParams(ProtocolRegistration registration) {
var fields = registration.getFields();
if (ArrayUtils.isEmpty(fields)) {
return StringUtils.EMPTY_JSON;
}
var fieldRegistrations = registration.getFieldRegistrations();
var luaBuilder = new StringBuilder();
// when generate source code fields, use origin fields sort
var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass());
var params = new ArrayList<String>();
for (var field : sequencedFields) {
var fieldName = field.getName();
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
if ((fieldRegistration instanceof ArrayField) || (fieldRegistration instanceof ListField)) {
params.add(StringUtils.format("table.arrayToString(self.{})", fieldName));
} else if (fieldRegistration instanceof MapField) {
params.add(StringUtils.format("table.mapToString(self.{})", fieldName));
} else {
params.add(StringUtils.format("self.{}", fieldName));
}
}
luaBuilder.append(StringUtils.joinWith(", ", params.toArray()));
return luaBuilder.toString();
}

private static String writePacket(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
Expand Down
4 changes: 1 addition & 3 deletions protocol/src/main/resources/lua-one/ProtocolBaseTemplate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ function Protocols.{}:protocolName()
end

function Protocols.{}:__tostring()
local jsonTemplate = "{}"
local result = string.format(jsonTemplate, {})
return result
return table.serializeTableToJson(self)
end
27 changes: 13 additions & 14 deletions protocol/src/main/resources/lua/ByteBuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@ local ByteBuffer = {}
local trueBooleanStrValue = string.char(1)
local falseBooleanStrValue = string.char(0)

function table.arrayToString(array)
local items = {}
for index, element in pairs(array) do
table.insert(items, tostring(element))
function serializeTableToJson(tbl)
local res = {}
for k, v in pairs(tbl) do
local key = tostring(k)
if type(v) == "number" then
res[#res + 1] = key .. ":" .. v
elseif type(v) == "string" then
res[#res + 1] = key .. ':"' .. v .. '"'
elseif type(v) == "table" then
res[#res + 1] = key .. ":" .. serializeTableToJson(v)
end
end
local result = table.concat(items, ", ")
return "[" .. result .. "]";
return "{" .. table.concat(res, ",") .. "}"
end

function table.mapToString(map)
local items = {}
for key, value in pairs(map) do
table.insert(items, key .. ":" .. value)
end
local result = table.concat(items, ", ")
return "{" .. result .. "}";
end
table.serializeTableToJson = serializeTableToJson;

-------------------------------------构造器-------------------------------------
function ByteBuffer:new()
Expand Down
4 changes: 1 addition & 3 deletions protocol/src/main/resources/lua/ProtocolTemplate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ function {}:protocolName()
end

function {}:__tostring()
local jsonTemplate = "{}"
local result = string.format(jsonTemplate, {})
return result
return table.serializeTableToJson(self)
end

function {}:write(buffer, packet)
Expand Down

0 comments on commit 6dbb718

Please sign in to comment.