[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua as a configuration language -- validation
- From: Chris Smith <space.dandy@...>
- Date: Thu, 22 Aug 2019 14:22:48 +0100
> On 22 Aug 2019, at 13:58, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
>
> Please show us a sample configuration file and what validation you have in mind.
Ok, well let’s take a trivial and contrived example of a UI configuration. The configuration might look something like this:
UI = {
mainwindow = {
size = {
width = 100,
height = 200
}
}
}
Now, my code wants to use this information very simply:
local width = config.UI.mainwindow.size.width
But before I can do that, as a minimum I need to verify the structure and format of the config before I can safely use it. Very naively, I might do something like this:
local width
if type(UI) == ’table’ then
if type(UI.mainwindow) == ’table’ then
if type(UI.mainwindow.size) == ’table’ then
— now can access safely
width = UI.mainwindow.size.width
end
end
end
If type(width) == ’number’ then
— do something
end
That’s an awful lot of cruft just for one element, and I haven’t even dealt with range checking, default values and passing back warnings to the user. There are a few ways I can think to make it simpler, like wrapping a simple access in a pcall to see if it crashes or not, but this must be quite a common issue and I wondered if there are already some standard libraries or patterns to deal with it before I start re-inventing wheels.
Regards,
Chris
—
Chris Smith <space.dandy@icloud.com>