Skip to content

Commit 7486760

Browse files
authored
Merge pull request #1191 from HubSpot/unixtimestampfunc-default
Do not require argument to unixtimestamp function
2 parents 655d310 + ca64a81 commit 7486760

4 files changed

Lines changed: 36 additions & 40 deletions

File tree

src/main/java/com/hubspot/jinjava/lib/filter/DateTimeFormatFilter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
value = "Formats a date object",
1616
input = @JinjavaParam(
1717
value = "value",
18-
defaultValue = "current time",
1918
desc = "The date variable or UNIX timestamp to format",
2019
required = true
2120
),
Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package com.hubspot.jinjava.lib.filter;
22

3+
import static com.hubspot.jinjava.lib.filter.time.DateTimeFormatHelper.FIXED_DATE_TIME_FILTER_NULL_ARG;
4+
35
import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
46
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
57
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
8+
import com.hubspot.jinjava.features.DateTimeFeatureActivationStrategy;
9+
import com.hubspot.jinjava.features.FeatureActivationStrategy;
10+
import com.hubspot.jinjava.interpret.InvalidArgumentException;
611
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
12+
import com.hubspot.jinjava.interpret.TemplateError;
713
import com.hubspot.jinjava.lib.fn.Functions;
814

915
@JinjavaDoc(
1016
value = "Gets the UNIX timestamp value (in milliseconds) of a date object",
11-
input = @JinjavaParam(
12-
value = "value",
13-
defaultValue = "current time",
14-
desc = "The date variable",
15-
required = true
16-
),
17+
input = @JinjavaParam(value = "value", desc = "The date variable", required = true),
1718
snippets = { @JinjavaSnippet(code = "{% mydatetime|unixtimestamp %}") }
1819
)
1920
public class UnixTimestampFilter implements Filter {
@@ -25,6 +26,27 @@ public String getName() {
2526

2627
@Override
2728
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
29+
if (var == null) {
30+
interpreter.addError(
31+
TemplateError.fromMissingFilterArgException(
32+
new InvalidArgumentException(
33+
interpreter,
34+
"unixtimestamp",
35+
"unixtimestamp filter called with null datetime"
36+
)
37+
)
38+
);
39+
40+
FeatureActivationStrategy feat = interpreter
41+
.getConfig()
42+
.getFeatures()
43+
.getActivationStrategy(FIXED_DATE_TIME_FILTER_NULL_ARG);
44+
45+
if (feat.isActive(interpreter.getContext())) {
46+
var = ((DateTimeFeatureActivationStrategy) feat).getActivateAt();
47+
}
48+
}
49+
2850
return Functions.unixtimestamp(var);
2951
}
3052
}

src/main/java/com/hubspot/jinjava/lib/fn/Functions.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static ZonedDateTime today(String... var) {
182182
@JinjavaDoc(
183183
value = "formats a date to a string",
184184
params = {
185-
@JinjavaParam(value = "var", type = "date", defaultValue = "current time"),
185+
@JinjavaParam(value = "var", type = "date", required = true),
186186
@JinjavaParam(
187187
value = "format",
188188
defaultValue = StrftimeFormatter.DEFAULT_DATE_FORMAT
@@ -310,36 +310,11 @@ public static ZonedDateTime getDateTimeArg(Object var, ZoneId zoneOffset) {
310310

311311
@JinjavaDoc(
312312
value = "gets the unix timestamp milliseconds value of a datetime",
313-
params = {
314-
@JinjavaParam(value = "var", type = "date", defaultValue = "current time"),
315-
}
313+
params = { @JinjavaParam(value = "var", type = "date", required = true) }
316314
)
317315
public static long unixtimestamp(Object... var) {
318316
Object filterVar = var == null || var.length == 0 ? null : var[0];
319317

320-
if (filterVar == null) {
321-
JinjavaInterpreter interpreter = JinjavaInterpreter.getCurrent();
322-
323-
interpreter.addError(
324-
TemplateError.fromMissingFilterArgException(
325-
new InvalidArgumentException(
326-
interpreter,
327-
"unixtimestamp",
328-
"unixtimestamp filter called with null datetime"
329-
)
330-
)
331-
);
332-
333-
FeatureActivationStrategy feat = interpreter
334-
.getConfig()
335-
.getFeatures()
336-
.getActivationStrategy(FIXED_DATE_TIME_FILTER_NULL_ARG);
337-
338-
if (feat.isActive(interpreter.getContext())) {
339-
filterVar = ((DateTimeFeatureActivationStrategy) feat).getActivateAt();
340-
}
341-
}
342-
343318
ZonedDateTime d = getDateTimeArg(filterVar, ZoneOffset.UTC);
344319

345320
if (d == null) {

src/test/java/com/hubspot/jinjava/lib/fn/UnixTimestampFunctionTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ public void tearDown() {
2828

2929
@Test
3030
public void itGetsUnixTimestamps() {
31-
JinjavaInterpreter.pushCurrent(
32-
new JinjavaInterpreter(
33-
new Jinjava(),
34-
new Context(),
35-
JinjavaConfig.newBuilder().build()
36-
)
31+
JinjavaInterpreter jinjavaInterpreter = new JinjavaInterpreter(
32+
new Jinjava(),
33+
new Context(),
34+
JinjavaConfig.newBuilder().build()
3735
);
36+
JinjavaInterpreter.pushCurrent(jinjavaInterpreter);
3837
assertThat(Functions.unixtimestamp())
3938
.isGreaterThan(0)
4039
.isLessThanOrEqualTo(System.currentTimeMillis());
4140
assertThat(Functions.unixtimestamp(epochMilliseconds)).isEqualTo(epochMilliseconds);
4241
assertThat(Functions.unixtimestamp(d)).isEqualTo(epochMilliseconds);
4342
assertThat(Functions.unixtimestamp((Object) null))
4443
.isCloseTo(System.currentTimeMillis(), Offset.offset(1000L));
44+
assertThat(jinjavaInterpreter.getErrors()).isEmpty();
4545
}
4646

4747
@Test

0 commit comments

Comments
 (0)