Skip to content

Commit bef8e3b

Browse files
authored
DateProcessor refactoring (elastic#124349) (elastic#124409)
1 parent 0e0fdf5 commit bef8e3b

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,9 @@ public final class DateProcessor extends AbstractProcessor {
9393
formatter = DateFormatter.forPattern(this.outputFormat);
9494
}
9595

96-
private static ZoneId newDateTimeZone(String timezone) {
97-
return timezone == null ? ZoneOffset.UTC : ZoneId.of(timezone);
98-
}
99-
100-
private static Locale newLocale(String locale) {
101-
return locale == null ? Locale.ENGLISH : LocaleUtils.parse(locale);
102-
}
103-
10496
@Override
105-
public IngestDocument execute(IngestDocument ingestDocument) {
106-
Object obj = ingestDocument.getFieldValue(field, Object.class);
97+
public IngestDocument execute(IngestDocument document) {
98+
Object obj = document.getFieldValue(field, Object.class);
10799
String value = null;
108100
if (obj != null) {
109101
// Don't use Objects.toString(...) here, because null gets changed to "null" which may confuse some date parsers
@@ -114,10 +106,9 @@ public IngestDocument execute(IngestDocument ingestDocument) {
114106
// extract the timezone and locale to use for date parsing
115107
final ZoneId documentTimezone;
116108
final Locale documentLocale;
117-
final Map<String, Object> sourceAndMetadata = ingestDocument.getSourceAndMetadata();
118109
try {
119-
documentTimezone = newDateTimeZone(timezone == null ? null : timezone.newInstance(sourceAndMetadata).execute());
120-
documentLocale = newLocale(locale == null ? null : locale.newInstance(sourceAndMetadata).execute());
110+
documentTimezone = getTimezone(document);
111+
documentLocale = getLocale(document);
121112
} catch (Exception e) {
122113
throw new IllegalArgumentException("unable to parse date [" + value + "]", e);
123114
}
@@ -138,21 +129,33 @@ public IngestDocument execute(IngestDocument ingestDocument) {
138129
throw new IllegalArgumentException("unable to parse date [" + value + "]", lastException);
139130
}
140131

141-
ingestDocument.setFieldValue(targetField, formatter.format(dateTime));
142-
return ingestDocument;
132+
document.setFieldValue(targetField, formatter.format(dateTime));
133+
return document;
143134
}
144135

145136
@Override
146137
public String getType() {
147138
return TYPE;
148139
}
149140

150-
TemplateScript.Factory getTimezone() {
151-
return timezone;
141+
// visible for testing
142+
ZoneId getTimezone(IngestDocument document) {
143+
String value = timezone == null ? null : document.renderTemplate(timezone);
144+
if (value == null) {
145+
return ZoneOffset.UTC;
146+
} else {
147+
return ZoneId.of(value);
148+
}
152149
}
153150

154-
TemplateScript.Factory getLocale() {
155-
return locale;
151+
// visible for testing
152+
Locale getLocale(IngestDocument document) {
153+
String value = locale == null ? null : document.renderTemplate(locale);
154+
if (value == null) {
155+
return Locale.ENGLISH;
156+
} else {
157+
return LocaleUtils.parse(value);
158+
}
156159
}
157160

158161
String getField() {
@@ -179,40 +182,30 @@ public Factory(ScriptService scriptService) {
179182
this.scriptService = scriptService;
180183
}
181184

182-
public DateProcessor create(
183-
Map<String, Processor.Factory> registry,
184-
String processorTag,
185-
String description,
186-
Map<String, Object> config
187-
) throws Exception {
188-
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field");
189-
String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", DEFAULT_TARGET_FIELD);
190-
String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "timezone");
185+
public DateProcessor create(Map<String, Processor.Factory> registry, String tag, String description, Map<String, Object> config)
186+
throws Exception {
187+
String field = ConfigurationUtils.readStringProperty(TYPE, tag, config, "field");
188+
String targetField = ConfigurationUtils.readStringProperty(TYPE, tag, config, "target_field", DEFAULT_TARGET_FIELD);
189+
String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "timezone");
191190
TemplateScript.Factory compiledTimezoneTemplate = null;
192191
if (timezoneString != null) {
193-
compiledTimezoneTemplate = ConfigurationUtils.compileTemplate(
194-
TYPE,
195-
processorTag,
196-
"timezone",
197-
timezoneString,
198-
scriptService
199-
);
192+
compiledTimezoneTemplate = ConfigurationUtils.compileTemplate(TYPE, tag, "timezone", timezoneString, scriptService);
200193
}
201-
String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "locale");
194+
String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "locale");
202195
TemplateScript.Factory compiledLocaleTemplate = null;
203196
if (localeString != null) {
204-
compiledLocaleTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "locale", localeString, scriptService);
197+
compiledLocaleTemplate = ConfigurationUtils.compileTemplate(TYPE, tag, "locale", localeString, scriptService);
205198
}
206-
List<String> formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats");
207-
String outputFormat = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "output_format", DEFAULT_OUTPUT_FORMAT);
199+
List<String> formats = ConfigurationUtils.readList(TYPE, tag, config, "formats");
200+
String outputFormat = ConfigurationUtils.readStringProperty(TYPE, tag, config, "output_format", DEFAULT_OUTPUT_FORMAT);
208201
try {
209202
DateFormatter.forPattern(outputFormat);
210203
} catch (Exception e) {
211204
throw new IllegalArgumentException("invalid output format [" + outputFormat + "]", e);
212205
}
213206

214207
return new DateProcessor(
215-
processorTag,
208+
tag,
216209
description,
217210
compiledTimezoneTemplate,
218211
compiledLocaleTemplate,

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorFactoryTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
package org.elasticsearch.ingest.common;
1111

1212
import org.elasticsearch.ElasticsearchParseException;
13+
import org.elasticsearch.ingest.IngestDocument;
14+
import org.elasticsearch.ingest.RandomDocumentPicks;
1315
import org.elasticsearch.ingest.TestTemplateService;
1416
import org.elasticsearch.test.ESTestCase;
1517
import org.junit.Before;
1618

1719
import java.time.ZoneId;
20+
import java.time.ZoneOffset;
1821
import java.util.HashMap;
1922
import java.util.List;
2023
import java.util.Locale;
@@ -43,8 +46,8 @@ public void testBuildDefaults() throws Exception {
4346
assertThat(processor.getField(), equalTo(sourceField));
4447
assertThat(processor.getTargetField(), equalTo(DateProcessor.DEFAULT_TARGET_FIELD));
4548
assertThat(processor.getFormats(), equalTo(List.of("dd/MM/yyyyy")));
46-
assertNull(processor.getLocale());
47-
assertNull(processor.getTimezone());
49+
assertThat(processor.getTimezone(null), equalTo(ZoneOffset.UTC));
50+
assertThat(processor.getLocale(null), equalTo(Locale.ENGLISH));
4851
}
4952

5053
public void testMatchFieldIsMandatory() throws Exception {
@@ -81,11 +84,12 @@ public void testParseLocale() throws Exception {
8184
String sourceField = randomAlphaOfLengthBetween(1, 10);
8285
config.put("field", sourceField);
8386
config.put("formats", List.of("dd/MM/yyyyy"));
84-
Locale locale = randomFrom(Locale.GERMANY, Locale.FRENCH, Locale.ROOT);
87+
Locale locale = randomFrom(Locale.GERMANY, Locale.FRENCH, Locale.CANADA);
8588
config.put("locale", locale.toLanguageTag());
8689

8790
DateProcessor processor = factory.create(null, null, null, config);
88-
assertThat(processor.getLocale().newInstance(Map.of()).execute(), equalTo(locale.toLanguageTag()));
91+
IngestDocument document = RandomDocumentPicks.randomIngestDocument(random(), Map.of());
92+
assertThat(processor.getLocale(document), equalTo(locale));
8993
}
9094

9195
public void testParseTimezone() throws Exception {
@@ -97,7 +101,8 @@ public void testParseTimezone() throws Exception {
97101
ZoneId timezone = randomZone();
98102
config.put("timezone", timezone.getId());
99103
DateProcessor processor = factory.create(null, null, null, config);
100-
assertThat(processor.getTimezone().newInstance(Map.of()).execute(), equalTo(timezone.getId()));
104+
IngestDocument document = RandomDocumentPicks.randomIngestDocument(random(), Map.of());
105+
assertThat(processor.getTimezone(document), equalTo(timezone));
101106
}
102107

103108
public void testParseMatchFormats() throws Exception {

0 commit comments

Comments
 (0)