Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
local print = print | ||
local tconcat = table.concat | ||
local tinsert = table.insert | ||
local srep = string.rep | ||
local type = type | ||
local pairs = pairs | ||
local tostring = tostring | ||
local next = next | ||
|
||
local function print_r(root) | ||
local cache = { [root] = "." } | ||
local function _dump(t,space,name) | ||
local temp = {} | ||
for k,v in pairs(t) do | ||
local key = tostring(k) | ||
if cache[v] then | ||
tinsert(temp,"+" .. key .. " {" .. cache[v].."}") | ||
elseif type(v) == "table" then | ||
local new_key = name .. "." .. key | ||
cache[v] = new_key | ||
tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key)) | ||
else | ||
tinsert(temp,"+" .. key .. " [" .. tostring(v).."]") | ||
end | ||
end | ||
return tconcat(temp,"\n"..space) | ||
end | ||
print(_dump(root, "","")) | ||
end | ||
|
||
return print_r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
local parser = require "sprotoparser" | ||
local core = require "sproto.core" | ||
local print_r = require "print_r" | ||
|
||
local sp = parser.parse [[ | ||
.foobar { | ||
.nest { | ||
a 1 : string | ||
b 3 : boolean | ||
c 5 : integer | ||
} | ||
a 0 : string | ||
b 1 : integer | ||
c 2 : boolean | ||
d 3 : nest | ||
e 4 : *string | ||
f 5 : *integer | ||
g 6 : *boolean | ||
h 7 : *foobar | ||
} | ||
]] | ||
|
||
sp = core.newproto(sp) | ||
core.dumpproto(sp) | ||
|
||
local st = core.querytype(sp, "foobar") | ||
|
||
local obj = { | ||
a = "hello", | ||
b = 1000000, | ||
c = true, | ||
d = { | ||
a = "world", | ||
-- skip b | ||
c = -1, | ||
}, | ||
e = { "ABC", "def" }, | ||
f = { -3, -2, -1, 0 , 1, 2}, | ||
g = { true, false, true }, | ||
h = { | ||
{ b = 100 }, | ||
{ b = -100, c= false }, | ||
{ b = 0, e = { "test" } }, | ||
}, | ||
} | ||
|
||
local code = core.encode(st, obj) | ||
parser.dump(code) | ||
obj = core.decode(st, code) | ||
print_r(obj) | ||
|
||
|