moonbridge

changeset 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 de20248b53c2
children b841dc424baf
files moonbridge_http.lua reference.txt
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),
     2.1 --- a/reference.txt	Tue Feb 17 16:54:35 2015 +0100
     2.2 +++ b/reference.txt	Fri Feb 27 00:53:40 2015 +0100
     2.3 @@ -219,11 +219,17 @@
     2.4  A table that maps field names to their corresponding GET value. If there are
     2.5  several GET values with the given field name, then the first value is used.
     2.6  
     2.7 +Note: May be implemented through metamethods, but does support iteration
     2.8 +through pairs(...).
     2.9 +
    2.10  
    2.11  ### request.get_params_list
    2.12  
    2.13  A table that maps field names to a sequence of their corresponding GET values.
    2.14  
    2.15 +Note: May be implemented through metamethods, but does support iteration
    2.16 +through pairs(...).
    2.17 +
    2.18  
    2.19  ### request.headers
    2.20  
    2.21 @@ -297,11 +303,17 @@
    2.22  A table that maps field names to their corresponding POST value. If there are
    2.23  several POST values with the given field name, then the first value is used.
    2.24  
    2.25 +Note: May be implemented through metamethods, but does support iteration
    2.26 +through pairs(...).
    2.27 +
    2.28  
    2.29  ### request.post_params_list
    2.30  
    2.31  A table that maps field names to a sequence of their corresponding POST values.
    2.32  
    2.33 +Note: May be implemented through metamethods, but does support iteration
    2.34 +through pairs(...).
    2.35 +
    2.36  
    2.37  ### request.query
    2.38  

Impressum / About Us