diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-19 16:54:18 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-19 16:54:18 +0200 |
| commit | 5aee90eca42e15fd67068cb7973c1547f8d44eca (patch) | |
| tree | 9d1c9297dbaf943f826471e823cf8207d3dba866 | |
| parent | fc971e91a377c58f804d34e146833c7b9cba8d33 (diff) | |
| download | lanes-5aee90eca42e15fd67068cb7973c1547f8d44eca.tar.gz lanes-5aee90eca42e15fd67068cb7973c1547f8d44eca.tar.bz2 lanes-5aee90eca42e15fd67068cb7973c1547f8d44eca.zip | |
Config parameter validators can append extra information to the error message
| -rw-r--r-- | src/lanes.lua | 56 |
1 files changed, 45 insertions, 11 deletions
diff --git a/src/lanes.lua b/src/lanes.lua index 7c1f8df..3ed4e4e 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
| @@ -109,39 +109,72 @@ local default_params = | |||
| 109 | 109 | ||
| 110 | local boolean_param_checker = function(val_) | 110 | local boolean_param_checker = function(val_) |
| 111 | -- non-'boolean-false|nil' should be 'boolean-true' | 111 | -- non-'boolean-false|nil' should be 'boolean-true' |
| 112 | return (not val_) or (val_ == true) | 112 | if (not val_) or (val_ == true) then |
| 113 | return true | ||
| 114 | end | ||
| 115 | return nil, "not a boolean" | ||
| 113 | end | 116 | end |
| 114 | 117 | ||
| 115 | local param_checkers = | 118 | local param_checkers = |
| 116 | { | 119 | { |
| 117 | allocator = function(val_) | 120 | allocator = function(val_) |
| 118 | -- can be nil, "protected", or a function | 121 | -- can be nil, "protected", or a function |
| 119 | return (val_ == nil) or (type(val_) == "function" or val_ == "protected") | 122 | if val_ ~= nil and val_ ~= "protected" and type(val_) ~= "function" then |
| 123 | return nil, "unknown value" | ||
| 124 | end | ||
| 125 | return true | ||
| 120 | end, | 126 | end, |
| 121 | internal_allocator = function(val_) | 127 | internal_allocator = function(val_) |
| 122 | -- can be "libc" or "allocator" | 128 | -- can be "libc" or "allocator" |
| 123 | return val_ == "libc" or val_ == "allocator" | 129 | if type(val_) ~= "string" then |
| 130 | return nil, "not a string" | ||
| 131 | end | ||
| 132 | if val_ ~= "libc" and val_ ~= "allocator" then | ||
| 133 | return nil, "unknown value" | ||
| 134 | end | ||
| 135 | return true | ||
| 124 | end, | 136 | end, |
| 125 | keepers_gc_threshold = function(val_) | 137 | keepers_gc_threshold = function(val_) |
| 126 | -- keepers_gc_threshold should be a number | 138 | -- keepers_gc_threshold should be a number |
| 127 | return type(val_) == "number" | 139 | if type(val_) ~= "number" then |
| 140 | return nil, "not a number" | ||
| 141 | end | ||
| 142 | return true | ||
| 128 | end, | 143 | end, |
| 129 | nb_user_keepers = function(val_) | 144 | nb_user_keepers = function(val_) |
| 130 | -- nb_user_keepers should be a number in [0,100] (so that nobody tries to run OOM by specifying a huge amount) | 145 | -- nb_user_keepers should be a number in [0,100] (so that nobody tries to run OOM by specifying a huge amount) |
| 131 | return (type(val_) == "number") and (val_ >= 0) and (val_ <= 100) | 146 | if type(val_) ~= "number" then |
| 147 | return nil, "not a number" | ||
| 148 | end | ||
| 149 | if val_ < 0 or val_ > 100 then | ||
| 150 | return nil, "value out of range" | ||
| 151 | end | ||
| 152 | return true | ||
| 132 | end, | 153 | end, |
| 133 | on_state_create = function(val_) | 154 | on_state_create = function(val_) |
| 134 | -- on_state_create may be nil or a function | 155 | -- on_state_create may be nil or a function |
| 135 | return (val_ == nil) or (type(val_) == "function") | 156 | if val_ ~= nil and type(val_) ~= "function" then |
| 157 | return nil, "not a function" | ||
| 158 | end | ||
| 159 | return true | ||
| 136 | end, | 160 | end, |
| 137 | shutdown_mode = function(val_) | 161 | shutdown_mode = function(val_) |
| 138 | local valid_hooks = { soft = true, hard = true, call = true, ret = true, line = true, count = true } | 162 | local valid_hooks = { soft = true, hard = true, call = true, ret = true, line = true, count = true } |
| 139 | -- shutdown_mode should be a known hook mask | 163 | -- shutdown_mode should be a known hook mask |
| 140 | return valid_hooks[val_] | 164 | if not valid_hooks[val_] then |
| 165 | return nil, "unknown value" | ||
| 166 | end | ||
| 167 | return true | ||
| 141 | end, | 168 | end, |
| 142 | shutdown_timeout = function(val_) | 169 | shutdown_timeout = function(val_) |
| 143 | -- shutdown_timeout should be a number >= 0 | 170 | -- shutdown_timeout should be a number in [0,3600] |
| 144 | return type(val_) == "number" and val_ >= 0 | 171 | if type(val_) ~= "number" then |
| 172 | return nil, "not a number" | ||
| 173 | end | ||
| 174 | if val_ < 0 or val_ > 3600 then | ||
| 175 | return nil, "value out of range" | ||
| 176 | end | ||
| 177 | return true | ||
| 145 | end, | 178 | end, |
| 146 | strip_functions = boolean_param_checker, | 179 | strip_functions = boolean_param_checker, |
| 147 | track_lanes = boolean_param_checker, | 180 | track_lanes = boolean_param_checker, |
| @@ -175,8 +208,9 @@ local params_checker = function(settings_) | |||
| 175 | else | 208 | else |
| 176 | param = default_params[key] | 209 | param = default_params[key] |
| 177 | end | 210 | end |
| 178 | if not checker(param) then | 211 | local _result, _msg = checker(param) |
| 179 | error("Bad argument " .. key .. ": " .. tostring(param), 2) | 212 | if not _result then |
| 213 | error("Bad argument " .. key .. ": " .. tostring(param) .. (_msg and " (" .. _msg .. ")" or ""), 2) | ||
| 180 | end | 214 | end |
| 181 | settings[key] = param | 215 | settings[key] = param |
| 182 | end | 216 | end |
