Comparison

core/moduleapi.lua @ 7975:c64ddee9d671

core.moduleapi: Factor out code for getting a scalar config option
author Kim Alvefur <zash@zash.se>
date Wed, 15 Mar 2017 15:07:16 +0100
parent 7947:24170d74b00b
child 7982:e30b0cbed472
comparison
equal deleted inserted replaced
7973:703f7f45feb4 7975:c64ddee9d671
211 value = default_value; 211 value = default_value;
212 end 212 end
213 return value; 213 return value;
214 end 214 end
215 215
216 function api:get_option_string(name, default_value) 216 function api:get_option_scalar(name, default_value)
217 local value = self:get_option(name, default_value); 217 local value = self:get_option(name, default_value);
218 if type(value) == "table" then 218 if type(value) == "table" then
219 if #value > 1 then 219 if #value > 1 then
220 self:log("error", "Config option '%s' does not take a list, using just the first item", name); 220 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
221 end 221 end
222 value = value[1]; 222 value = value[1];
223 end 223 end
224 return value;
225 end
226
227 function api:get_option_string(name, default_value)
228 local value = self:get_option_scalar(name, default_value);
224 if value == nil then 229 if value == nil then
225 return nil; 230 return nil;
226 end 231 end
227 return tostring(value); 232 return tostring(value);
228 end 233 end
229 234
230 function api:get_option_number(name, ...) 235 function api:get_option_number(name, ...)
231 local value = self:get_option(name, ...); 236 local value = self:get_option_scalar(name, ...);
232 if type(value) == "table" then
233 if #value > 1 then
234 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
235 end
236 value = value[1];
237 end
238 local ret = tonumber(value); 237 local ret = tonumber(value);
239 if value ~= nil and ret == nil then 238 if value ~= nil and ret == nil then
240 self:log("error", "Config option '%s' not understood, expecting a number", name); 239 self:log("error", "Config option '%s' not understood, expecting a number", name);
241 end 240 end
242 return ret; 241 return ret;
243 end 242 end
244 243
245 function api:get_option_boolean(name, ...) 244 function api:get_option_boolean(name, ...)
246 local value = self:get_option(name, ...); 245 local value = self:get_option_scalar(name, ...);
247 if type(value) == "table" then
248 if #value > 1 then
249 self:log("error", "Config option '%s' does not take a list, using just the first item", name);
250 end
251 value = value[1];
252 end
253 if value == nil then 246 if value == nil then
254 return nil; 247 return nil;
255 end 248 end
256 local ret = value == true or value == "true" or value == 1 or nil; 249 local ret = value == true or value == "true" or value == 1 or nil;
257 if ret == nil then 250 if ret == nil then