-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Environment Scope * rework(DB): naming like in prisma.io The reasoning behind this is to make it easier to find code that does updates, or just fetches a single item, or fetches the whole dataset. Additionally add global data which is currently only used for .replay(). .replay() should enable the user to run the last command in any buffer, not only in .http or .rest buffers. * rework(DB): naming like in prisma.io The reasoning behind this is to make it easier to find code that does updates, or just fetches a single item, or fetches the whole dataset. Additionally add global data which is currently only used for .replay(). .replay() should enable the user to run the last command in any buffer, not only in .http or .rest buffers. * move db_spec.lua --------- Co-authored-by: Jan Stocker <Jan.Stocker@cosmoconsult.com> Co-authored-by: Marco Kellershoff <marco@kellershoff.net>
- Loading branch information
1 parent
09d7b94
commit e01f764
Showing
12 changed files
with
169 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,78 @@ | ||
local CONFIG = require("kulala.config") | ||
|
||
local M = {} | ||
|
||
M.data = { | ||
selected_env = nil, -- string - name of selected env | ||
http_client_env = nil, -- table of envs from http-client.env.json | ||
http_client_env_base = nil, -- table of base env values which should be applied to all requests | ||
env = {}, -- table of envs from document sources | ||
} | ||
M.data = nil | ||
M.global_data = {} | ||
|
||
local function default_data() | ||
return { | ||
selected_env = nil, -- string - name of selected env | ||
http_client_env = nil, -- table of envs from http-client.env.json | ||
http_client_env_base = nil, -- table of base env values which should be applied to all requests | ||
env = {}, -- table of envs from document sources | ||
scope_nr = nil, -- number - buffer number of the current scope | ||
} | ||
end | ||
|
||
local function get_current_scope_nr() | ||
if CONFIG.get().environment_scope == "b" then | ||
return vim.fn.bufnr() | ||
elseif CONFIG.get().environment_scope == "g" then | ||
return 0 | ||
end | ||
end | ||
|
||
local function load_data() | ||
if CONFIG.get().environment_scope == "b" then | ||
M.data = vim.b.kulala_data or default_data() | ||
elseif CONFIG.get().environment_scope == "g" then | ||
-- keep in lua only | ||
if not M.data then | ||
M.data = default_data() | ||
end | ||
end | ||
M.data.scope_nr = get_current_scope_nr() | ||
end | ||
|
||
local function save_data() | ||
if CONFIG.get().environment_scope == "b" then | ||
if vim.fn.bufexists(M.data.scope_nr) ~= -1 then | ||
vim.b[M.data.scope_nr].kulala_data = M.data | ||
end | ||
elseif CONFIG.get().environment_scope == "g" then | ||
-- keep in lua only | ||
end | ||
end | ||
|
||
M.global_find_many = function() | ||
return M.global_data | ||
end | ||
|
||
M.global_find_unique = function(key) | ||
return M.global_data[key] | ||
end | ||
|
||
M.global_update = function() | ||
return M.global_data | ||
end | ||
|
||
M.find_many = function() | ||
if not M.data or not M.data.scope_nr then | ||
load_data() | ||
elseif M.data.scope_nr ~= get_current_scope_nr() then | ||
save_data() | ||
load_data() | ||
end | ||
return M.data | ||
end | ||
|
||
M.update = function() | ||
return M.find_many() | ||
end | ||
|
||
M.find_unique = function(key) | ||
return M.find_many()[key] | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
local DB = require("kulala.db") | ||
|
||
describe("db scoped", function() | ||
it("should not leak into other buffers", function() | ||
vim.cmd("new") | ||
local buf1 = vim.api.nvim_get_current_buf() | ||
DB.update().key1 = "value1" | ||
assert.equal(DB.find_unique("key1"), "value1") | ||
|
||
vim.cmd("new") | ||
local buf2 = vim.api.nvim_get_current_buf() | ||
DB.update().key2 = "value2" | ||
assert.equal(DB.find_unique("key1"), nil) | ||
assert.equal(DB.find_unique("key2"), "value2") | ||
|
||
vim.api.nvim_set_current_buf(buf1) | ||
assert.equal(DB.find_unique("key1"), "value1") | ||
assert.equal(DB.find_unique("key2"), nil) | ||
|
||
vim.api.nvim_set_current_buf(buf2) | ||
assert.equal(DB.find_unique("key1"), nil) | ||
assert.equal(DB.find_unique("key2"), "value2") | ||
end) | ||
end) |