moonbridge
diff moonbridge_http.lua @ 35:2d7e3028d993
Support iteration using pairs(...) on request.get_params, request.get_params_list, request.post_params, and request.post_params_list
author | jbe |
---|---|
date | Fri Feb 27 00:53:40 2015 +0100 (2015-02-27) |
parents | 59f485dc48ea |
children | b841dc424baf |
line diff
1.1 --- a/moonbridge_http.lua Tue Feb 17 16:54:35 2015 +0100 1.2 +++ b/moonbridge_http.lua Fri Feb 27 00:53:40 2015 +0100 1.3 @@ -87,11 +87,33 @@ 1.4 local new_params_list -- defined later 1.5 do 1.6 local params_list_mapping = setmetatable({}, {__mode="k"}) 1.7 + local function nextnonempty(tbl, key) 1.8 + while true do 1.9 + key = next(tbl, key) 1.10 + if key == nil then 1.11 + return nil 1.12 + end 1.13 + local value = tbl[key] 1.14 + if #value > 0 then 1.15 + return key, value 1.16 + end 1.17 + end 1.18 + end 1.19 + local function nextvalue(tbl, key) 1.20 + key = next(tbl, key) 1.21 + if key == nil then 1.22 + return nil 1.23 + end 1.24 + return key, tbl[key][1] 1.25 + end 1.26 local params_list_metatable = { 1.27 __index = function(self, key) 1.28 local tbl = {} 1.29 self[key] = tbl 1.30 return tbl 1.31 + end, 1.32 + __pairs = function(self) 1.33 + return nextnonempty, self, nil 1.34 end 1.35 } 1.36 local params_metatable = { 1.37 @@ -99,6 +121,9 @@ 1.38 local value = params_list_mapping[self][key][1] 1.39 self[key] = value 1.40 return value 1.41 + end, 1.42 + __pairs = function(self) 1.43 + return nextvalue, params_list_mapping[self], nil 1.44 end 1.45 } 1.46 -- returns a table to store key value-list pairs (i.e. multiple values),