check_site_lib.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. function in_site(var)
  2. return var
  3. end
  4. function in_domain(var)
  5. return var
  6. end
  7. local function path_to_string(path)
  8. return table.concat(path, '/')
  9. end
  10. local function array_to_string(array)
  11. return '[' .. table.concat(array, ', ') .. ']'
  12. end
  13. local function var_error(path, val, msg)
  14. print(string.format('*** site.conf error: expected %s to %s, but it is %s', path_to_string(path), msg, tostring(val)))
  15. os.exit(1)
  16. end
  17. function extend(path, c)
  18. local p = {unpack(path)}
  19. for _, e in ipairs(c) do
  20. p[#p+1] = e
  21. end
  22. return p
  23. end
  24. local function loadpath(path, base, c, ...)
  25. if not c or base == nil then
  26. return base
  27. end
  28. if type(base) ~= 'table' then
  29. var_error(path, base, 'be a table')
  30. end
  31. return loadpath(extend(path, {c}), base[c], ...)
  32. end
  33. local function loadvar(path)
  34. return loadpath({}, site, unpack(path))
  35. end
  36. local function check_type(t)
  37. return function(val)
  38. return type(val) == t
  39. end
  40. end
  41. local function check_one_of(array)
  42. return function(val)
  43. for _, v in ipairs(array) do
  44. if v == val then
  45. return true
  46. end
  47. end
  48. return false
  49. end
  50. end
  51. function need(path, check, required, msg)
  52. local val = loadvar(path)
  53. if required == false and val == nil then
  54. return nil
  55. end
  56. if not check(val) then
  57. var_error(path, val, msg)
  58. end
  59. return val
  60. end
  61. local function need_type(path, type, required, msg)
  62. return need(path, check_type(type), required, msg)
  63. end
  64. function need_alphanumeric_key(path)
  65. local val = path[#path]
  66. -- We don't use character classes like %w here to be independent of the locale
  67. if not val:match('^[0-9a-zA-Z_]+$') then
  68. var_error(path, val, 'have a key using only alphanumeric characters and underscores')
  69. end
  70. end
  71. function need_string(path, required)
  72. return need_type(path, 'string', required, 'be a string')
  73. end
  74. function need_string_match(path, pat, required)
  75. local val = need_string(path, required)
  76. if not val then
  77. return nil
  78. end
  79. if not val:match(pat) then
  80. var_error(path, val, "match pattern '" .. pat .. "'")
  81. end
  82. return val
  83. end
  84. function need_number(path, required)
  85. return need_type(path, 'number', required, 'be a number')
  86. end
  87. function need_boolean(path, required)
  88. return need_type(path, 'boolean', required, 'be a boolean')
  89. end
  90. function need_array(path, subcheck, required)
  91. local val = need_type(path, 'table', required, 'be an array')
  92. if not val then
  93. return nil
  94. end
  95. if subcheck then
  96. for i = 1, #val do
  97. subcheck(extend(path, {i}))
  98. end
  99. end
  100. return val
  101. end
  102. function need_table(path, subcheck, required)
  103. local val = need_type(path, 'table', required, 'be a table')
  104. if not val then
  105. return nil
  106. end
  107. if subcheck then
  108. for k, _ in pairs(val) do
  109. subcheck(extend(path, {k}))
  110. end
  111. end
  112. return val
  113. end
  114. function need_value(path, value, required)
  115. return need(path, function(v)
  116. return v == value
  117. end, required, 'be ' .. tostring(value))
  118. end
  119. function need_one_of(path, array, required)
  120. return need(path, check_one_of(array), required, 'be one of the given array ' .. array_to_string(array))
  121. end
  122. function need_string_array(path, required)
  123. return need_array(path, need_string, required)
  124. end
  125. function need_string_array_match(path, pat, required)
  126. return need_array(path, function(e) need_string_match(e, pat) end, required)
  127. end
  128. function need_array_of(path, array, required)
  129. return need_array(path, function(e) need_one_of(e, array) end, required)
  130. end