Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstracted out Workplace's storage system for other plugins. #1738

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Sumenko reports duplicate function definitions, unsure why.
  • Loading branch information
adamharrison committed Sep 16, 2024
commit a49cc25d5f0f19e08e0f512199cc400e8dfb3744
17 changes: 10 additions & 7 deletions data/core/storage.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
local core = require "core"
local common = require "core.common"

local function module_key_to_path(module, key)
return USERDIR .. PATHSEP .. "storage" .. (module and (PATHSEP .. module .. (key and (PATHSEP .. key:gsub("[\\/]", "-")) or "")) or "")
end


----- Provides persistent storage between restarts of the application.
---Provides persistent storage between restarts of the application.
---@class storage
local storage = {}

---Loads data from a persistent storage file.

---Loads data from a persistent storage file.
---
---@param module string The module under which the data is stored.
---@param key string The key under which the data is stored.
---@return string|table|number data The stored data present for this module, at this key.
---@return string|table|number? data The stored data present for this module, at this key.
function storage.load(module, key)
local path = module_key_to_path(module, key)
if system.get_file_info(path) then
Expand All @@ -29,7 +31,7 @@ end


---Saves data to a persistent storage file.

---
---@param module string The module under which the data is stored.
---@param key string The key under which the data is stored.
---@param value table|string|number The value to store.
Expand All @@ -53,16 +55,16 @@ end


---Gets the list of keys saved under a module.

---
---@param module string The module under which the data is stored.
---@return table A table of keys under which data is stored for this module.
function storage.keys(module)
return system.list_dir(module_key_to_path(module))
return system.list_dir(module_key_to_path(module)) or {}
end


---Clears data for a particular module and optionally key.

---
---@param module string The module under which the data is stored.
---@param key? string The key under which the data is stored. If omitted, will clear the entire store for this module.
function storage.clear(module, key)
Expand All @@ -72,4 +74,5 @@ function storage.clear(module, key)
end
end


return storage