forked from Liquipedia/Lua-Modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_flag.lua
More file actions
113 lines (94 loc) · 2.74 KB
/
Copy pathfeature_flag.lua
File metadata and controls
113 lines (94 loc) · 2.74 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
112
113
---
-- @Liquipedia
-- wiki=commons
-- page=Module:FeatureFlag
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--
local FeatureFlagConfig = mw.loadData('Module:FeatureFlag/Config')
local Logic = require('Module:Logic')
local Table = require('Module:Table')
--[[
Module for reading and setting feature flags. Feature flags are boolean values
that control whether certain code paths pertaining to new features are enabled.
To create a new feature flag, add an entry to Module:FeatureFlag/Config.
Usage:
Feature flags can be read and written to. If the value has not been set, then
the configured default value is used.
FeatureFlag.get('chiseled_face')
FeatureFlag.set('thick_mustache', true)
Feature flags can be applied temporarily inside a scope.
FeatureFlag.with({rugged_grin = true}, function()
-- ...
end)
Feature flags are also available in wikicode, with the exception of the
configured default value which is only available in lua.
{{#var:feature_chiseled_face}}
{{#vardefine:feature_thick_mustache|1}}
]]
local FeatureFlag = {}
local cachedFlags = {}
--[[
Retrieves the boolean value of a feature flag. If the flag has not been
previously set, this returns the configured default value of the flag.
]]
function FeatureFlag.get(flag)
if cachedFlags[flag] == nil then
cachedFlags[flag] = FeatureFlag._get(flag)
end
return cachedFlags[flag]
end
function FeatureFlag._get(flag)
local config = FeatureFlag.getConfig(flag)
return Logic.nilOr(
Logic.readBoolOrNil(mw.ext.VariablesLua.var('feature_' .. flag)),
config.defaultValue,
false
)
end
--[[
Sets the value of a feature flag. If value is nil, then this resets the value
to the configured default.
]]
function FeatureFlag.set(flag, value)
FeatureFlag.getConfig(flag)
if value ~= nil then
mw.ext.VariablesLua.vardefine('feature_' .. flag, value and '1' or '0')
else
mw.ext.VariablesLua.vardefine('feature_' .. flag, '')
end
cachedFlags[flag] = nil
end
--[[
Runs a function inside a scope where the specified flags are set.
]]
function FeatureFlag.with(flags, f)
if Table.isEmpty(flags) then
return f()
end
-- Remember previous flags
local oldFlags = Table.map(flags, function(flag, value)
return flag, mw.ext.VariablesLua.var('feature_' .. flag)
end)
-- Set new flags
for flag, value in pairs(flags) do
FeatureFlag.set(flag, value)
end
return Logic.try(f)
:finally(function()
-- Restore previous flags
for flag, oldValue in pairs(oldFlags) do
mw.ext.VariablesLua.vardefine('feature_' .. flag, oldValue)
cachedFlags[flag] = nil
end
end)
:get()
end
function FeatureFlag.getConfig(flag)
local config = FeatureFlagConfig[flag]
if not config then
error('Unrecognized feature flag \'' .. flag .. '\'', 2)
end
return config
end
return FeatureFlag