parent
437c5d0d3b
commit
a87a23447f
|
@ -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);
|
||||
|
||||
|
@ -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<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;
|
||||
}
|
||||
|
@ -88,5 +103,4 @@ public class ParseField {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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<ParseField.Flag> 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<ParseField.Flag> flags = EnumSet.of(ParseField.Flag.STRICT);
|
||||
try {
|
||||
field.match(randomFrom(allValues), flags);
|
||||
fail();
|
||||
} catch (ElasticsearchIllegalArgumentException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue