Skip to content

Commit

Permalink
Liquid: Fix issues with non-alphanumeric keys
Browse files Browse the repository at this point in the history
Fix #17

Signed-off-by: Eloy Coto <[email protected]>
  • Loading branch information
eloycoto committed Jan 15, 2020
1 parent 58e0a19 commit 948cb54
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/liquid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,12 @@ function InterpreterContext:find_var( name )
if name == nil then
error("Invalid var name")
end

-- Just returns the full context if the value if self.
if name == "self" then
return self.stackframe
end

local value = nil
local length = #(self.stackframe)
local step = -1
Expand Down Expand Up @@ -2978,8 +2984,16 @@ local function json( obj )
-- body
return cjson.encode(obj)
end


local function get(obj, key)
return obj[key]
end

--=== Additional filter end



--========================================================== add filers to FilterSet instance================================
--Array filter
FilterSet:add_filter("join", join )
Expand Down Expand Up @@ -3025,7 +3039,7 @@ FilterSet:add_filter("str_reverse", str_reverse )

--Additinal filter
FilterSet:add_filter("json", json )

FilterSet:add_filter("get", get)



Expand Down
53 changes: 53 additions & 0 deletions t/additional_filters.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use Test::Nginx::Socket 'no_plan';
use Cwd qw(cwd);

my $pwd = cwd();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;;";
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
};

run_tests();

__DATA__
=== TEST 1: get filter.
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local context = {}
context.foo = "123"
local Liquid = require 'liquid'
local document = 'SELF::{{ self | first | get: "foo" }}'
local template = Liquid.Template:parse(document)
ngx.say(template:render( Liquid.InterpreterContext:new(context)))
}
}
--- request
GET /t
--- response_body
SELF::123
=== TEST 2: get filter with non alphanumeric keys
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local context = {}
context["foo/bar"] = "123"
local Liquid = require 'liquid'
local document = 'SELF::{{ self | first | get: "foo/bar" }}'
local template = Liquid.Template:parse(document)
ngx.say(template:render( Liquid.InterpreterContext:new(context)))
}
}
--- request
GET /t
--- response_body
SELF::123

0 comments on commit 948cb54

Please sign in to comment.