Skip to content

Commit

Permalink
bugfix: request_encode, request_decode, response_encode, response_dec…
Browse files Browse the repository at this point in the history
…ode should handle nil
  • Loading branch information
cloudwu committed May 12, 2015
1 parent 312c9e8 commit b5c3a4d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
34 changes: 28 additions & 6 deletions sproto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,40 @@ end

function sproto:request_encode(protoname, tbl)
local p = queryproto(self, protoname)
return core.encode(p.request,tbl) , p.tag
local request = p.request
if request then
return core.encode(request,tbl) , p.tag
else
return "" , p.tag
end
end

function sproto:response_encode(protoname, tbl)
local p = queryproto(self, protoname)
return core.encode(p.response,tbl)
local response = p.response
if response then
return core.encode(response,tbl)
else
return ""
end
end

function sproto:request_decode(protoname, ...)
local p = queryproto(self, protoname)
return core.decode(p.request,...) , p.name
local request = p.request
if request then
return core.decode(request,...) , p.name
else
return nil, p.name
end
end

function sproto:response_decode(protoname, ...)
local p = queryproto(self, protoname)
return core.decode(p.response,...)
local response = p.response
if response then
return core.decode(response,...)
end
end

sproto.pack = core.pack
Expand All @@ -128,9 +146,13 @@ function sproto:default(typename, type)
else
local p = queryproto(self, typename)
if type == "REQUEST" then
return core.default(p.request)
if p.request then
return core.default(p.request)
end
elseif type == "RESPONSE" then
return core.default(p.response)
if p.response then
return core.default(p.response)
end
else
error "Invalid type"
end
Expand Down
8 changes: 7 additions & 1 deletion testrpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ foo 2 {
bar 3 {}
blackhole 4 {
request {}
}
]]

Expand All @@ -36,8 +35,15 @@ local client_proto = sproto.parse [[
}
]]

print("=== default table")

print_r(server_proto:default("package"))
print_r(server_proto:default("foobar", "REQUEST"))
assert(server_proto:default("foo", "REQUEST")==nil)
assert(server_proto:request_encode("foo")=="")
server_proto:response_encode("foo", { ok = true })
assert(server_proto:request_decode("blackhole")==nil)
assert(server_proto:response_decode("blackhole")==nil)

print("=== test 1")

Expand Down

0 comments on commit b5c3a4d

Please sign in to comment.