From a87a23447fed17279453088b10d4b960bffd8e85 Mon Sep 17 00:00:00 2001 From: Alex Ksikes Date: Mon, 13 Oct 2014 16:23:15 +0200 Subject: [PATCH] ParseField: Support for when all fields are deprecated Closes #8067 --- .../org/elasticsearch/common/ParseField.java | 24 ++++++-- .../elasticsearch/common/ParseFieldTests.java | 56 ++++++++++++++----- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/ParseField.java b/src/main/java/org/elasticsearch/common/ParseField.java index c4221868a6a..a1d6f465a0b 100644 --- a/src/main/java/org/elasticsearch/common/ParseField.java +++ b/src/main/java/org/elasticsearch/common/ParseField.java @@ -29,6 +29,7 @@ public class ParseField { private final String camelCaseName; private final String underscoreName; private final String[] deprecatedNames; + private String allReplacedWith = null; public static final EnumSet EMPTY_FLAGS = EnumSet.noneOf(Flag.class); @@ -50,7 +51,7 @@ public class ParseField { this.deprecatedNames = set.toArray(new String[0]); } } - + public String getPreferredName(){ return underscoreName; } @@ -68,19 +69,33 @@ public class ParseField { public ParseField withDeprecation(String... deprecatedNames) { return new ParseField(this.underscoreName, deprecatedNames); } - + + /** + * Return a new ParseField where all field names are deprecated and replaced with {@code allReplacedWith}. + */ + public ParseField withAllDeprecated(String allReplacedWith) { + ParseField parseField = this.withDeprecation(getAllNamesIncludedDeprecated()); + parseField.allReplacedWith = allReplacedWith; + return parseField; + } + public boolean match(String currentFieldName) { return match(currentFieldName, EMPTY_FLAGS); } public boolean match(String currentFieldName, EnumSet flags) { - if (currentFieldName.equals(camelCaseName) || currentFieldName.equals(underscoreName)) { + if (allReplacedWith == null && (currentFieldName.equals(camelCaseName) || currentFieldName.equals(underscoreName))) { return true; } + String msg; for (String depName : deprecatedNames) { if (currentFieldName.equals(depName)) { if (flags.contains(Flag.STRICT)) { - throw new ElasticsearchIllegalArgumentException("Deprecated field [" + currentFieldName + "] used expected [" + underscoreName + "] instead"); + msg = "Deprecated field [" + currentFieldName + "] used, expected [" + underscoreName + "] instead"; + if (allReplacedWith != null) { + msg = "Deprecated field [" + currentFieldName + "] used, replaced by [" + allReplacedWith + "]"; + } + throw new ElasticsearchIllegalArgumentException(msg); } return true; } @@ -88,5 +103,4 @@ public class ParseField { return false; } - } diff --git a/src/test/java/org/elasticsearch/common/ParseFieldTests.java b/src/test/java/org/elasticsearch/common/ParseFieldTests.java index d1dca3a4824..ef4d2e093f6 100644 --- a/src/test/java/org/elasticsearch/common/ParseFieldTests.java +++ b/src/test/java/org/elasticsearch/common/ParseFieldTests.java @@ -18,8 +18,10 @@ */ package org.elasticsearch.common; +import org.apache.commons.lang3.ArrayUtils; import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.test.ElasticsearchTestCase; +import org.junit.Test; import java.util.EnumSet; @@ -27,22 +29,22 @@ import static org.hamcrest.CoreMatchers.*; public class ParseFieldTests extends ElasticsearchTestCase { + @Test public void testParse() { String[] values = new String[]{"foo_bar", "fooBar"}; ParseField field = new ParseField(randomFrom(values)); String[] deprecated = new String[]{"barFoo", "bar_foo"}; - ParseField withDepredcations = field.withDeprecation("Foobar", randomFrom(deprecated)); - assertThat(field, not(sameInstance(withDepredcations))); + ParseField withDeprecations = field.withDeprecation("Foobar", randomFrom(deprecated)); + assertThat(field, not(sameInstance(withDeprecations))); assertThat(field.match(randomFrom(values), ParseField.EMPTY_FLAGS), is(true)); assertThat(field.match("foo bar", ParseField.EMPTY_FLAGS), is(false)); assertThat(field.match(randomFrom(deprecated), ParseField.EMPTY_FLAGS), is(false)); assertThat(field.match("barFoo", ParseField.EMPTY_FLAGS), is(false)); - - assertThat(withDepredcations.match(randomFrom(values), ParseField.EMPTY_FLAGS), is(true)); - assertThat(withDepredcations.match("foo bar", ParseField.EMPTY_FLAGS), is(false)); - assertThat(withDepredcations.match(randomFrom(deprecated), ParseField.EMPTY_FLAGS), is(true)); - assertThat(withDepredcations.match("barFoo", ParseField.EMPTY_FLAGS), is(true)); + assertThat(withDeprecations.match(randomFrom(values), ParseField.EMPTY_FLAGS), is(true)); + assertThat(withDeprecations.match("foo bar", ParseField.EMPTY_FLAGS), is(false)); + assertThat(withDeprecations.match(randomFrom(deprecated), ParseField.EMPTY_FLAGS), is(true)); + assertThat(withDeprecations.match("barFoo", ParseField.EMPTY_FLAGS), is(true)); // now with strict mode EnumSet flags = EnumSet.of(ParseField.Flag.STRICT); @@ -51,24 +53,50 @@ public class ParseFieldTests extends ElasticsearchTestCase { assertThat(field.match(randomFrom(deprecated), flags), is(false)); assertThat(field.match("barFoo", flags), is(false)); - - assertThat(withDepredcations.match(randomFrom(values), flags), is(true)); - assertThat(withDepredcations.match("foo bar", flags), is(false)); + assertThat(withDeprecations.match(randomFrom(values), flags), is(true)); + assertThat(withDeprecations.match("foo bar", flags), is(false)); try { - withDepredcations.match(randomFrom(deprecated), flags); + withDeprecations.match(randomFrom(deprecated), flags); fail(); } catch (ElasticsearchIllegalArgumentException ex) { } try { - withDepredcations.match("barFoo", flags); + withDeprecations.match("barFoo", flags); fail(); } catch (ElasticsearchIllegalArgumentException ex) { } - - } + @Test + public void testAllDeprecated() { + String[] values = new String[]{"like_text", "likeText"}; + + boolean withDeprecatedNames = randomBoolean(); + String[] deprecated = new String[]{"text", "same_as_text"}; + String[] allValues = values; + if (withDeprecatedNames) { + allValues = ArrayUtils.addAll(values, deprecated); + } + + ParseField field = new ParseField(randomFrom(values)); + if (withDeprecatedNames) { + field = field.withDeprecation(deprecated); + } + field = field.withAllDeprecated("like"); + + // strict mode off + assertThat(field.match(randomFrom(allValues), ParseField.EMPTY_FLAGS), is(true)); + assertThat(field.match("not a field name", ParseField.EMPTY_FLAGS), is(false)); + + // now with strict mode + EnumSet flags = EnumSet.of(ParseField.Flag.STRICT); + try { + field.match(randomFrom(allValues), flags); + fail(); + } catch (ElasticsearchIllegalArgumentException ex) { + } + } }