Skip to content

Commit 6c8f884

Browse files
authored
Merge pull request #232 from metafacture/230-addTransformationFunction
Add `timestamp()` Fix function.
2 parents 55610ac + 9144ac9 commit 6c8f884

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ hash("foo")
310310
# ["name", "value"] => {"name":"value"}
311311
```
312312

313+
#### `timestamp`
314+
315+
Creates (or replaces) a field with the current timestamp.
316+
317+
Options:
318+
319+
- `format`: Date and time pattern as in [java.text.SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) (Default: Unix timestamp).
320+
- `timezone`: Time zone as in [java.util.TimeZone](https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html) (Default: UTC).
321+
- `language`: Language tag as in [java.util.Locale](https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html) (Default: The locale of the host system).
322+
323+
```perl
324+
timestamp("<targetField>"[, format: "<formatPattern>"][, timezone: "<timezoneCode>"][, language: "<languageCode>"])
325+
```
326+
313327
#### `format`
314328

315329
Replaces the value with a formatted (`sprintf`-like) version.

metafix/src/main/java/org/metafacture/metafix/FixMethod.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.metafacture.metafix.api.FixFunction;
2020
import org.metafacture.metamorph.api.Maps;
21+
import org.metafacture.metamorph.functions.Timestamp;
2122
import org.metafacture.metamorph.maps.FileMap;
2223

2324
import java.io.File;
@@ -286,6 +287,19 @@ public void apply(final Metafix metafix, final Record record, final List<String>
286287
record.set(field, newValue);
287288
}
288289
},
290+
timestamp {
291+
@Override
292+
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
293+
final String field = params.get(0);
294+
final Timestamp timestamp = new Timestamp();
295+
296+
withOption(options, "format", timestamp::setFormat);
297+
withOption(options, "language", timestamp::setLanguage);
298+
withOption(options, "timezone", timestamp::setTimezone);
299+
300+
record.set(field, new Value(timestamp.process(null)));
301+
}
302+
},
289303
vacuum {
290304
@Override
291305
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {

metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Set;
27+
import java.util.function.Consumer;
2728
import java.util.stream.Stream;
2829

2930
@FunctionalInterface
3031
public interface FixFunction {
3132

3233
void apply(Metafix metafix, Record record, List<String> params, Map<String, String> options);
3334

35+
default void withOption(final Map<String, String> options, final String key, final Consumer<String> consumer) {
36+
if (options.containsKey(key)) {
37+
consumer.accept(options.get(key));
38+
}
39+
}
40+
3441
default boolean getBoolean(final Map<String, String> options, final String key) {
3542
return Boolean.parseBoolean(options.get(key));
3643
}

metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.metafacture.metafix;
1818

1919
import org.metafacture.framework.StreamReceiver;
20+
import org.metafacture.metamorph.api.MorphBuildException;
2021

2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.extension.ExtendWith;
@@ -2798,6 +2799,56 @@ public void shouldRenameArrayFieldWithAsterisk() {
27982799
);
27992800
}
28002801

2802+
private void shouldNotAddTimestamp(final String options, final String message) {
2803+
MetafixTestHelpers.assertProcessException(MorphBuildException.class, message, () -> shouldAddTimestamp(options, ""));
2804+
}
2805+
2806+
private void shouldAddTimestamp(final String options, final String pattern) {
2807+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2808+
"timestamp(test" + options + ")"
2809+
),
2810+
i -> {
2811+
i.startRecord("1");
2812+
i.endRecord();
2813+
},
2814+
o -> {
2815+
o.get().startRecord("1");
2816+
o.get().literal(ArgumentMatchers.eq("test"), ArgumentMatchers.matches(pattern));
2817+
o.get().endRecord();
2818+
}
2819+
);
2820+
}
2821+
2822+
@Test
2823+
public void shouldAddTimestamp() {
2824+
shouldAddTimestamp("", "\\d+");
2825+
}
2826+
2827+
@Test
2828+
public void shouldAddTimestampWithFormat() {
2829+
shouldAddTimestamp(", format: 'yyyy-MM-dd'", "\\d{4}-\\d{2}-\\d{2}");
2830+
}
2831+
2832+
@Test
2833+
public void shouldNotAddTimestampWithUnsupportedFormat() {
2834+
shouldNotAddTimestamp(", format: \"'\"", "The date/time format ''' is not supported. ");
2835+
}
2836+
2837+
@Test
2838+
public void shouldAddTimestampWithLanguage() {
2839+
shouldAddTimestamp(", format: 'yyyy-MM-dd G', language: 'pl'", "\\d{4}-\\d{2}-\\d{2} n\\.e\\.");
2840+
}
2841+
2842+
@Test
2843+
public void shouldNotAddTimestampWithUnsupportedLanguage() {
2844+
shouldNotAddTimestamp(", language: '--'", "Language '--' not supported.");
2845+
}
2846+
2847+
@Test
2848+
public void shouldAddTimestampWithTimezone() {
2849+
shouldAddTimestamp(", format: 'yyyy-MM-dd Z', timezone: 'UTC'", "\\d{4}-\\d{2}-\\d{2} \\+0000");
2850+
}
2851+
28012852
@Test
28022853
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/pull/170")
28032854
public void shouldNotSplitLiteralName() {

0 commit comments

Comments
 (0)