Skip to content

Commit

Permalink
ParseField: Support for when all fields are deprecated
Browse files Browse the repository at this point in the history
Closes #8067
  • Loading branch information
alexksikes committed Oct 24, 2014
1 parent 16d10e1 commit 9965cbd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
24 changes: 19 additions & 5 deletions src/main/java/org/elasticsearch/common/ParseField.java
Expand Up @@ -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<Flag> EMPTY_FLAGS = EnumSet.noneOf(Flag.class);

Expand All @@ -50,7 +51,7 @@ public ParseField(String value, String... deprecatedNames) {
this.deprecatedNames = set.toArray(new String[0]);
}
}

public String getPreferredName(){
return underscoreName;
}
Expand All @@ -68,25 +69,38 @@ public String[] getAllNamesIncludedDeprecated() {
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<Flag> 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;
}
}
return false;
}


}
54 changes: 41 additions & 13 deletions src/test/java/org/elasticsearch/common/ParseFieldTests.java
Expand Up @@ -18,31 +18,33 @@
*/
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;

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<ParseField.Flag> flags = EnumSet.of(ParseField.Flag.STRICT);
Expand All @@ -51,24 +53,50 @@ public void testParse() {
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<ParseField.Flag> flags = EnumSet.of(ParseField.Flag.STRICT);
try {
field.match(randomFrom(allValues), flags);
fail();
} catch (ElasticsearchIllegalArgumentException ex) {
}
}
}

0 comments on commit 9965cbd

Please sign in to comment.