# HG changeset patch # User jbe # Date 1424994820 -3600 # Node ID 2d7e3028d99316f3bbab6812fcaf9fecf7b8e6c7 # Parent de20248b53c28ba4b3e712c0108f451f51c7e397 Support iteration using pairs(...) on request.get_params, request.get_params_list, request.post_params, and request.post_params_list diff -r de20248b53c2 -r 2d7e3028d993 moonbridge_http.lua --- a/moonbridge_http.lua Tue Feb 17 16:54:35 2015 +0100 +++ b/moonbridge_http.lua Fri Feb 27 00:53:40 2015 +0100 @@ -87,11 +87,33 @@ local new_params_list -- defined later do local params_list_mapping = setmetatable({}, {__mode="k"}) + local function nextnonempty(tbl, key) + while true do + key = next(tbl, key) + if key == nil then + return nil + end + local value = tbl[key] + if #value > 0 then + return key, value + end + end + end + local function nextvalue(tbl, key) + key = next(tbl, key) + if key == nil then + return nil + end + return key, tbl[key][1] + end local params_list_metatable = { __index = function(self, key) local tbl = {} self[key] = tbl return tbl + end, + __pairs = function(self) + return nextnonempty, self, nil end } local params_metatable = { @@ -99,6 +121,9 @@ local value = params_list_mapping[self][key][1] self[key] = value return value + end, + __pairs = function(self) + return nextvalue, params_list_mapping[self], nil end } -- returns a table to store key value-list pairs (i.e. multiple values), diff -r de20248b53c2 -r 2d7e3028d993 reference.txt --- a/reference.txt Tue Feb 17 16:54:35 2015 +0100 +++ b/reference.txt Fri Feb 27 00:53:40 2015 +0100 @@ -219,11 +219,17 @@ A table that maps field names to their corresponding GET value. If there are several GET values with the given field name, then the first value is used. +Note: May be implemented through metamethods, but does support iteration +through pairs(...). + ### request.get_params_list A table that maps field names to a sequence of their corresponding GET values. +Note: May be implemented through metamethods, but does support iteration +through pairs(...). + ### request.headers @@ -297,11 +303,17 @@ A table that maps field names to their corresponding POST value. If there are several POST values with the given field name, then the first value is used. +Note: May be implemented through metamethods, but does support iteration +through pairs(...). + ### request.post_params_list A table that maps field names to a sequence of their corresponding POST values. +Note: May be implemented through metamethods, but does support iteration +through pairs(...). + ### request.query