-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompactVarDump
More file actions
165 lines (154 loc) · 5.39 KB
/
compactVarDump
File metadata and controls
165 lines (154 loc) · 5.39 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
module('AQ', package.seeall)
local descriptor = require "protobuf.descriptor"
local M = {}
KyleProtoFmt = M
local FieldDescriptor = descriptor.FieldDescriptor
local indentSymbol ="\t"
function Msg_Format_Indent(write, msg, indent)
local appendLine =function(content,lineIndent )
if content then
lineIndent = lineIndent or indent
write('\n')
write(string.rep(indentSymbol, lineIndent))
write(content)
end
end
for field, value in msg:ListFields() do
local print_field = function(field_value,fieldIndex, isRepeated,fieldIndent)
local name = field.name
if field.type == FieldDescriptor.TYPE_MESSAGE then
local extensions = getmetatable(msg)._extensions_by_name
if extensions[field.full_name] then
appendLine("[" .. name .. "] {")
else
if isRepeated then
appendLine(string.format('[%s]={',fieldIndex),fieldIndent)
else
appendLine(name .. "={")
end
end
Msg_Format_Indent(write, field_value, fieldIndent)
appendLine("},",fieldIndent)
else
if type(field_value) == 'string' then
appendLine(string.format("%s = '%s'", name, tostring(field_value)),fieldIndent)
else
appendLine(string.format("%s = %s", name, tostring(field_value)),fieldIndent)
end
end
end
if field.label == FieldDescriptor.LABEL_REPEATED then
local name = field.name
appendLine(string.format("%s = %s items{",name,#value))
for index , labelValue in ipairs(value) do
print_field(labelValue, index,true,indent+1)
end
appendLine("}")
else
print_field(value, 0,false,indent+1)
end
end
end
function M.Format(msg,indent)
if indent==nil then
indent=1
end
local out = {}
local write = function(value)
out[#out + 1] = value
end
local makeIndent =function()
write(string.rep(indentSymbol, indent))
end
write("{")
Msg_Format_Indent(write, msg, indent+1)
write('\n')
makeIndent(indent)
write("}")
return table.concat(out)
end
function KyleProtoFmt.ProtoBufToTableFormat(msg,indent)
return M.Format(msg,indent)
end
function IsProtoBuffTable(tableObj )
if tableObj and tableObj.ListFields~=nil and type(tableObj.ListFields)=='function' then
return true
end
return false
end
function GetVarDump(value,ignoreFunction , depth, key,stackDepth)
if value== nil then
return "nil value"
end
stackDepth = stackDepth or 0
depth = depth or -1
stackDepth=stackDepth+1
depth = depth + 1
--to prevent stack overflow
if stackDepth>8 then
return nil
end
local contentStr = ""
local addContent =function(str )
if str~=nil and str ~='' then
contentStr =string.format('%s%s', contentStr,tostring(str))
end
end
local makeIndent =function(indent)
indent = indent or depth
addContent(string.rep(indentSymbol, indent))
end
local addLine=function(str ,indent)
addContent('\n')
makeIndent(indent)
if str~=nil and str ~='' then
contentStr =string.format('%s%s', contentStr,tostring(str))
end
end
local linePrefix = ""
if key ~= nil then
linePrefix = tostring(key).." = "
end
if type(value) == 'table' then
if IsProtoBuffTable(value )==true then
addLine(string.format('%s(protoData)',linePrefix))
addContent(tostring(KyleProtoFmt.ProtoBufToTableFormat(value,depth)))
return contentStr
else
addLine(linePrefix)
addContent('{')
local mTable = getmetatable(value)
if mTable == nil then
for tableKey, tableValue in pairs(value) do
if value~=tableValue then
local content = GetVarDump(tableValue,ignoreFunction, depth, tableKey,stackDepth)
if content==nil then
--reachMaxStack
else
addContent(content)
end
end
end
else
addContent(string.format('(metatable)%s{',linePrefix))
for someKey,someValue in pairs(value) do
if someValue ~= value then
local content = GetVarDump(someValue ,ignoreFunction, depth, someKey,stackDepth,ignoreFunction)
addContent(tostring(content))
end
end
end
addLine('}')
end
elseif type(value) == 'function' then
if ignoreFunction~=true then
addLine(string.format('%s = %s (%s)',tostring(key),tostring(value),type(value)))
end
elseif type(value) == 'thread' or type(value) == 'userdata' or value == nil then
addLine(string.format('%s = %s (%s)',tostring(key),tostring(value),type(value)))
else
addLine(string.format('%s = %s (%s)',tostring(key),tostring(value),type(value)))
end
return contentStr
end
setglobal("GetVarDump",GetVarDump)