Module:ConfigHelpers
Appearance
Documentation for this module may be created at Module:ConfigHelpers/doc
-- Module:ConfigHelpers
-- Helper functions for configuration management used across the template system
--
-- This module provides utility functions for working with configuration data
-- that are used by ConfigRepository and potentially other modules.
local p = {}
-- Recursively merges two tables, with values from source overriding target; arrays are concatenated, other nested tables are recursively merged
function p.deepMerge(target, source)
if type(source) ~= 'table' or type(target) ~= 'table' then
return source
end
local result = {}
-- First, copy all values from target
for k, v in pairs(target) do
result[k] = v
end
-- Then, merge values from source
for k, v in pairs(source) do
-- If both are tables, merge them recursively
if type(v) == 'table' and type(result[k]) == 'table' then
-- Check if this is an array (sequential numeric indices)
local isArraySource = true
local isArrayTarget = true
for key, _ in pairs(v) do
if type(key) ~= 'number' then
isArraySource = false
break
end
end
for key, _ in pairs(result[k]) do
if type(key) ~= 'number' then
isArrayTarget = false
break
end
end
-- If both are arrays, concatenate them
if isArraySource and isArrayTarget then
local merged = {}
-- Copy all elements from target array
for i, val in ipairs(result[k]) do
merged[i] = val
end
-- Append all elements from source array
for i, val in ipairs(v) do
merged[#merged + 1] = val
end
result[k] = merged
else
-- Otherwise, recursively merge as associative tables
result[k] = p.deepMerge(result[k], v)
end
else
-- For non-table values or if only one is a table, use the source value
result[k] = v
end
end
return result
end
-- Determines if a field structure contains a specific key
function p.fieldHasKey(field, keyToFind)
if field.key and field.key == keyToFind then
return true
end
if field.keys and type(field.keys) == 'table' then
for _, key in ipairs(field.keys) do
if key == keyToFind then
return true
end
end
end
return false
end
return p