Skip to content

Commit

Permalink
Do not require paramter on unixtimestamp function
Browse files Browse the repository at this point in the history
  • Loading branch information
jboulter committed Jun 6, 2024
1 parent 655d310 commit ca64a81
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
value = "Formats a date object",
input = @JinjavaParam(
value = "value",
defaultValue = "current time",
desc = "The date variable or UNIX timestamp to format",
required = true
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.hubspot.jinjava.lib.filter;

import static com.hubspot.jinjava.lib.filter.time.DateTimeFormatHelper.FIXED_DATE_TIME_FILTER_NULL_ARG;

import com.hubspot.jinjava.doc.annotations.JinjavaDoc;
import com.hubspot.jinjava.doc.annotations.JinjavaParam;
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.features.DateTimeFeatureActivationStrategy;
import com.hubspot.jinjava.features.FeatureActivationStrategy;
import com.hubspot.jinjava.interpret.InvalidArgumentException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateError;
import com.hubspot.jinjava.lib.fn.Functions;

@JinjavaDoc(
value = "Gets the UNIX timestamp value (in milliseconds) of a date object",
input = @JinjavaParam(
value = "value",
defaultValue = "current time",
desc = "The date variable",
required = true
),
input = @JinjavaParam(value = "value", desc = "The date variable", required = true),
snippets = { @JinjavaSnippet(code = "{% mydatetime|unixtimestamp %}") }
)
public class UnixTimestampFilter implements Filter {
Expand All @@ -25,6 +26,27 @@ public String getName() {

@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
if (var == null) {
interpreter.addError(
TemplateError.fromMissingFilterArgException(
new InvalidArgumentException(
interpreter,
"unixtimestamp",
"unixtimestamp filter called with null datetime"
)
)
);

FeatureActivationStrategy feat = interpreter
.getConfig()
.getFeatures()
.getActivationStrategy(FIXED_DATE_TIME_FILTER_NULL_ARG);

if (feat.isActive(interpreter.getContext())) {
var = ((DateTimeFeatureActivationStrategy) feat).getActivateAt();
}
}

return Functions.unixtimestamp(var);
}
}
29 changes: 2 additions & 27 deletions src/main/java/com/hubspot/jinjava/lib/fn/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static ZonedDateTime today(String... var) {
@JinjavaDoc(
value = "formats a date to a string",
params = {
@JinjavaParam(value = "var", type = "date", defaultValue = "current time"),
@JinjavaParam(value = "var", type = "date", required = true),
@JinjavaParam(
value = "format",
defaultValue = StrftimeFormatter.DEFAULT_DATE_FORMAT
Expand Down Expand Up @@ -310,36 +310,11 @@ public static ZonedDateTime getDateTimeArg(Object var, ZoneId zoneOffset) {

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

if (filterVar == null) {
JinjavaInterpreter interpreter = JinjavaInterpreter.getCurrent();

interpreter.addError(
TemplateError.fromMissingFilterArgException(
new InvalidArgumentException(
interpreter,
"unixtimestamp",
"unixtimestamp filter called with null datetime"
)
)
);

FeatureActivationStrategy feat = interpreter
.getConfig()
.getFeatures()
.getActivationStrategy(FIXED_DATE_TIME_FILTER_NULL_ARG);

if (feat.isActive(interpreter.getContext())) {
filterVar = ((DateTimeFeatureActivationStrategy) feat).getActivateAt();
}
}

ZonedDateTime d = getDateTimeArg(filterVar, ZoneOffset.UTC);

if (d == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ public void tearDown() {

@Test
public void itGetsUnixTimestamps() {
JinjavaInterpreter.pushCurrent(
new JinjavaInterpreter(
new Jinjava(),
new Context(),
JinjavaConfig.newBuilder().build()
)
JinjavaInterpreter jinjavaInterpreter = new JinjavaInterpreter(
new Jinjava(),
new Context(),
JinjavaConfig.newBuilder().build()
);
JinjavaInterpreter.pushCurrent(jinjavaInterpreter);
assertThat(Functions.unixtimestamp())
.isGreaterThan(0)
.isLessThanOrEqualTo(System.currentTimeMillis());
assertThat(Functions.unixtimestamp(epochMilliseconds)).isEqualTo(epochMilliseconds);
assertThat(Functions.unixtimestamp(d)).isEqualTo(epochMilliseconds);
assertThat(Functions.unixtimestamp((Object) null))
.isCloseTo(System.currentTimeMillis(), Offset.offset(1000L));
assertThat(jinjavaInterpreter.getErrors()).isEmpty();
}

@Test
Expand Down

0 comments on commit ca64a81

Please sign in to comment.