2025-01-21 06:29:06 +08:00
|
|
|
#meta
|
|
|
|
|
|
|
|
Config library for defining and getting config values
|
|
|
|
|
|
|
|
```space-lua
|
2025-01-23 03:26:37 +08:00
|
|
|
-- priority: 10
|
2025-01-21 06:29:06 +08:00
|
|
|
config = {}
|
|
|
|
|
2025-02-06 17:04:45 +08:00
|
|
|
local configValues = {}
|
|
|
|
local configSchema = {}
|
2025-01-21 06:29:06 +08:00
|
|
|
|
|
|
|
function config.define(key, schema)
|
2025-02-06 17:04:45 +08:00
|
|
|
configSchema[key] = schema or true
|
2025-01-21 06:29:06 +08:00
|
|
|
end
|
|
|
|
|
2025-02-06 15:37:09 +08:00
|
|
|
function config.set(keyOrTable, value)
|
|
|
|
if type(keyOrTable) == "table" then
|
|
|
|
for key, value in pairs(keyOrTable) do
|
|
|
|
config.set(key, value)
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local key = keyOrTable
|
2025-02-06 17:04:45 +08:00
|
|
|
local schema = configSchema[key]
|
2025-01-21 06:29:06 +08:00
|
|
|
if schema == nil then
|
|
|
|
error("Config key not defined: " .. key)
|
|
|
|
end
|
|
|
|
if schema != true then
|
2025-02-06 17:04:45 +08:00
|
|
|
local result = jsonschema.validateObject(schema, value)
|
2025-01-21 06:29:06 +08:00
|
|
|
if result != nil then
|
|
|
|
error("Validation error (" .. key .. "): " .. result)
|
|
|
|
end
|
|
|
|
end
|
2025-02-06 17:04:45 +08:00
|
|
|
configValues[key] = value
|
2025-01-21 06:29:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function config.get(key)
|
2025-02-06 17:04:45 +08:00
|
|
|
return configValues[key]
|
2025-01-21 06:29:06 +08:00
|
|
|
end
|