Move dynamic template matching logic to the MatchType enum. #17281

This commit is contained in:
Adrien Grand 2016-03-23 14:44:20 +01:00
parent b139f4e0bf
commit 940fc3f864
1 changed files with 16 additions and 12 deletions

View File

@ -41,12 +41,20 @@ public class DynamicTemplate implements ToXContent {
public static enum MatchType {
SIMPLE {
@Override
public boolean matches(String pattern, String value) {
return Regex.simpleMatch(pattern, value);
}
@Override
public String toString() {
return "simple";
}
},
REGEX {
@Override
public boolean matches(String pattern, String value) {
return value.matches(pattern);
}
@Override
public String toString() {
return "regex";
@ -61,6 +69,9 @@ public class DynamicTemplate implements ToXContent {
}
throw new IllegalArgumentException("No matching pattern matched on [" + value + "]");
}
/** Whether {@code value} matches {@code regex}. */
public abstract boolean matches(String regex, String value);
}
public static DynamicTemplate parse(String name, Map<String, Object> conf,
@ -137,23 +148,23 @@ public class DynamicTemplate implements ToXContent {
}
public boolean match(ContentPath path, String name, String dynamicType) {
if (pathMatch != null && !patternMatch(pathMatch, path.pathAsText(name))) {
if (pathMatch != null && !matchType.matches(pathMatch, path.pathAsText(name))) {
return false;
}
if (match != null && !patternMatch(match, name)) {
if (match != null && !matchType.matches(match, name)) {
return false;
}
if (pathUnmatch != null && patternMatch(pathUnmatch, path.pathAsText(name))) {
if (pathUnmatch != null && matchType.matches(pathUnmatch, path.pathAsText(name))) {
return false;
}
if (unmatch != null && patternMatch(unmatch, name)) {
if (unmatch != null && matchType.matches(unmatch, name)) {
return false;
}
if (matchMappingType != null) {
if (dynamicType == null) {
return false;
}
if (!patternMatch(matchMappingType, dynamicType)) {
if (!matchType.matches(matchMappingType, dynamicType)) {
return false;
}
}
@ -186,13 +197,6 @@ public class DynamicTemplate implements ToXContent {
return type;
}
private boolean patternMatch(String pattern, String str) {
if (matchType == MatchType.SIMPLE) {
return Regex.simpleMatch(pattern, str);
}
return str.matches(pattern);
}
public Map<String, Object> mappingForName(String name, String dynamicType) {
return processMap(mapping, name, dynamicType);
}