Skip to content

Commit

Permalink
Add annos to PageVariableNamespace (#3468)
Browse files Browse the repository at this point in the history
* Add annos to PageVariableNamespace

* use overload

* typo

* Update standard/page_variable_namespace.lua

Co-authored-by: Rikard Blixt <[email protected]>

---------

Co-authored-by: Rikard Blixt <[email protected]>
  • Loading branch information
hjpalpha and Rathoz authored Nov 2, 2023
1 parent 6e5e777 commit 47134ef
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions standard/page_variable_namespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,48 @@ local Namespace = Class.new(function(self, prefix)
self.prefix = prefix
end)

---@param key string|number
---@return string?
function Namespace:get(key)
return StringUtils.nilIfEmpty(mw.ext.VariablesLua.var(self.prefix .. key))
end

---@param key string|number
---@param value string|nil
function Namespace:set(key, value)
mw.ext.VariablesLua.vardefine(self.prefix .. key, StringUtils.nilIfEmpty(value))
end

---@param key string|number
function Namespace:delete(key)
self:set(key, nil)
end

---@class PageVariableNamespaceCachedTable
---@field results table
---@field table table
local CachedTable = Class.new(function(self, table)
self.results = {}
self.table = table
end)

---@param key string|number
---@return unknown
function CachedTable:get(key)
if not self.results[key] then
self.results[key] = self.table:get(key)
end
return self.results[key]
end

---@param key string|number
---@param value string|number|nil
function CachedTable:set(key, value)
self.results[key] = nil
self.table:set(key, value)
end

---@param key string|number
function CachedTable:delete(key)
self:set(key, nil)
end
Expand All @@ -53,16 +66,23 @@ end
---@operator call(string?):Namespace
local PageVariableNamespace = {}

---@param options {cached: boolean?, separator: string?, namespace: string?}?
---@return {cached: boolean, separator: string, namespace: string?}
---@overload fun(options: string): {cached: boolean, separator: string, namespace: string?}
---@overload fun(): {cached: false, separator: string}
function PageVariableNamespace.readOptions(options)
if not options then
options = {}
elseif type(options) == 'string' then
options = {namespace = options}
local parsedOptions = {}

options = options or {}
if type(options) == 'string' then
parsedOptions = {namespace = options}
end

options.cached = Logic.nilOr(options.cached, false)
options.separator = options.separator or '.'
return options
parsedOptions.cached = Logic.nilOr(options.cached, false)
parsedOptions.separator = options.separator or '.'
parsedOptions.namespace = parsedOptions.namespace or options.namespace

return parsedOptions
end

PageVariableNamespace.Namespace = Namespace
Expand Down

0 comments on commit 47134ef

Please sign in to comment.