refactor(cache): nest credentials under platform namespace (#293)

## Problem

Credentials lived in a top-level `_credentials` namespace, requiring
special
preservation logic in `clear_all()` and a separate key hierarchy from
the
platform data they belong to.

## Solution

Move credentials from `_credentials.<platform>` to
`<platform>._credentials`.
Migrate v1 caches on load, skip underscore-prefixed keys when
enumerating
contest IDs and summaries, and simplify `clear_all()` now that no
special
preservation is needed.

Stacked on #292.
This commit is contained in:
Barrett Ruth 2026-03-04 13:37:22 -05:00 committed by GitHub
parent 49e0ae3885
commit 1bc0aa41b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -40,7 +40,7 @@
local M = {} local M = {}
local CACHE_VERSION = 1 local CACHE_VERSION = 2
local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json' local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json'
local cache_data = {} local cache_data = {}
@ -67,13 +67,27 @@ function M.load()
end end
local ok, decoded = pcall(vim.json.decode, table.concat(content, '\n')) local ok, decoded = pcall(vim.json.decode, table.concat(content, '\n'))
if ok then if not ok then
if decoded._version ~= CACHE_VERSION then cache_data = {}
cache_data = {} M.save()
M.save() loaded = true
else return
cache_data = decoded end
if decoded._version == 1 then
local old_creds = decoded._credentials
decoded._credentials = nil
if old_creds then
for platform, creds in pairs(old_creds) do
decoded[platform] = decoded[platform] or {}
decoded[platform]._credentials = creds
end
end end
decoded._version = CACHE_VERSION
cache_data = decoded
M.save()
elseif decoded._version == CACHE_VERSION then
cache_data = decoded
else else
cache_data = {} cache_data = {}
M.save() M.save()
@ -122,7 +136,9 @@ function M.get_cached_contest_ids(platform)
local contest_ids = {} local contest_ids = {}
for contest_id, _ in pairs(cache_data[platform]) do for contest_id, _ in pairs(cache_data[platform]) do
table.insert(contest_ids, contest_id) if contest_id:sub(1, 1) ~= '_' then
table.insert(contest_ids, contest_id)
end
end end
table.sort(contest_ids) table.sort(contest_ids)
return contest_ids return contest_ids
@ -336,11 +352,13 @@ end
function M.get_contest_summaries(platform) function M.get_contest_summaries(platform)
local contest_list = {} local contest_list = {}
for contest_id, contest_data in pairs(cache_data[platform] or {}) do for contest_id, contest_data in pairs(cache_data[platform] or {}) do
table.insert(contest_list, { if contest_id:sub(1, 1) ~= '_' then
id = contest_id, table.insert(contest_list, {
name = contest_data.name, id = contest_id,
display_name = contest_data.display_name, name = contest_data.name,
}) display_name = contest_data.display_name,
})
end
end end
return contest_list return contest_list
end end
@ -374,38 +392,30 @@ end
---@param platform string ---@param platform string
---@return table? ---@return table?
function M.get_credentials(platform) function M.get_credentials(platform)
if not cache_data._credentials then if not cache_data[platform] then
return nil return nil
end end
return cache_data._credentials[platform] return cache_data[platform]._credentials
end end
---@param platform string ---@param platform string
---@param creds table ---@param creds table
function M.set_credentials(platform, creds) function M.set_credentials(platform, creds)
cache_data._credentials = cache_data._credentials or {} cache_data[platform] = cache_data[platform] or {}
cache_data._credentials[platform] = creds cache_data[platform]._credentials = creds
M.save() M.save()
end end
---@param platform string? ---@param platform string
function M.clear_credentials(platform) function M.clear_credentials(platform)
if platform then if cache_data[platform] then
if cache_data._credentials then cache_data[platform]._credentials = nil
cache_data._credentials[platform] = nil
end
else
cache_data._credentials = nil
end end
M.save() M.save()
end end
function M.clear_all() function M.clear_all()
local creds = cache_data._credentials
cache_data = {} cache_data = {}
if creds then
cache_data._credentials = creds
end
M.save() M.save()
end end