Skip to content

Commit 3c4d150

Browse files
authored
feat: Add function copy (#108)
Signed-off-by: Edmond <edomondja@gmail.com>
1 parent 7a99784 commit 3c4d150

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/model/Model.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,21 @@ function Model:sortPoliciesByPriority()
235235
end
236236
end
237237

238+
local function deepcopy(object)
239+
if type(object) ~= "table" then
240+
return object
241+
end
242+
local newobject = {}
243+
244+
for key,value in pairs(object) do
245+
newobject[key] = deepcopy(value)
246+
end
247+
setmetatable(newobject,getmetatable(object))
248+
return newobject
249+
end
250+
251+
function Model:copy()
252+
return deepcopy(self.model)
253+
end
254+
238255
return Model

tests/model/model_spec.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,15 @@ describe("model tests", function()
235235
m:printPolicy()
236236
end)
237237
end)
238+
239+
it("test copy", function ()
240+
local m = Model:new()
241+
m:loadModel(basic_path)
242+
243+
local rule = {'admin', 'domain1', 'data1', 'read'}
244+
m:addPolicy("p", "p", rule)
245+
246+
assert.is.same(m.model,m:copy())
247+
end)
248+
238249
end)

0 commit comments

Comments
 (0)