>OmegaK2 |
|
(20 intermediate revisions by 4 users not shown) |
Line 1: |
Line 1: |
| local p = {}
| | -- This is not an actual module. Do not add code. |
| | |
| p.cases = {
| |
| -- Basic test list
| |
| {
| |
| parser = {
| |
| explicit = {
| |
| _type = 'list',
| |
| },
| |
| },
| |
| | |
| input = {
| |
| explicit1 = 'Test1',
| |
| explicit2 = 'Test2',
| |
| },
| |
|
| |
| expected = {
| |
| explicit = {'Test1', 'Test2'},
| |
| },
| |
| },
| |
| -- Basic test table
| |
| {
| |
| parser = {
| |
| stat = {
| |
| _type = 'table',
| |
| id = {},
| |
| value = {},
| |
| },
| |
| },
| |
| input = {
| |
| stat1_id = 'test1',
| |
| stat1_value = '1',
| |
| stat2_id = 'test2',
| |
| stat2_value = '2',
| |
| },
| |
| expected = {
| |
| stat = {
| |
| [1] = {
| |
| id = 'test1',
| |
| value = '1',
| |
| },
| |
| [2] = {
| |
| id = 'test2',
| |
| value = '2',
| |
| },
| |
| },
| |
| },
| |
| },
| |
| -- Test nested table
| |
| {
| |
| parser = {
| |
| stat_column = {
| |
| _type = 'table',
| |
| header = {
| |
| _func = nil,
| |
| _type = nil,
| |
| },
| |
| format = {
| |
| _func = nil,
| |
| _type = nil,
| |
| },
| |
| stat_format = {
| |
| _func = nil,
| |
| _type = nil,
| |
| },
| |
| stat = {
| |
| _type = 'list',
| |
| --id = {
| |
| -- _func = nil,
| |
| -- _type = nil,
| |
| --},
| |
| },
| |
| },
| |
| },
| |
| input = {
| |
| stat_column1_header = 'Local Attack<br>Chaos Damage',
| |
| stat_column1_format = '%s-%s',
| |
| stat_column1_stat_format = 'separate',
| |
| stat_column1_stat1_id = 'local_minimum_added_chaos_damage',
| |
| stat_column1_stat2_id = 'local_maximum_added_chaos_damage',
| |
| stat_column2_header = 'Global Attack<br>Chaos Damage',
| |
| stat_column2_format = '%s-%s',
| |
| stat_column2_stat_format = 'separate',
| |
| stat_column2_stat1_id = 'attack_minimum_added_chaos_damage',
| |
| stat_column2_stat2_id = 'attack_minimum_added_chaos_damage',
| |
| },
| |
| expected = {
| |
| stat_column = {
| |
| [1] = {
| |
| header = 'Local Attack<br>Chaos Damage',
| |
| format = '%s-%s',
| |
| stat_format = 'separate',
| |
| stat = {
| |
| [1] = {
| |
| id = 'local_minimum_added_chaos_damage',
| |
| },
| |
| [2] = {
| |
| id = 'local_maximum_added_chaos_damage',
| |
| },
| |
| },
| |
| },
| |
| [2] = {
| |
| header = 'Global Attack<br>Chaos Damage',
| |
| format = '%s-%s',
| |
| stat_format = 'separate',
| |
| stat = {
| |
| [1] = {
| |
| id = 'attack_minimum_added_chaos_damage',
| |
| },
| |
| [2] = {
| |
| id = 'attack_minimum_added_chaos_damage',
| |
| },
| |
| },
| |
| },
| |
| },
| |
| },
| |
| },
| |
| }
| |
| | |
| | |
| function p.validator(args, parser, prefix)
| |
| local rtr = {}
| |
| prefix = prefix or ''
| |
| for key, data in pairs(parser) do
| |
| local pkey = prefix .. key
| |
| if data._type == 'table' then
| |
| local i = 1
| |
| local stop = false
| |
| local tbl = {}
| |
| repeat
| |
| local result = p.validator(args, data, string.format('%s%s_', pkey, i))
| |
|
| |
| -- As long there was one entry, it is valid
| |
| local count = 0
| |
| for _ in pairs(result) do
| |
| count = count + 1
| |
| break
| |
| end
| |
|
| |
| -- Stop if we don't get any results anymore
| |
| if count == 0 then
| |
| stop = true
| |
| else
| |
| tbl[#tbl+1] = result
| |
| i = i + 1
| |
| end
| |
|
| |
| until stop == true
| |
|
| |
| rtr[key] = tbl
| |
| elseif data._type == 'list' then
| |
| local i = 1
| |
| local stop = false
| |
| local tbl = {}
| |
| repeat
| |
| local result = args[pkey .. i]
| |
| if data._func ~= nil then
| |
| result = data._func(result)
| |
| end
| |
|
| |
| if result == nil then
| |
| stop = true
| |
| else
| |
| tbl[#tbl+1] = result
| |
| i = i + 1
| |
| end
| |
| until stop == true
| |
|
| |
| rtr[key] = tbl
| |
| else
| |
| local result = args[pkey]
| |
| if data._func ~= nil then
| |
| result = data._func(result)
| |
| end
| |
| rtr[key] = result
| |
| end
| |
| end
| |
|
| |
| return rtr
| |
| end
| |
| | |
| return p
| |