-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function and filter to convert string to date (#608)
* Add strtodate function and filter * Register function, add tests * Add jinjava doc * prettier * convert to string Co-authored-by: Liam Harwood <[email protected]>
- Loading branch information
1 parent
0578ea9
commit 7cc0e6b
Showing
5 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/hubspot/jinjava/lib/filter/StringToDateFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.hubspot.jinjava.lib.filter; | ||
|
||
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.interpret.JinjavaInterpreter; | ||
import com.hubspot.jinjava.interpret.TemplateSyntaxException; | ||
import com.hubspot.jinjava.lib.fn.Functions; | ||
|
||
@JinjavaDoc( | ||
value = "Converts a date string and date format to a date object", | ||
input = @JinjavaParam(value = "dateString", desc = "Date string", required = true), | ||
params = { | ||
@JinjavaParam( | ||
value = "dateFormat", | ||
desc = "Format of the date string", | ||
required = true | ||
) | ||
}, | ||
snippets = { @JinjavaSnippet(code = "{{ '3/3/21'|strtodate('M/d/yy') }}") } | ||
) | ||
public class StringToDateFilter implements Filter { | ||
|
||
@Override | ||
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) { | ||
if (args.length < 1) { | ||
throw new TemplateSyntaxException( | ||
interpreter, | ||
getName(), | ||
"requires 1 argument (date format string)" | ||
); | ||
} | ||
|
||
if (var == null) { | ||
return null; | ||
} | ||
|
||
if (!(var instanceof String)) { | ||
var = String.valueOf(var); | ||
} | ||
|
||
return Functions.stringToDate((String) var, args[0]); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "strtodate"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/com/hubspot/jinjava/lib/fn/StringToDateFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.hubspot.jinjava.lib.fn; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import com.hubspot.jinjava.interpret.InterpretException; | ||
import com.hubspot.jinjava.objects.date.PyishDate; | ||
import java.time.LocalDate; | ||
import java.time.ZoneOffset; | ||
import org.junit.Test; | ||
|
||
public class StringToDateFunctionTest { | ||
|
||
@Test | ||
public void itConvertsStringToDate() { | ||
String dateString = "3/4/21"; | ||
String format = "M/d/yy"; | ||
PyishDate expected = new PyishDate( | ||
LocalDate.of(2021, 3, 4).atTime(0, 0).toInstant(ZoneOffset.UTC) | ||
); | ||
assertThat(Functions.stringToDate(dateString, format)).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
public void itFailsOnInvalidFormat() { | ||
String dateString = "3/4/21"; | ||
String format = "blah blah"; | ||
|
||
assertThatThrownBy(() -> Functions.stringToDate(dateString, format)) | ||
.isInstanceOf(InterpretException.class); | ||
} | ||
|
||
@Test | ||
public void itFailsOnTimeFormatMismatch() { | ||
String dateString = "3/4/21"; | ||
String format = "M/d/yyyy"; | ||
|
||
assertThatThrownBy(() -> Functions.stringToDate(dateString, format)) | ||
.isInstanceOf(InterpretException.class); | ||
} | ||
|
||
@Test | ||
public void itFailsOnNullDatetimeFormat() { | ||
String dateString = "3/4/21"; | ||
String format = null; | ||
|
||
assertThatThrownBy(() -> Functions.stringToDate(dateString, format)) | ||
.isInstanceOf(InterpretException.class); | ||
} | ||
} |