Consolidate locale parsing. (#26400)
Mappings and ingest have different locale parsing code.
This commit is contained in:
parent
06b7f9c78e
commit
34a6c7af26
|
@ -103,15 +103,4 @@ public class LocaleUtils {
|
|||
throw new IllegalArgumentException("Locales can have at most 3 parts but got " + parts.length + ": " + Arrays.asList(parts));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a {@link Locale} object
|
||||
*/
|
||||
public static boolean isValid(Locale locale) {
|
||||
try {
|
||||
return locale.getISO3Language() != null && locale.getISO3Country() != null;
|
||||
} catch (MissingResourceException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -111,7 +111,6 @@ public final class DateProcessor extends AbstractProcessor {
|
|||
|
||||
public static final class Factory implements Processor.Factory {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public DateProcessor create(Map<String, Processor.Factory> registry, String processorTag,
|
||||
Map<String, Object> config) throws Exception {
|
||||
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field");
|
||||
|
@ -119,16 +118,9 @@ public final class DateProcessor extends AbstractProcessor {
|
|||
String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "timezone");
|
||||
DateTimeZone timezone = timezoneString == null ? DateTimeZone.UTC : DateTimeZone.forID(timezoneString);
|
||||
String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "locale");
|
||||
Locale locale = Locale.ENGLISH;
|
||||
Locale locale = Locale.ROOT;
|
||||
if (localeString != null) {
|
||||
try {
|
||||
locale = (new Locale.Builder()).setLanguageTag(localeString).build();
|
||||
} catch (IllformedLocaleException e) {
|
||||
throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
|
||||
}
|
||||
if (!LocaleUtils.isValid(locale)) {
|
||||
throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
|
||||
}
|
||||
locale = LocaleUtils.parse(localeString);
|
||||
}
|
||||
List<String> formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats");
|
||||
return new DateProcessor(processorTag, timezone, locale, field, formats, targetField);
|
||||
|
|
|
@ -46,7 +46,7 @@ public class DateProcessorFactoryTests extends ESTestCase {
|
|||
assertThat(processor.getField(), equalTo(sourceField));
|
||||
assertThat(processor.getTargetField(), equalTo(DateProcessor.DEFAULT_TARGET_FIELD));
|
||||
assertThat(processor.getFormats(), equalTo(Collections.singletonList("dd/MM/yyyyy")));
|
||||
assertThat(processor.getLocale(), equalTo(Locale.ENGLISH));
|
||||
assertThat(processor.getLocale(), equalTo(Locale.ROOT));
|
||||
assertThat(processor.getTimezone(), equalTo(DateTimeZone.UTC));
|
||||
}
|
||||
|
||||
|
@ -95,7 +95,7 @@ public class DateProcessorFactoryTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testParseInvalidLocale() throws Exception {
|
||||
String[] locales = new String[] { "invalid_locale", "english", "xy", "en-XY" };
|
||||
String[] locales = new String[] { "invalid_locale", "english", "xy", "xy-US" };
|
||||
for (String locale : locales) {
|
||||
DateProcessor.Factory factory = new DateProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
|
@ -103,12 +103,22 @@ public class DateProcessorFactoryTests extends ESTestCase {
|
|||
config.put("field", sourceField);
|
||||
config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
|
||||
config.put("locale", locale);
|
||||
try {
|
||||
factory.create(null, null, config);
|
||||
fail("should fail with invalid locale");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("Invalid language tag specified: " + locale));
|
||||
}
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
|
||||
() -> factory.create(null, null, config));
|
||||
assertThat(e.getMessage(), equalTo("Unknown language: " + locale.split("[_-]")[0]));
|
||||
}
|
||||
|
||||
locales = new String[] { "en-XY", "en-Canada" };
|
||||
for (String locale : locales) {
|
||||
DateProcessor.Factory factory = new DateProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
String sourceField = randomAlphaOfLengthBetween(1, 10);
|
||||
config.put("field", sourceField);
|
||||
config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
|
||||
config.put("locale", locale);
|
||||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
|
||||
() -> factory.create(null, null, config));
|
||||
assertThat(e.getMessage(), equalTo("Unknown country: " + locale.split("[_-]")[1]));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue