Skip to content

Commit df33a9c

Browse files
authored
test: use ljson to encode lua table, we can got same encode result. (#71)
1 parent b725eeb commit df33a9c

File tree

3 files changed

+26
-52
lines changed

3 files changed

+26
-52
lines changed

lib/resty/radixtree.lua

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,18 +652,6 @@ local function _match_from_routes(routes, path, opts, args)
652652

653653
local opts_matched_exists = (opts.matched ~= nil)
654654
for _, route in ipairs(routes) do
655-
if route.path_op == "=" then
656-
if route.path == path then
657-
if match_route_opts(route, opts, args) then
658-
if opts_matched_exists then
659-
opts.matched._path = path
660-
end
661-
return route
662-
end
663-
end
664-
goto continue
665-
end
666-
667655
if match_route_opts(route, opts, args) then
668656
-- log_info("matched route: ", require("cjson").encode(route))
669657
-- log_info("matched path: ", path)
@@ -674,8 +662,6 @@ local function _match_from_routes(routes, path, opts, args)
674662
return route
675663
end
676664
end
677-
678-
::continue::
679665
end
680666

681667
return nil

t/lib/ljson.lua

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local new_tab = require("table.new")
2+
local isarray = require("table.isarray")
13
local ngx_null = ngx.null
24
local tostring = tostring
35
local gsub = string.gsub
@@ -6,9 +8,6 @@ local pairs = pairs
68
local ipairs = ipairs
79
local concat = table.concat
810
local type = type
9-
local new_tab = require("table.new")
10-
11-
local _M = {version = 0.1}
1211

1312
local metachars = {
1413
['\t'] = '\\t',
@@ -17,27 +16,15 @@ local metachars = {
1716
['\r'] = '\\r',
1817
['\n'] = '\\n',
1918
}
19+
local _M = {}
20+
2021

2122
local function encode_str(s)
22-
-- XXX we will rewrite this when string.buffer is implemented
23-
-- in LuaJIT 2.1 because string.gsub cannot be JIT compiled.
2423
return gsub(s, '["\\\r\n\t]', metachars)
2524
end
2625

27-
local function is_arr(t)
28-
local exp = 1
29-
for k, _ in pairs(t) do
30-
if k ~= exp then
31-
return nil
32-
end
33-
exp = exp + 1
34-
end
35-
return exp - 1
36-
end
3726

38-
local encode
39-
40-
encode = function (v)
27+
local function encode(v)
4128
if v == nil or v == ngx_null then
4229
return "null"
4330
end
@@ -52,8 +39,8 @@ encode = function (v)
5239
end
5340

5441
if typ == 'table' then
55-
local n = is_arr(v)
56-
if n then
42+
if isarray(v) then
43+
local n = #v
5744
local bits = new_tab(n, 0)
5845
for i, elem in ipairs(v) do
5946
bits[i] = encode(elem)
@@ -82,4 +69,5 @@ encode = function (v)
8269
end
8370
_M.encode = encode
8471

72+
8573
return _M

t/parameter.t

Lines changed: 18 additions & 18 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("cjson.safe")
14+
local json = require("ljson")
1515
local radix = require("resty.radixtree")
1616
local rx = radix.new({
1717
{
@@ -36,17 +36,17 @@ GET /t
3636
[error]
3737
--- response_body
3838
match meta: metadata /name
39-
matched: {"_path":"\/name\/*name","name":"json"}
39+
matched: {"_path":"/name/*name","name":"json"}
4040
match meta: metadata /name
41-
matched: {"_path":"\/name\/*name","name":""}
41+
matched: {"_path":"/name/*name","name":""}
4242
4343
4444
4545
=== TEST 2: /name/* in path
4646
--- config
4747
location /t {
4848
content_by_lua_block {
49-
local json = require("cjson.safe")
49+
local json = require("ljson")
5050
local radix = require("resty.radixtree")
5151
local rx = radix.new({
5252
{
@@ -71,17 +71,17 @@ GET /t
7171
[error]
7272
--- response_body
7373
match meta: metadata /name
74-
matched: {"_path":"\/name\/*",":ext":"json\/foo\/bar"}
74+
matched: {":ext":"json/foo/bar","_path":"/name/*"}
7575
match meta: metadata /name
76-
matched: {"_path":"\/name\/*",":ext":""}
76+
matched: {":ext":"","_path":"/name/*"}
7777
7878
7979
8080
=== TEST 3: /name/:name/id/:id
8181
--- config
8282
location /t {
8383
content_by_lua_block {
84-
local json = require("cjson.safe")
84+
local json = require("ljson")
8585
local radix = require("resty.radixtree")
8686
local rx = radix.new({
8787
{
@@ -107,17 +107,17 @@ GET /t
107107
[error]
108108
--- response_body
109109
match meta: metadata /name
110-
matched: {"name":"json","_path":"\/name\/:name\/id\/:id","id":"1"}
110+
matched: {"_path":"/name/:name/id/:id","id":"1","name":"json"}
111111
match meta: nil
112-
matched: {}
112+
matched: []
113113
114114
115115
116116
=== TEST 4: /name/:name/id/:id/*other
117117
--- config
118118
location /t {
119119
content_by_lua_block {
120-
local json = require("cjson.safe")
120+
local json = require("ljson")
121121
local radix = require("resty.radixtree")
122122
local rx = radix.new({
123123
{
@@ -138,15 +138,15 @@ GET /t
138138
[error]
139139
--- response_body
140140
match meta: metadata /name
141-
matched: {"other":"foo\/bar\/gloo","name":"json","_path":"\/name\/:name\/id\/:id\/*other","id":"1"}
141+
matched: {"_path":"/name/:name/id/:id/*other","id":"1","name":"json","other":"foo/bar/gloo"}
142142
143143
144144
145145
=== TEST 5: /name/:name/id/:id (not match)
146146
--- config
147147
location /t {
148148
content_by_lua_block {
149-
local json = require("cjson.safe")
149+
local json = require("ljson")
150150
local radix = require("resty.radixtree")
151151
local rx = radix.new({
152152
{
@@ -167,15 +167,15 @@ GET /t
167167
[error]
168168
--- response_body
169169
match meta: nil
170-
matched: {}
170+
matched: []
171171
172172
173173
174174
=== TEST 6: /name/:name/foo (cached parameter)
175175
--- config
176176
location /t {
177177
content_by_lua_block {
178-
local json = require("cjson.safe")
178+
local json = require("ljson")
179179
local radix = require("resty.radixtree")
180180
local rx = radix.new({
181181
{
@@ -200,17 +200,17 @@ GET /t
200200
[error]
201201
--- response_body
202202
match meta: metadata /name
203-
matched: {"_path":"\/name\/:name\/foo","name":"json"}
203+
matched: {"_path":"/name/:name/foo","name":"json"}
204204
match meta: nil
205-
matched: {}
205+
matched: []
206206
207207
208208
209209
=== TEST 7: /name/:name/foo (no cached parameter)
210210
--- config
211211
location /t {
212212
content_by_lua_block {
213-
local json = require("cjson.safe")
213+
local json = require("ljson")
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("cjson.safe")
245+
local json = require("ljson")
246246
local radix = require("resty.radixtree")
247247
local rx = radix.new({
248248
{

0 commit comments

Comments
 (0)