WpSettings safe parse APIs
This MR adds new wp_settings_parse_{boolean|int|float|string}_safe()
APIs in WpSettings
. The reason for this is to avoid Lua errors when doing the following:
local duck_level = Settings.get ("default-policy-duck.level"):parse() or 0.3
In the above example, if the setting "default-policy-duck.level"
does not exist, the Settings.get()
returns a nil
, which will cause the :parse()
method to fail, resulting in a Lua error, and the 0.3
value will never be used. Apart from that, if the setting is defined, but contains a boolean
for example, instead of a float
value, the script would parse it to boolean without letting the user now the setting is the wrong type.
With the new API, Lua scripts can just do:
local duck_level = Settings.parse_float_safe ("default-policy-duck.level", 0.3)
Which will safely parse the setting. If the setting does not exist, it will use the fallback 0.3
value. If the setting exists but has a value which is not a float
, then it will ignore it showing a warning in the log, and use the fallback 0.3
value.