Skip to content

Commit 99f4049

Browse files
authored
feat: support 'has' to match element in array (#76)
The `graphql_root_fields` variable will be a table contains multiple root fields, so we need this change.
1 parent 8256908 commit 99f4049

File tree

8 files changed

+66
-86
lines changed

8 files changed

+66
-86
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "t/lib/toolkit"]
2+
path = t/lib/toolkit
3+
url = https://github.com/api7/test-toolkit.git

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ local rx = radix.new({
172172
|< |less than |{"arg_age", "<", 24}|
173173
|~~ |Regular match|{"arg_name", "~~", "[a-z]+"}|
174174
|in |find in array|{"arg_name", "in", {"1","2"}}|
175+
|has |left value array has value in the right |{"graphql_root_fields", "has", "repo"}|
175176

176177
[Back to TOC](#table-of-contents)
177178

lib/resty/radixtree.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,20 @@ local function in_array(l_v, r_v)
496496
return false
497497
end
498498

499+
local function has_element(l_v, r_v)
500+
if type(l_v) == "table" then
501+
for _, v in ipairs(l_v) do
502+
if v == r_v then
503+
return true
504+
end
505+
end
506+
507+
return false
508+
end
509+
510+
return false
511+
end
512+
499513
local compare_funcs = {
500514
["=="] = function (l_v, r_v)
501515
if type(r_v) == "number" then
@@ -534,6 +548,7 @@ local compare_funcs = {
534548
end,
535549
["IN"] = in_array,
536550
["in"] = in_array,
551+
["has"] = has_element,
537552
}
538553

539554

t/lib/ljson.lua

Lines changed: 0 additions & 73 deletions
This file was deleted.

t/lib/toolkit

Submodule toolkit added at ab2471c

t/matched.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ metadata prefix matching with GET, matched = {}
3636
--- config
3737
location /t {
3838
content_by_lua_block {
39-
local json = require("cjson.safe")
39+
local json = require("toolkit.json")
4040
local radix = require("resty.radixtree")
4141
local rx = radix.new({
4242
{
@@ -62,7 +62,7 @@ metadata prefix matching with host, matched = {}
6262
--- config
6363
location /t {
6464
content_by_lua_block {
65-
local json = require("cjson.safe")
65+
local json = require("toolkit.json")
6666
local radix = require("resty.radixtree")
6767
local rx = radix.new({
6868
{
@@ -90,7 +90,7 @@ metadata prefix matching with host, matched = {}
9090
--- config
9191
location /t {
9292
content_by_lua_block {
93-
local json = require("cjson.safe")
93+
local json = require("toolkit.json")
9494
local radix = require("resty.radixtree")
9595
local rx = radix.new({
9696
{
@@ -125,7 +125,7 @@ metadata prefix matching with method, host, vars, matched = {}
125125
--- config
126126
location /t {
127127
content_by_lua_block {
128-
local json = require("cjson.safe")
128+
local json = require("toolkit.json")
129129
local radix = require("resty.radixtree")
130130
local rx = radix.new({
131131
{
@@ -154,4 +154,4 @@ GET /t?k=v
154154
--- no_error_log
155155
[error]
156156
--- response_body
157-
after dispatch success get matched: {"_path":"\/hello*","_method":"GET","_host":"foo.com"}
157+
after dispatch success get matched: {"_host":"foo.com","_method":"GET","_path":"/hello*"}

t/parameter.t

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ __DATA__
1111
--- config
1212
location /t {
1313
content_by_lua_block {
14-
local json = require("ljson")
14+
local json = require("toolkit.json")
1515
local radix = require("resty.radixtree")
1616
local rx = radix.new({
1717
{
@@ -46,7 +46,7 @@ matched: {"_path":"/name/*name","name":""}
4646
--- config
4747
location /t {
4848
content_by_lua_block {
49-
local json = require("ljson")
49+
local json = require("toolkit.json")
5050
local radix = require("resty.radixtree")
5151
local rx = radix.new({
5252
{
@@ -81,7 +81,7 @@ matched: {":ext":"","_path":"/name/*"}
8181
--- config
8282
location /t {
8383
content_by_lua_block {
84-
local json = require("ljson")
84+
local json = require("toolkit.json")
8585
local radix = require("resty.radixtree")
8686
local rx = radix.new({
8787
{
@@ -117,7 +117,7 @@ matched: []
117117
--- config
118118
location /t {
119119
content_by_lua_block {
120-
local json = require("ljson")
120+
local json = require("toolkit.json")
121121
local radix = require("resty.radixtree")
122122
local rx = radix.new({
123123
{
@@ -146,7 +146,7 @@ matched: {"_path":"/name/:name/id/:id/*other","id":"1","name":"json","other":"fo
146146
--- config
147147
location /t {
148148
content_by_lua_block {
149-
local json = require("ljson")
149+
local json = require("toolkit.json")
150150
local radix = require("resty.radixtree")
151151
local rx = radix.new({
152152
{
@@ -175,7 +175,7 @@ matched: []
175175
--- config
176176
location /t {
177177
content_by_lua_block {
178-
local json = require("ljson")
178+
local json = require("toolkit.json")
179179
local radix = require("resty.radixtree")
180180
local rx = radix.new({
181181
{
@@ -210,7 +210,7 @@ matched: []
210210
--- config
211211
location /t {
212212
content_by_lua_block {
213-
local json = require("ljson")
213+
local json = require("toolkit.json")
214214
local radix = require("resty.radixtree")
215215
local rx = radix.new({
216216
{
@@ -242,7 +242,7 @@ pcre pat:
242242
--- config
243243
location /t {
244244
content_by_lua_block {
245-
local json = require("ljson")
245+
local json = require("toolkit.json")
246246
local radix = require("resty.radixtree")
247247
local rx = radix.new({
248248
{

t/vars.t

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,36 @@ metadata /aa
533533
nil
534534
nil
535535
nil
536+
537+
538+
539+
=== TEST 19: operator has
540+
--- config
541+
location /t {
542+
content_by_lua_block {
543+
local radix = require("resty.radixtree")
544+
local rx = radix.new({
545+
{
546+
paths = "/aa",
547+
metadata = "metadata /aa",
548+
vars = {
549+
{"x", "has", "a"},
550+
},
551+
}
552+
})
553+
554+
ngx.say(rx:match("/aa", {vars = {x = {'a', 'b'}}}))
555+
ngx.say(rx:match("/aa", {vars = {x = {'a'}}}))
556+
ngx.say(rx:match("/aa", {vars = {x = {'b'}}}))
557+
ngx.say(rx:match("/aa", {vars = {x = {}}}))
558+
}
559+
}
560+
--- request
561+
GET /t
562+
--- no_error_log
563+
[error]
564+
--- response_body
565+
metadata /aa
566+
metadata /aa
567+
nil
568+
nil

0 commit comments

Comments
 (0)