Skip to content

Commit fcd4237

Browse files
authored
Merge pull request #14 from 3scale/THREESCALE-6594
Threescale 6594 Adds date filter support
2 parents 0931c36 + a8d7562 commit fcd4237

4 files changed

Lines changed: 81 additions & 1 deletion

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ install:
1717
- docker run --name testContainer -d -v $(pwd):/opt/code openresty/openresty:bionic sleep 1h
1818
- docker exec -ti testContainer apt-get install -qq -y cpanminus axel
1919
- docker exec -ti testContainer cpanm --notest Test::Nginx
20+
- docker exec -ti testContainer luarocks install date 2.2-2
2021

2122
script:
2223
- docker exec -ti testContainer bash -c "cd /opt/code; prove -r t"

lib/liquid.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local cjson = require 'cjson'
2+
local Datelib = require 'date'
23
local Liquid = {}
34
local Lexer = {}
45
local Parser = {}
@@ -3076,6 +3077,34 @@ local function str_reverse( str )
30763077
return string.reverse(str)
30773078
end
30783079
--=== String filter end
3080+
--=== Date filter
3081+
local function date_parse(obj)
3082+
local dateobj = Datelib(obj)
3083+
return dateobj
3084+
end
3085+
3086+
local function date_now()
3087+
local dateobj = Datelib(true)
3088+
return dateobj
3089+
end
3090+
3091+
local date_func =
3092+
{
3093+
["now"] = date_now,
3094+
["default"] = date_parse,
3095+
}
3096+
3097+
local function date(obj, format)
3098+
local date_obj = nil;
3099+
local func = date_func[obj]
3100+
if func then
3101+
date_obj = func(obj)
3102+
else
3103+
date_obj = date_func["default"](obj)
3104+
end
3105+
return date_obj:fmt(format)
3106+
end
3107+
--=== Date filter end
30793108
--=== Additional filter begin
30803109
local function json( obj )
30813110
-- body
@@ -3134,6 +3163,9 @@ FilterSet:add_filter("url_encode", url_encode )
31343163
FilterSet:add_filter("url_decode", url_decode )
31353164
FilterSet:add_filter("str_reverse", str_reverse )
31363165

3166+
--Date filter
3167+
FilterSet:add_filter("date", date )
3168+
31373169
--Additinal filter
31383170
FilterSet:add_filter("json", json )
31393171
FilterSet:add_filter("get", get)

rockspecs/liquid-scm-1.rockspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ description = {
1010
homepage = "https://github.com/chenxianyu2015/liquid-lua",
1111
license = "BSD-2-Clause"
1212
}
13-
dependencies = {}
13+
dependencies = {
14+
"date >= 2.2-2"
15+
}
1416
build = {
1517
type = "builtin",
1618
modules = {

t/date_filters.t

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use Test::Nginx::Socket::Lua;
2+
use Cwd qw(cwd);
3+
4+
# repeat_each(2);
5+
6+
plan tests => repeat_each() * (3 * blocks());
7+
8+
my $pwd = cwd();
9+
10+
our $HttpConfig = qq{
11+
lua_package_path "$pwd/lib/?.lua;;;";
12+
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
13+
};
14+
15+
16+
no_long_string();
17+
#no_diff();
18+
19+
run_tests();
20+
21+
__DATA__
22+
23+
=== TEST 1: 'date' filter.
24+
--- http_config eval: $::HttpConfig
25+
--- config
26+
location /t {
27+
content_by_lua_block {
28+
local Liquid = require 'liquid'
29+
local Lexer = Liquid.Lexer
30+
local Parser = Liquid.Parser
31+
local Interpreter = Liquid.Interpreter
32+
local document = "{% assign a = \"03/Jan/2022:09:24:58 +0000\" %} {{ a | date: \"%m %d, %Y %H:%M:%S\" }}"
33+
local lexer = Lexer:new(document)
34+
local parser = Parser:new(lexer)
35+
local interpreter = Interpreter:new(parser)
36+
ngx.say(interpreter:interpret())
37+
}
38+
}
39+
--- request
40+
GET /t
41+
--- response_body
42+
01 03, 2022 09:24:58
43+
44+
--- no_error_log
45+
[error]

0 commit comments

Comments
 (0)