webmcp
view framework/env/param/get.lua @ 343:34bf5f7abe0d
Code cleanup in extos.c: use 1.0e9 instead of 0.000000001 to avoid floating-point inaccuracies, added "const" qualifiers to avoid two compiler warnings
| author | jbe | 
|---|---|
| date | Thu Mar 26 01:56:10 2015 +0100 (2015-03-26) | 
| parents | 32ec28229bb5 | 
| children | 
 line source
     1 --[[--
     2 value =       -- value of the parameter casted to the chosen param_type
     3 param.get(
     4   key,        -- name of the parameter
     5   param_type  -- desired type of the returned value
     6 )
     8 Either a GET or POST request parameter is returned by this function, or if param.exchange(...) was called before, one of the exchanged parameters is returned. You can specify which type the returned value shall have. If an external request parameter was used and there is another GET or POST parameter with the same name but a "__format" suffix, the parser with the name of the specified format will be automatically used to parse and convert the input value.
    10 --]]--
    12 function param.get(key, param_type)
    13   local param_type = param_type or atom.string
    14   if param._exchanged then
    15     local value = param._exchanged.params[key]
    16     if value ~= nil and not atom.has_type(value, param_type) then
    17       error("Parameter has unexpected type.")
    18     end
    19     return value
    20   else
    21     local str         = request.get_param{ name = key }
    22     local format_info = request.get_param{ name = key .. "__format" }
    23     if not str then
    24       if not format_info then
    25         return nil
    26       end
    27       str = ""
    28     end
    29     return param._get_parser(format_info, param_type)(str)
    30   end
    31 end
