Skip to content

Commit 250d73d

Browse files
authored
feat: avoid requiring extra dependency unless we really need it (#59)
1 parent e2813da commit 250d73d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

lib/jsonschema.lua

+11-4
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,24 @@ do
4040
end
4141

4242
local match_pattern
43+
local rex_find
4344
if ngx then
4445
local function re_find(s, p)
4546
return ngx.re.find(s, p, "jo")
4647
end
4748
match_pattern = re_find
4849
else
49-
local ok, rex = pcall(require, "rex_pcre")
50-
if not ok then
51-
error("depends on lrexlib-pcre, please install it first: " .. rex)
50+
match_pattern = function (s, p)
51+
if not rex_find then
52+
local ok, rex = pcall(require, "rex_pcre")
53+
if not ok then
54+
error("depends on lrexlib-pcre, please install it first: " .. rex)
55+
end
56+
57+
rex_find = rex.find
58+
end
59+
return rex_find(s, p)
5260
end
53-
match_pattern = rex.find
5461
end
5562

5663
local parse_ipv4

0 commit comments

Comments
 (0)