-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationLoader.lua
More file actions
111 lines (95 loc) · 3.83 KB
/
ConfigurationLoader.lua
File metadata and controls
111 lines (95 loc) · 3.83 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
--- ConfigurationLoader: Loads and applies configuration overrides from JSON file
--- Reads optional config.json file and overwrites global variables with specified values
--- Follows separation of concerns by encapsulating all file I/O and parsing logic
---
--- @class ConfigurationLoader
--- @field configPath string Path to configuration file
--- @usage
--- local loader = ConfigurationLoader()
--- local config = loader:load()
--- if config then
--- loader:applyToGlobals(config)
--- end
---
--- @author Nicolae Cudlenco
ConfigurationLoader = class(function(o, configPath)
--- Constructor for ConfigurationLoader
--- @param configPath string Optional path to config file (defaults to "config.json")
o.configPath = configPath or "config.json"
end)
--- Load configuration from JSON file
--- Reads and parses the configuration file if it exists
--- Handles file I/O errors and JSON parsing errors gracefully
---
--- @return table|nil Parsed configuration table or nil if file doesn't exist or parsing fails
--- @usage local config = loader:load()
function ConfigurationLoader:load()
-- Check if file exists
if not fileExists(self.configPath) then
print("[ConfigurationLoader] Configuration file not found: " .. self.configPath)
return nil
end
-- Open file
local file = fileOpen(self.configPath)
if not file then
print("[ConfigurationLoader] ERROR: Failed to open configuration file: " .. self.configPath)
return nil
end
-- Read file content
local fileSize = fileGetSize(file)
if fileSize == 0 then
fileClose(file)
print("[ConfigurationLoader] Configuration file is empty: " .. self.configPath)
return nil
end
local jsonStr = fileRead(file, fileSize)
fileClose(file)
if not jsonStr then
print("[ConfigurationLoader] ERROR: Failed to read configuration file: " .. self.configPath)
return nil
end
-- Parse JSON
local config = fromJSON(jsonStr)
if not config then
print("[ConfigurationLoader] ERROR: Invalid JSON in configuration file: " .. self.configPath)
return nil
end
print("[ConfigurationLoader] Successfully loaded configuration from: " .. self.configPath)
return config
end
--- Apply configuration values to global variables
--- Iterates through configuration key-value pairs and overwrites matching globals
--- Validates types and logs changes when DEBUG is enabled
---
--- @param config table Configuration table with key-value pairs to apply
--- @usage loader:applyToGlobals(config)
function ConfigurationLoader:applyToGlobals(config)
if not config or type(config) ~= "table" then
print("[ConfigurationLoader] ERROR: Invalid configuration object")
return
end
local overrideCount = 0
for key, value in pairs(config) do
-- Check if global variable exists
if _G[key] ~= nil then
local oldValue = _G[key]
local oldType = type(oldValue)
local newType = type(value)
-- Type validation: warn if types don't match but still allow override
if oldType ~= newType then
print(string.format("[ConfigurationLoader] WARNING: Type mismatch for '%s' (was %s, now %s)",
key, oldType, newType))
end
-- Apply override
_G[key] = value
overrideCount = overrideCount + 1
print(string.format("[ConfigurationLoader] Overridden %s: %s -> %s",
key, tostring(oldValue), tostring(value)))
else
print(string.format("[ConfigurationLoader] WARNING: Unknown configuration key '%s' (global variable doesn't exist)", key))
end
end
if overrideCount > 0 then
print(string.format("[ConfigurationLoader] Applied %d configuration override(s)", overrideCount))
end
end