Skip to content

Commit d74dc02

Browse files
joegallogeorgewallace
authored andcommitted
DateProcessor refactoring (elastic#124349)
1 parent 5982720 commit d74dc02

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

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

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

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

142-
ingestDocument.setFieldValue(targetField, formatter.format(dateTime));
143-
return ingestDocument;
133+
document.setFieldValue(targetField, formatter.format(dateTime));
134+
return document;
144135
}
145136

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

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

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

159162
String getField() {
@@ -182,39 +185,33 @@ public Factory(ScriptService scriptService) {
182185

183186
public DateProcessor create(
184187
Map<String, Processor.Factory> registry,
185-
String processorTag,
188+
String tag,
186189
String description,
187190
Map<String, Object> config,
188191
ProjectId projectId
189192
) throws Exception {
190-
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field");
191-
String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", DEFAULT_TARGET_FIELD);
192-
String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "timezone");
193+
String field = ConfigurationUtils.readStringProperty(TYPE, tag, config, "field");
194+
String targetField = ConfigurationUtils.readStringProperty(TYPE, tag, config, "target_field", DEFAULT_TARGET_FIELD);
195+
String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "timezone");
193196
TemplateScript.Factory compiledTimezoneTemplate = null;
194197
if (timezoneString != null) {
195-
compiledTimezoneTemplate = ConfigurationUtils.compileTemplate(
196-
TYPE,
197-
processorTag,
198-
"timezone",
199-
timezoneString,
200-
scriptService
201-
);
198+
compiledTimezoneTemplate = ConfigurationUtils.compileTemplate(TYPE, tag, "timezone", timezoneString, scriptService);
202199
}
203-
String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "locale");
200+
String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "locale");
204201
TemplateScript.Factory compiledLocaleTemplate = null;
205202
if (localeString != null) {
206-
compiledLocaleTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "locale", localeString, scriptService);
203+
compiledLocaleTemplate = ConfigurationUtils.compileTemplate(TYPE, tag, "locale", localeString, scriptService);
207204
}
208-
List<String> formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats");
209-
String outputFormat = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "output_format", DEFAULT_OUTPUT_FORMAT);
205+
List<String> formats = ConfigurationUtils.readList(TYPE, tag, config, "formats");
206+
String outputFormat = ConfigurationUtils.readStringProperty(TYPE, tag, config, "output_format", DEFAULT_OUTPUT_FORMAT);
210207
try {
211208
DateFormatter.forPattern(outputFormat);
212209
} catch (Exception e) {
213210
throw new IllegalArgumentException("invalid output format [" + outputFormat + "]", e);
214211
}
215212

216213
return new DateProcessor(
217-
processorTag,
214+
tag,
218215
description,
219216
compiledTimezoneTemplate,
220217
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, null);
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, null);
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)