Commit c2211587387ba076755b75a87d7088f598f9d294

Authored by totoro
1 parent 5d201cc4

create helper to get or extract web argument value in some format like String, Bool, Int, etc.

Showing 1 changed file with 196 additions and 0 deletions   Show diff stats
web/CXM_web_arg_utils.anubis 0 → 100644
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: Totoro
  4 + * Date: 07/02/2014
  5 + * Time: 21:43
  6 + *
  7 + */
  8 +read tools/basis.anubis
  9 +read calexium_lib/web/CXM_common.anubis
  10 +
  11 +public define Maybe(String)
  12 + get_String
  13 + (
  14 + List(Web_arg) lwa,
  15 + String arg_name
  16 + ) =
  17 + if web_arg_value(lwa, arg_name) is
  18 + {
  19 + not_found then failure,
  20 + found(value) then success(value)
  21 + }.
  22 +
  23 +public define String
  24 + get_String
  25 + (
  26 + List(Web_arg) lwa,
  27 + String arg_name,
  28 + String default_value
  29 + ) =
  30 + if get_String(lwa, arg_name) is
  31 + {
  32 + failure then default_value
  33 + success(value) then value
  34 + }.
  35 +
  36 +public define Bool
  37 + get_Bool
  38 + (
  39 + List(Web_arg) lwa,
  40 + String arg_name
  41 + ) =
  42 + if web_arg_value(lwa, arg_name) is
  43 + {
  44 + not_found then false,
  45 + found(_) then true
  46 + }.
  47 +
  48 +public define Maybe(Int)
  49 + get_Int
  50 + (
  51 + List(Web_arg) lwa,
  52 + String name
  53 + ) =
  54 + if web_arg_value(lwa, name) is
  55 + {
  56 + not_found then /*logDebug(debug_log, "Can't find argument '" + name + "'");*/ failure
  57 + found(str) then
  58 + if str = "" then
  59 + failure
  60 + else
  61 + decimal_scan(str)
  62 + }.
  63 +
  64 +public define Int
  65 + get_Int
  66 + (
  67 + List(Web_arg) lwa,
  68 + String name,
  69 + Int default_value
  70 + ) =
  71 + if get_Int(lwa, name) is
  72 + {
  73 + failure then default_value,
  74 + success(value) then value
  75 + }.
  76 +
  77 +public define Maybe(Int)
  78 + get_Int
  79 + (
  80 + List(Web_arg) lwa,
  81 + String name,
  82 + Int min,
  83 + Int max,
  84 + ) =
  85 + if get_Int(lwa, name) is
  86 + {
  87 + failure then /*logDebug(debug_log, "Can't convert argument '" + name + "' to integer.");*/ failure
  88 + success(value) then
  89 + if (value < min) | (value > max) then
  90 + /*logDebug(debug_log, "The entered value (" + val + ")is out of boundaries [" + min + "," + max + "].");*/ failure
  91 + else
  92 + success(value)
  93 + }.
  94 +
  95 +public define Int
  96 + get_Int
  97 + (
  98 + List(Web_arg) lwa,
  99 + String name,
  100 + Int min,
  101 + Int max,
  102 + Int default_value
  103 + ) =
  104 + if get_Int(lwa, name, min, max) is
  105 + {
  106 + failure then default_value,
  107 + success(value) then value
  108 + }.
  109 +
  110 +public define Maybe(Word32)
  111 + get_Word32
  112 + (
  113 + List(Web_arg) lwa,
  114 + String name
  115 + ) =
  116 + if get_Int(lwa, name) is
  117 + {
  118 + failure then failure,
  119 + success(int_val) then success(truncate_to_Word32(int_val))
  120 + }.
  121 +
  122 +public define Word32
  123 + get_Word32
  124 + (
  125 + List(Web_arg) lwa,
  126 + String name,
  127 + Word32 default_value
  128 + ) =
  129 + if get_Word32(lwa, name) is
  130 + {
  131 + failure then /*logDebug(debug_log, "Can't convert argument '" + name + "' to integer.");*/ default_value,
  132 + success(value) then value
  133 + }.
  134 +
  135 +public define Maybe(Word32)
  136 + get_Word32
  137 + (
  138 + List(Web_arg) lwa,
  139 + String name,
  140 + Word32 min,
  141 + Word32 max,
  142 + ) =
  143 + if get_Word32(lwa, name) is
  144 + {
  145 + failure then /*logDebug(debug_log, "Can't convert argument '" + name + "' to integer.");*/ failure
  146 + success(value) then
  147 + if value +< min | value >+ max then
  148 + /*logDebug(debug_log, "The entered value (" + val + ")is out of boundaries [" + min + "," + max + "].");*/ failure
  149 + else
  150 + success(value)
  151 + }.
  152 +
  153 +public define Word32
  154 + get_Word32
  155 + (
  156 + List(Web_arg) lwa,
  157 + String name,
  158 + Word32 min,
  159 + Word32 max,
  160 + Word32 default_value
  161 + ) =
  162 + if get_Word32(lwa, name, min, max) is
  163 + {
  164 + failure then default_value,
  165 + success(value) then value
  166 + }.
  167 +
  168 +public define Maybe(Word8)
  169 + get_Word8
  170 + (
  171 + List(Web_arg) lwa,
  172 + String name
  173 + ) =
  174 + if get_Int(lwa, name) is
  175 + {
  176 + failure then failure,
  177 + success(int_val) then success(truncate_to_Word8(int_val))
  178 + }.
  179 +
  180 +public define Maybe(Word8)
  181 + get_Word8
  182 + (
  183 + List(Web_arg) lwa,
  184 + String name,
  185 + Word8 min,
  186 + Word8 max,
  187 + ) =
  188 + if get_Word8(lwa, name) is
  189 + {
  190 + failure then /*logDebug(debug_log, "Can't convert argument '" + name + "' to integer.");*/ failure
  191 + success(value) then
  192 + if value +< min | value >+ max then
  193 + /*logDebug(debug_log, "The entered value (" + val + ")is out of boundaries [" + min + "," + max + "].");*/ failure
  194 + else
  195 + success(value)
  196 + }.