Skip to content

Commit 7ee85ed

Browse files
authored
feat: some adjustments to builtin functions (#162)
* test: added cases for builtin functions errors * style: conformed to casbin/casbin - moved wrappers under respective functions - moved keyMatch5 * fix: globMatch FunctionMap
1 parent 399e18b commit 7ee85ed

File tree

3 files changed

+90
-53
lines changed

3 files changed

+90
-53
lines changed

src/model/FunctionMap.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ local FunctionMap = {
2121
["keyGet2"] = BuiltInFunctions.keyGet2Func,
2222
["keyMatch3"] = BuiltInFunctions.keyMatch3Func,
2323
["keyMatch4"] = BuiltInFunctions.keyMatch4Func,
24+
["keyMatch5"] = BuiltInFunctions.keyMatch4Func,
2425
["regexMatch"] = BuiltInFunctions.regexMatchFunc,
2526
["IPMatch"] = BuiltInFunctions.IPMatchFunc,
26-
["globMatch"] = BuiltInFunctions.globMatch,
27-
["keyMatch5"] = BuiltInFunctions.keyMatch4Func,
27+
["globMatch"] = BuiltInFunctions.globMatchFunc,
2828
}
2929

3030
-- FunctionMap provides a set of built in functions

src/util/BuiltInFunctions.lua

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ function BuiltInFunctions.validateVariadicArgs(expectedLen, args)
2828
end
2929
end
3030

31-
-- Wrapper for keyMatch
32-
function BuiltInFunctions.keyMatchFunc(args)
33-
BuiltInFunctions.validateVariadicArgs(2, args)
34-
return BuiltInFunctions.keyMatch(args[1], args[2])
35-
end
36-
3731
-- KeyMatch determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
3832
-- For example, "/foo/bar" matches "/foo/*"
3933
function BuiltInFunctions.keyMatch(key1, key2)
@@ -49,10 +43,10 @@ function BuiltInFunctions.keyMatch(key1, key2)
4943
return (key1 == string.sub(key2, 1, i-1))
5044
end
5145

52-
-- Wrapper for keyGet
53-
function BuiltInFunctions.keyGetFunc(args)
46+
-- Wrapper for keyMatch
47+
function BuiltInFunctions.keyMatchFunc(args)
5448
BuiltInFunctions.validateVariadicArgs(2, args)
55-
return BuiltInFunctions.keyGet(args[1], args[2])
49+
return BuiltInFunctions.keyMatch(args[1], args[2])
5650
end
5751

5852
-- KeyGet returns the matched part
@@ -72,10 +66,10 @@ function BuiltInFunctions.keyGet(key1, key2)
7266
return ""
7367
end
7468

75-
-- Wrapper for keyMatch2
76-
function BuiltInFunctions.keyMatch2Func(args)
69+
-- Wrapper for keyGet
70+
function BuiltInFunctions.keyGetFunc(args)
7771
BuiltInFunctions.validateVariadicArgs(2, args)
78-
return BuiltInFunctions.keyMatch2(args[1], args[2])
72+
return BuiltInFunctions.keyGet(args[1], args[2])
7973
end
8074

8175
-- KeyMatch2 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
@@ -87,6 +81,12 @@ function BuiltInFunctions.keyMatch2(key1, key2)
8781
return BuiltInFunctions.regexMatch(key1, "^"..key.."$")
8882
end
8983

84+
-- Wrapper for keyMatch2
85+
function BuiltInFunctions.keyMatch2Func(args)
86+
BuiltInFunctions.validateVariadicArgs(2, args)
87+
return BuiltInFunctions.keyMatch2(args[1], args[2])
88+
end
89+
9090
-- KeyGet2 returns value matched pattern
9191
-- For example, "/resource1" matches "/:resource"
9292
-- if the pathVar == "resource", then "resource1" will be returned
@@ -117,12 +117,6 @@ function BuiltInFunctions.keyGet2Func(args)
117117
return BuiltInFunctions.keyGet2(args[1], args[2],args[3])
118118
end
119119

120-
-- Wrapper for keyMatch3
121-
function BuiltInFunctions.keyMatch3Func(args)
122-
BuiltInFunctions.validateVariadicArgs(2, args)
123-
return BuiltInFunctions.keyMatch3(args[1], args[2])
124-
end
125-
126120
-- KeyMatch3 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
127121
-- For example, "/foo/bar" matches "/foo/*", "/resource1" matches "/{resource}"
128122
function BuiltInFunctions.keyMatch3(key1, key2)
@@ -131,10 +125,10 @@ function BuiltInFunctions.keyMatch3(key1, key2)
131125
return BuiltInFunctions.regexMatch(key1, "^"..key.."$")
132126
end
133127

134-
-- Wrapper for keyMatch4
135-
function BuiltInFunctions.keyMatch4Func(args)
128+
-- Wrapper for keyMatch3
129+
function BuiltInFunctions.keyMatch3Func(args)
136130
BuiltInFunctions.validateVariadicArgs(2, args)
137-
return BuiltInFunctions.keyMatch4(args[1], args[2])
131+
return BuiltInFunctions.keyMatch3(args[1], args[2])
138132
end
139133

140134
-- KeyMatch4 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
@@ -170,10 +164,34 @@ function BuiltInFunctions.keyMatch4(key1, key2)
170164
return true
171165
end
172166

173-
-- Wrapper for regexMatch
174-
function BuiltInFunctions.regexMatchFunc(args)
167+
-- Wrapper for keyMatch4
168+
function BuiltInFunctions.keyMatch4Func(args)
175169
BuiltInFunctions.validateVariadicArgs(2, args)
176-
return BuiltInFunctions.regexMatch(args[1], args[2])
170+
return BuiltInFunctions.keyMatch4(args[1], args[2])
171+
end
172+
173+
-- KeyMatch5 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *
174+
-- For example,
175+
-- - "/foo/bar?status=1&type=2" matches "/foo/bar"
176+
-- - "/parent/child1" and "/parent/child1" matches "/parent/*"
177+
-- - "/parent/child1?status=1" matches "/parent/*"
178+
function BuiltInFunctions.keyMatch5(key1, key2)
179+
local i = string.find(key1, "?", 1, true)
180+
181+
if i then
182+
key1 = string.sub(key1, 1, i - 1)
183+
end
184+
185+
key2 = string.gsub(key2, "/%*", "/.*")
186+
key2 = string.gsub(key2, "%{[^/]+%}", "[^/]+")
187+
188+
return string.match(key1, "^" .. key2 .. "$") ~= nil
189+
end
190+
191+
-- Wrapper for keyMatch5
192+
function BuiltInFunctions.keyMatch5Func(args)
193+
BuiltInFunctions.validateVariadicArgs(2, args)
194+
return BuiltInFunctions.keyMatch5(args[1], args[2])
177195
end
178196

179197
-- RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
@@ -186,6 +204,12 @@ function BuiltInFunctions.regexMatch(key1, key2)
186204
end
187205
end
188206

207+
-- Wrapper for regexMatch
208+
function BuiltInFunctions.regexMatchFunc(args)
209+
BuiltInFunctions.validateVariadicArgs(2, args)
210+
return BuiltInFunctions.regexMatch(args[1], args[2])
211+
end
212+
189213
-- IPMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
190214
-- For example, "192.168.2.123" matches "192.168.2.0/24"
191215
function BuiltInFunctions.IPMatch(ip1, ip2)
@@ -232,12 +256,6 @@ function BuiltInFunctions.IPMatchFunc(args)
232256
return BuiltInFunctions.IPMatch(args[1], args[2])
233257
end
234258

235-
-- Wrapper for globMatch
236-
function BuiltInFunctions.globMatchFunc(args)
237-
BuiltInFunctions.validateVariadicArgs(2, args)
238-
return BuiltInFunctions.globMatch(args[1], args[2])
239-
end
240-
241259
-- GlobMatch determines whether key1 matches the pattern of key2 using glob pattern
242260
function BuiltInFunctions.globMatch(key1, key2)
243261
if posix.fnmatch(key2, key1, posix.FNM_PATHNAME or posix.FNM_PERIOD) == 0 then
@@ -247,28 +265,10 @@ function BuiltInFunctions.globMatch(key1, key2)
247265
end
248266
end
249267

250-
-- Wrapper for keyMatch5
251-
function BuiltInFunctions.keyMatch5Func(args)
268+
-- Wrapper for globMatch
269+
function BuiltInFunctions.globMatchFunc(args)
252270
BuiltInFunctions.validateVariadicArgs(2, args)
253-
return BuiltInFunctions.keyMatch5(args[1], args[2])
254-
end
255-
256-
-- KeyMatch5 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *
257-
-- For example,
258-
-- - "/foo/bar?status=1&type=2" matches "/foo/bar"
259-
-- - "/parent/child1" and "/parent/child1" matches "/parent/*"
260-
-- - "/parent/child1?status=1" matches "/parent/*"
261-
function BuiltInFunctions.keyMatch5(key1, key2)
262-
local i = string.find(key1, "?", 1, true)
263-
264-
if i then
265-
key1 = string.sub(key1, 1, i - 1)
266-
end
267-
268-
key2 = string.gsub(key2, "/%*", "/.*")
269-
key2 = string.gsub(key2, "%{[^/]+%}", "[^/]+")
270-
271-
return string.match(key1, "^" .. key2 .. "$") ~= nil
271+
return BuiltInFunctions.globMatch(args[1], args[2])
272272
end
273273

274274
-- GenerateGFunction is the factory method of the g(_, _) function.

tests/util/built_in_functions_spec.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ local BuiltInFunctions = require("src.util.BuiltInFunctions")
1616

1717
describe("BuiltInFunctions tests", function ()
1818
it("keyMatch tests", function ()
19+
assert.has_error(function () BuiltInFunctions.keyMatchFunc({"/"}) end, "Expected 2 arguments, but got 1")
20+
assert.has_error(function () BuiltInFunctions.keyMatchFunc({"/foo/create/123", "/*", "/foo/update/123"}) end, "Expected 2 arguments, but got 3")
21+
assert.has_error(function () BuiltInFunctions.keyMatchFunc({"/foo", true}) end, "Argument must be a string")
22+
1923
assert.is.False(BuiltInFunctions.keyMatch("/foo", "/"))
2024
assert.is.True(BuiltInFunctions.keyMatch("/foo", "/foo"))
2125
assert.is.True(BuiltInFunctions.keyMatch("/foo", "/foo*"))
@@ -29,6 +33,10 @@ describe("BuiltInFunctions tests", function ()
2933
end)
3034

3135
it("keyGet tests", function ()
36+
assert.has_error(function () BuiltInFunctions.keyGetFunc({"/foo/bar/foo"}) end, "Expected 2 arguments, but got 1")
37+
assert.has_error(function () BuiltInFunctions.keyGetFunc({"/foo/bar/foo", "/foo/*", "/bar"}) end, "Expected 2 arguments, but got 3")
38+
assert.has_error(function () BuiltInFunctions.keyGetFunc({"/foo/bar/foo", true}) end, "Argument must be a string")
39+
3240
assert.are.same("", BuiltInFunctions.keyGet("/foo", "/foo"))
3341
assert.are.same("", BuiltInFunctions.keyGet("/foo", "/foo*"))
3442
assert.are.same("", BuiltInFunctions.keyGet("/foo", "/foo/*"))
@@ -41,6 +49,10 @@ describe("BuiltInFunctions tests", function ()
4149
end)
4250

4351
it("keyMatch2 tests", function ()
52+
assert.has_error(function () BuiltInFunctions.keyMatch2Func({"/"}) end, "Expected 2 arguments, but got 1")
53+
assert.has_error(function () BuiltInFunctions.keyMatch2Func({"/foo/create/123", "/*", "/foo/update/123"}) end, "Expected 2 arguments, but got 3")
54+
assert.has_error(function () BuiltInFunctions.keyMatch2Func({"/foo", true}) end, "Argument must be a string")
55+
4456
assert.is.False(BuiltInFunctions.keyMatch2("/foo", "/"))
4557
assert.is.True(BuiltInFunctions.keyMatch2("/foo", "/foo"))
4658
assert.is.True(BuiltInFunctions.keyMatch2("/foo", "/foo*"))
@@ -73,6 +85,11 @@ describe("BuiltInFunctions tests", function ()
7385
end)
7486

7587
it("keyGet2 tests", function ()
88+
assert.has_error(function () BuiltInFunctions.keyGet2Func({"/foo"}) end, "Expected 3 arguments, but got 1")
89+
assert.has_error(function () BuiltInFunctions.keyGet2Func({"/foo", "/:bar"}) end, "Expected 3 arguments, but got 2")
90+
assert.has_error(function () BuiltInFunctions.keyGet2Func({"/foo", "/:bar", "bar", "foobar"}) end, "Expected 3 arguments, but got 4")
91+
assert.has_error(function () BuiltInFunctions.keyGet2Func({"/foo", "/:bar", true}) end, "Argument must be a string")
92+
7693
assert.are.equal("",BuiltInFunctions.keyGet2("/foo", "/foo", "id"))
7794
assert.are.equal("",BuiltInFunctions.keyGet2("/foo", "/foo*", "id"))
7895
assert.are.equal("",BuiltInFunctions.keyGet2("/foo", "/foo/*", "id"))
@@ -106,6 +123,10 @@ describe("BuiltInFunctions tests", function ()
106123
end)
107124

108125
it("keyMatch3 tests", function ()
126+
assert.has_error(function () BuiltInFunctions.keyMatch3Func({"/"}) end, "Expected 2 arguments, but got 1")
127+
assert.has_error(function () BuiltInFunctions.keyMatch3Func({"/foo/create/123", "/*", "/foo/update/123"}) end, "Expected 2 arguments, but got 3")
128+
assert.has_error(function () BuiltInFunctions.keyMatch3Func({"/foo", true}) end, "Argument must be a string")
129+
109130
assert.is.True(BuiltInFunctions.keyMatch3("/foo", "/foo"))
110131
assert.is.True(BuiltInFunctions.keyMatch3("/foo", "/foo*"))
111132
assert.is.False(BuiltInFunctions.keyMatch3("/foo", "/foo/*"))
@@ -132,6 +153,10 @@ describe("BuiltInFunctions tests", function ()
132153
end)
133154

134155
it("keyMatch4 tests", function ()
156+
assert.has_error(function () BuiltInFunctions.keyMatch4Func({"/parent/123/child/123"}) end, "Expected 2 arguments, but got 1")
157+
assert.has_error(function () BuiltInFunctions.keyMatch4Func({"/parent/123/child/123", "/parent/{id}/child/{id}", true}) end, "Expected 2 arguments, but got 3")
158+
assert.has_error(function () BuiltInFunctions.keyMatch4Func({"/parent/123/child/123", true}) end, "Argument must be a string")
159+
135160
assert.is.True(BuiltInFunctions.keyMatch4("/parent/123/child/123", "/parent/{id}/child/{id}"))
136161
assert.is.False(BuiltInFunctions.keyMatch4("/parent/123/child/456", "/parent/{id}/child/{id}"))
137162

@@ -148,6 +173,10 @@ describe("BuiltInFunctions tests", function ()
148173
end)
149174

150175
it("regexMatch tests", function ()
176+
assert.has_error(function () BuiltInFunctions.regexMatchFunc({"/topic/create"}) end, "Expected 2 arguments, but got 1")
177+
assert.has_error(function () BuiltInFunctions.regexMatchFunc({"/topic/create/123", "/topic/create", "/topic/update"}) end, "Expected 2 arguments, but got 3")
178+
assert.has_error(function () BuiltInFunctions.regexMatchFunc({"/topic/create", false}) end, "Argument must be a string")
179+
151180
assert.is.True(BuiltInFunctions.regexMatch("/topic/create", "/topic/create"))
152181
assert.is.True(BuiltInFunctions.regexMatch("/topic/create/123", "/topic/create"))
153182
assert.is.False(BuiltInFunctions.regexMatch("/topic/delete", "/topic/create"))
@@ -160,6 +189,10 @@ describe("BuiltInFunctions tests", function ()
160189
end)
161190

162191
it("globMatch tests", function ()
192+
assert.has_error(function () BuiltInFunctions.globMatchFunc({"/foo"}) end, "Expected 2 arguments, but got 1")
193+
assert.has_error(function () BuiltInFunctions.globMatchFunc({"/foo", "/bar", "/foobar"}) end, "Expected 2 arguments, but got 3")
194+
assert.has_error(function () BuiltInFunctions.globMatchFunc({"/foo", 128}) end, "Argument must be a string")
195+
163196
assert.is.True(BuiltInFunctions.globMatch("/foo", "/foo"))
164197
assert.is.True(BuiltInFunctions.globMatch("/foo", "/foo*"))
165198
assert.is.False(BuiltInFunctions.globMatch("/foo", "/foo/*"))
@@ -193,6 +226,10 @@ describe("BuiltInFunctions tests", function ()
193226
end)
194227

195228
it("IPMatch tests", function ()
229+
assert.has_error(function () BuiltInFunctions.IPMatchFunc({"192.168.2.123"}) end, "Expected 2 arguments, but got 1")
230+
assert.has_error(function () BuiltInFunctions.IPMatchFunc({"192.168.2.123", "192.168.2.0/24", "192.168.2.0/26"}) end, "Expected 2 arguments, but got 3")
231+
assert.has_error(function () BuiltInFunctions.IPMatchFunc({"192.168.2.123", 128}) end, "Argument must be a string")
232+
196233
assert.is.True(BuiltInFunctions.IPMatch("192.168.2.123", "192.168.2.0/24"))
197234
assert.is.True(BuiltInFunctions.IPMatch("192.168.2.123", "192.168.2.0/25"))
198235
assert.is.False(BuiltInFunctions.IPMatch("192.168.2.123", "192.168.2.0/26"))

0 commit comments

Comments
 (0)