-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaethrillian.lua
More file actions
113 lines (108 loc) · 3.77 KB
/
maethrillian.lua
File metadata and controls
113 lines (108 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
------------------------------------------------------------------------------
-- Maethrillian library
-- Sledmine
-- Version 4.0
-- Encode, decode tools for data manipulation
------------------------------------------------------------------------------
local glue = require "glue"
local maethrillian = {}
--- Compress table data in the given format
---@param inputTable table
---@param requestFormat table
---@param noHex boolean
---@return table
function maethrillian.encodeTable(inputTable, requestFormat, noHex)
local compressedTable = {}
for property, value in pairs(inputTable) do
if (type(value) ~= "table") then
local expectedProperty
local encodeFormat
for formatIndex, format in pairs(requestFormat) do
if (glue.arrayhas(format, property)) then
expectedProperty = format[1]
encodeFormat = format[2]
end
end
if (encodeFormat) then
if (not noHex) then
compressedTable[property] = glue.tohex(string.pack(encodeFormat, value))
else
compressedTable[property] = string.pack(encodeFormat, value)
end
else
if (expectedProperty == property) then
compressedTable[property] = value
end
end
end
end
return compressedTable
end
--- Format table into request string
---@param inputTable table
---@param requestFormat table
---@return string
function maethrillian.tableToRequest(inputTable, requestFormat, separator)
local requestData = {}
for property, value in pairs(inputTable) do
if (requestFormat) then
for formatIndex, format in pairs(requestFormat) do
if (glue.arrayhas(format, property)) then
requestData[formatIndex] = value
end
end
else
requestData[#requestData + 1] = value
end
end
return table.concat(requestData, separator)
end
--- Decompress table data given expected encoding format
---@param inputTable table
---@param requestFormat any
function maethrillian.decodeTable(inputTable, requestFormat)
local dataDecompressed = {}
for property, encodedValue in pairs(inputTable) do
-- Get encode format for current value
local encodeFormat
for formatIndex, format in pairs(requestFormat) do
if (glue.arrayhas(format, property)) then
encodeFormat = format[2]
end
end
if (encodeFormat) then
-- There is a compression format available
value = string.unpack(encodeFormat, glue.fromhex(tostring(encodedValue)))
elseif (tonumber(encodedValue)) then
-- Convert value into number
value = tonumber(encodedValue)
else
-- Value is just a string
value = encodedValue
end
dataDecompressed[property] = value
end
return dataDecompressed
end
--- Transform request into table given
---@param request string
---@param requestFormat table
function maethrillian.requestToTable(request, requestFormat, separator)
local outputTable = {}
local splitRequest = glue.string.split(request, separator)
for index, value in pairs(splitRequest) do
local currentFormat = requestFormat[index]
local propertyName = currentFormat[1]
local encodeFormat = currentFormat[2]
-- Convert value into number
local toNumberValue = tonumber(value)
if (not encodeFormat and toNumberValue) then
value = toNumberValue
end
if (propertyName) then
outputTable[propertyName] = value
end
end
return outputTable
end
return maethrillian