Skip to content

Commit

Permalink
Merge pull request #120 from Edmond-J-A/master
Browse files Browse the repository at this point in the history
feat: Add keyMatch4
  • Loading branch information
hsluoyz authored Aug 20, 2021
2 parents 757edd1 + ff81f13 commit 543ae0f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/util/BuiltInFunctions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,45 @@ function BuiltInFunctions.keyMatch3(key1, key2)
return BuiltInFunctions.regexMatch(key1, "^"..key.."$")
end

-- Wrapper for keyMatch4
function BuiltInFunctions.keyMatch4Func(args)
BuiltInFunctions.validateVariadicArgs(2, args)
return BuiltInFunctions.keyMatch4(args[1], args[2])
end

-- KeyMatch4 determines whether key1 matches the pattern of key2 (similar to RESTful path), key2 can contain a *.
-- Besides what KeyMatch3 does, KeyMatch4 can also match repeated patterns:
-- "/parent/123/child/123" matches "/parent/{id}/child/{id}"
-- "/parent/123/child/456" does not match "/parent/{id}/child/{id}"
-- But KeyMatch3 will match both.
function BuiltInFunctions.keyMatch4(key1, key2)
key2 = string.gsub(key2, "/%*", "/.*")
local tokens={}
local repl=function(s)
table.insert(tokens, string.sub(s, 1, -1))
return "([^/]+)"
end
key2=string.gsub(key2,"{([^/]+)}",repl)
if string.match(key1, key2)==nil then
return false
end
local matches={string.match(key1, key2)}
if #tokens~= #matches then
error("KeyMatch4: number of tokens is not equal to number of values")
end
local values={}
for key, token in pairs(tokens) do
if values[token]==nil then
values[token] = matches[key]
end
if values[token] ~= matches[key] then
return false
end
end

return true
end

-- Wrapper for regexMatch
function BuiltInFunctions.regexMatchFunc(args)
BuiltInFunctions.validateVariadicArgs(2, args)
Expand Down
16 changes: 16 additions & 0 deletions tests/util/built_in_functions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ describe("BuiltInFunctions tests", function ()
assert.is.False(BuiltInFunctions.keyMatch3("/myid/using/myresid", "/{id/using/{resId}"))
end)

it("keyMatch4 tests", function ()
assert.is.True(BuiltInFunctions.keyMatch4("/parent/123/child/123", "/parent/{id}/child/{id}"))
assert.is.False(BuiltInFunctions.keyMatch4("/parent/123/child/456", "/parent/{id}/child/{id}"))

assert.is.True(BuiltInFunctions.keyMatch4("/parent/123/child/123", "/parent/{id}/child/{another_id}"))
assert.is.True(BuiltInFunctions.keyMatch4("/parent/123/child/456", "/parent/{id}/child/{another_id}"))

assert.is.True(BuiltInFunctions.keyMatch4("/parent/123/child/123/book/123", "/parent/{id}/child/{id}/book/{id}"))
assert.is.False(BuiltInFunctions.keyMatch4("/parent/123/child/123/book/456", "/parent/{id}/child/{id}/book/{id}"))
assert.is.False(BuiltInFunctions.keyMatch4("/parent/123/child/456/book/123", "/parent/{id}/child/{id}/book/{id}"))
assert.is.False(BuiltInFunctions.keyMatch4("/parent/123/child/456/book/", "/parent/{id}/child/{id}/book/{id}"))
assert.is.False(BuiltInFunctions.keyMatch4("/parent/123/child/456", "/parent/{id}/child/{id}/book/{id}"))

assert.is.False(BuiltInFunctions.keyMatch4("/parent/123/child/123", "/parent/{i/d}/child/{i/d}"))
end)

it("regexMatch tests", function ()
assert.is.True(BuiltInFunctions.regexMatch("/topic/create", "/topic/create"))
assert.is.True(BuiltInFunctions.regexMatch("/topic/create/123", "/topic/create"))
Expand Down

0 comments on commit 543ae0f

Please sign in to comment.