webmcp

diff framework/env/param/get_list.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children 32ec28229bb5
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/param/get_list.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,37 @@
     1.4 +--[[--
     1.5 +values =         -- list of values casted to the chosen param_type
     1.6 +param.get_list(
     1.7 +  key,           -- name of the parameter without "[]" suffix
     1.8 +  param_type,    -- desired type of the returned values
     1.9 +)
    1.10 +
    1.11 +Same as param.get(...), but used for parameters which contain a list of values. For external GET/POST parameters the parameter name gets suffixed with "[]".
    1.12 +
    1.13 +--]]--
    1.14 +
    1.15 +function param.get_list(key, param_type)
    1.16 +  local param_type = param_type or atom.string
    1.17 +  if param._exchanged then
    1.18 +    local values = param._exchanged.params[key] or {}
    1.19 +    if type(values) ~= "table" then
    1.20 +      error("Parameter has unexpected type.")
    1.21 +    end
    1.22 +    for idx, value in ipairs(values) do
    1.23 +      if not atom.has_type(value, param_type) then
    1.24 +        error("Element of parameter list has unexpected type.")
    1.25 +      end
    1.26 +    end
    1.27 +    return values
    1.28 +  else
    1.29 +    local format_info = cgi.params[key .. "__format"]
    1.30 +    local parser = param._get_parser(format_info, param_type)
    1.31 +    local raw_values = cgi.params[key .. "[]"]
    1.32 +    local values = {}
    1.33 +    if raw_values then
    1.34 +      for idx, value in ipairs(raw_values) do
    1.35 +        values[idx] = parser(raw_values[idx])
    1.36 +      end
    1.37 +    end
    1.38 +    return values
    1.39 +  end
    1.40 +end

Impressum / About Us