Skip to content

Commit

Permalink
perf[lua]: convert the array or map of Lua to a normal output string
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jan 29, 2024
1 parent 58fbb96 commit f94a89a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,7 @@ private static String toStringJsonTemplate(ProtocolRegistration registration) {
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("{}:[%s]", fieldName));
} else if (fieldRegistration instanceof MapField) {
params.add(StringUtils.format("{}:{%s}", fieldName));
} else {
params.add(StringUtils.format("{}:%s", fieldName));
}
params.add(StringUtils.format("{}:%s", fieldName));
}
luaBuilder.append(StringUtils.joinWith(", ", params.toArray()));
luaBuilder.append("}");
Expand All @@ -303,8 +296,10 @@ private static String toStringParams(ProtocolRegistration registration) {
for (var field : sequencedFields) {
var fieldName = field.getName();
var fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
if ((fieldRegistration instanceof ArrayField) || (fieldRegistration instanceof ListField) || (fieldRegistration instanceof MapField)) {
params.add(StringUtils.format("table.concat(self.{}, \", \")", fieldName));
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));
}
Expand Down
18 changes: 18 additions & 0 deletions protocol/src/main/resources/lua/ByteBuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ 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))
end
local result = table.concat(items, ", ")
return "[" .. result .. "]";
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

-------------------------------------构造器-------------------------------------
function ByteBuffer:new()
--buffer里的每一个元素为一个长度为1的字符串
Expand Down

0 comments on commit f94a89a

Please sign in to comment.