summaryrefslogtreecommitdiff
path: root/tools/configuration.lua
blob: 8d7cac5d80b0b17744e789e93fed5b73c94709ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local json     = require 'json'
local template = require 'config.template'

local function getType(temp)
    if temp.name == 'Boolean' then
        return 'boolean'
    end
    if temp.name == 'String' then
        return 'string'
    end
    if temp.name == 'Integer' then
        return 'integer'
    end
    if temp.name == 'Nil' then
        return 'null'
    end
    if temp.name == 'Array' then
        return 'array'
    end
    if temp.name == 'Hash' then
        return 'object'
    end
    if temp.name == 'Or' then
        return { getType(temp.subs[1]), getType(temp.subs[2]) }
    end
    error('Unknown type: ' .. temp.name)
end

local function getDefault(temp)
    local default = temp.default
    if default == nil and temp.hasDefault then
        default = json.null
    end
    return default
end

local function getEnum(temp)
    return temp.enums
end

local function getEnumDesc(name, temp)
    if not temp.enums then
        return nil
    end
    local descs = {}

    for _, enum in ipairs(temp.enums) do
        descs[#descs+1] = name:gsub('^Lua', '%%config') .. '.' .. enum .. '%'
    end

    return descs
end

local function insertArray(conf, temp)
    conf.items = {
        type = getType(temp.sub),
    }
end

local function insertHash(conf, temp)
    conf.additionalProperties = false
    if not temp.subkey.enums then
        if  temp.subvalue.enums then
            conf.patternProperties = {
                ['.*'] = {
                    type    = getType( temp.subvalue),
                    default = getDefault( temp.subvalue),
                    enum    = getEnum( temp.subvalue),
                }
            }
        end
    end
end

local config = {}

for name, temp in pairs(template) do
    config[name] = {
        scope   = 'resource',
        type    = getType(temp),
        default = getDefault(temp),
        enum    = getEnum(temp),

        markdownDescription      = name:gsub('^Lua', '%%config') .. '%',
        markdownEnumDescriptions = getEnumDesc(name, temp),
    }

    if temp.name == 'Array' then
        insertArray(config[name], temp)
    end

    if temp.name == 'Hash' then
        insertHash(config[name], temp)
    end
end

config['Lua.telemetry.enable'].tags = { 'telemetry' }

return config