|
| 1 | +local json = require 'json' |
| 2 | +local template = require 'config.template' |
| 3 | + |
| 4 | +local function getType(temp) |
| 5 | + if temp.name == 'Boolean' then |
| 6 | + return 'boolean' |
| 7 | + end |
| 8 | + if temp.name == 'String' then |
| 9 | + return 'string' |
| 10 | + end |
| 11 | + if temp.name == 'Integer' then |
| 12 | + return 'integer' |
| 13 | + end |
| 14 | + if temp.name == 'Nil' then |
| 15 | + return 'null' |
| 16 | + end |
| 17 | + if temp.name == 'Array' then |
| 18 | + return 'array' |
| 19 | + end |
| 20 | + if temp.name == 'Hash' then |
| 21 | + return 'object' |
| 22 | + end |
| 23 | + if temp.name == 'Or' then |
| 24 | + return { getType(temp.subs[1]), getType(temp.subs[2]) } |
| 25 | + end |
| 26 | + error('Unknown type: ' .. temp.name) |
| 27 | +end |
| 28 | + |
| 29 | +local function getDefault(temp) |
| 30 | + local default = temp.default |
| 31 | + if default == nil and temp.hasDefault then |
| 32 | + default = json.null |
| 33 | + end |
| 34 | + return default |
| 35 | +end |
| 36 | + |
| 37 | +local function getEnum(temp) |
| 38 | + return temp.enums |
| 39 | +end |
| 40 | + |
| 41 | +local function getEnumDesc(name, temp) |
| 42 | + if not temp.enums then |
| 43 | + return nil |
| 44 | + end |
| 45 | + local descs = {} |
| 46 | + |
| 47 | + for _, enum in ipairs(temp.enums) do |
| 48 | + descs[#descs+1] = name:gsub('^Lua', '%%config') .. '.' .. enum .. '%' |
| 49 | + end |
| 50 | + |
| 51 | + return descs |
| 52 | +end |
| 53 | + |
| 54 | +local function insertArray(conf, temp) |
| 55 | + conf.items = { |
| 56 | + type = getType(temp.sub), |
| 57 | + } |
| 58 | +end |
| 59 | + |
| 60 | +local function insertHash(conf, temp) |
| 61 | + conf.additionalProperties = false |
| 62 | + if not temp.subkey.enums then |
| 63 | + if temp.subvalue.enums then |
| 64 | + conf.patternProperties = { |
| 65 | + ['.*'] = { |
| 66 | + type = getType( temp.subvalue), |
| 67 | + default = getDefault( temp.subvalue), |
| 68 | + enum = getEnum( temp.subvalue), |
| 69 | + } |
| 70 | + } |
| 71 | + end |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +local config = {} |
| 76 | + |
| 77 | +for name, temp in pairs(template) do |
| 78 | + config[name] = { |
| 79 | + scope = 'resource', |
| 80 | + type = getType(temp), |
| 81 | + default = getDefault(temp), |
| 82 | + enum = getEnum(temp), |
| 83 | + |
| 84 | + markdownDescription = name:gsub('^Lua', '%%config') .. '%', |
| 85 | + markdownEnumDescriptions = getEnumDesc(name, temp), |
| 86 | + } |
| 87 | + |
| 88 | + if temp.name == 'Array' then |
| 89 | + insertArray(config[name], temp) |
| 90 | + end |
| 91 | + |
| 92 | + if temp.name == 'Hash' then |
| 93 | + insertHash(config[name], temp) |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +config['Lua.telemetry.enable'].tags = { 'telemetry' } |
| 98 | + |
| 99 | +return config |
0 commit comments