Wrap up building parametrized TypeParsers (#59977)

The TypeParser implementations of all ParametrizedFieldMapper descendant classes are
essentially the same - stateless, requiring the construction of a Builder object, and calling
parse on it before returning it. We can make this easier (and less error-prone) to
implement by wrapping the logic up into a final class, which takes a function to produce
the Builder from a name and parser context.
This commit is contained in:
Alan Woodward 2020-07-21 15:44:47 +01:00 committed by Alan Woodward
parent 4d646ca819
commit a0ad1a196b
7 changed files with 41 additions and 51 deletions

View File

@ -82,15 +82,7 @@ public class BinaryFieldMapper extends ParametrizedFieldMapper {
}
}
public static class TypeParser implements Mapper.TypeParser {
@Override
public BinaryFieldMapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext)
throws MapperParsingException {
BinaryFieldMapper.Builder builder = new BinaryFieldMapper.Builder(name);
builder.parse(name, parserContext, node);
return builder;
}
}
public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n));
public static final class BinaryFieldType extends MappedFieldType {

View File

@ -102,15 +102,7 @@ public class BooleanFieldMapper extends ParametrizedFieldMapper {
}
}
public static class TypeParser implements Mapper.TypeParser {
@Override
public BooleanFieldMapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext)
throws MapperParsingException {
BooleanFieldMapper.Builder builder = new BooleanFieldMapper.Builder(name);
builder.parse(name, parserContext, node);
return builder;
}
}
public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n));
public static final class BooleanFieldType extends TermBasedFieldType {

View File

@ -220,17 +220,7 @@ public class CompletionFieldMapper extends ParametrizedFieldMapper {
public static final Set<String> ALLOWED_CONTENT_FIELD_NAMES = Sets.newHashSet(Fields.CONTENT_FIELD_NAME_INPUT,
Fields.CONTENT_FIELD_NAME_WEIGHT, Fields.CONTENT_FIELD_NAME_CONTEXTS);
public static class TypeParser implements Mapper.TypeParser {
@Override
public Mapper.Builder<?> parse(String name, Map<String, Object> node, ParserContext parserContext)
throws MapperParsingException {
CompletionFieldMapper.Builder builder
= new CompletionFieldMapper.Builder(name, parserContext.getIndexAnalyzers().get("simple"));
builder.parse(name, parserContext, node);
return builder;
}
}
public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, c.getIndexAnalyzers().get("simple")));
public static final class CompletionFieldType extends TermBasedFieldType {

View File

@ -227,23 +227,15 @@ public final class DateFieldMapper extends ParametrizedFieldMapper {
}
}
public static class TypeParser implements Mapper.TypeParser {
public static final TypeParser MILLIS_PARSER = new TypeParser((n, c) -> {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
return new Builder(n, c.indexVersionCreated(), Resolution.MILLISECONDS, c.getDateFormatter(), ignoreMalformedByDefault);
});
private final Resolution resolution;
public TypeParser(Resolution resolution) {
this.resolution = resolution;
}
@Override
public Mapper.Builder<?> parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(parserContext.getSettings());
Builder builder = new Builder(name, parserContext.indexVersionCreated(), resolution,
parserContext.getDateFormatter(), ignoreMalformedByDefault);
builder.parse(name, parserContext, node);
return builder;
}
}
public static final TypeParser NANOS_PARSER = new TypeParser((n, c) -> {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
return new Builder(n, c.indexVersionCreated(), Resolution.NANOSECONDS, c.getDateFormatter(), ignoreMalformedByDefault);
});
public static final class DateFieldType extends MappedFieldType {
protected final DateFormatter dateTimeFormatter;

View File

@ -41,6 +41,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@ -504,4 +505,27 @@ public abstract class ParametrizedFieldMapper extends FieldMapper {
return DEPRECATED_PARAMS.contains(propName);
}
}
/**
* TypeParser implementation that automatically handles parsing
*/
public static final class TypeParser implements Mapper.TypeParser {
private final BiFunction<String, ParserContext, Builder> builderFunction;
/**
* Creates a new TypeParser
* @param builderFunction a function that produces a Builder from a name and parsercontext
*/
public TypeParser(BiFunction<String, ParserContext, Builder> builderFunction) {
this.builderFunction = builderFunction;
}
@Override
public Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
Builder builder = builderFunction.apply(name, parserContext);
builder.parse(name, parserContext, node);
return builder;
}
}
}

View File

@ -119,18 +119,18 @@ public class IndicesModule extends AbstractModule {
for (RangeType type : RangeType.values()) {
mappers.put(type.typeName(), new RangeFieldMapper.TypeParser(type));
}
mappers.put(BooleanFieldMapper.CONTENT_TYPE, new BooleanFieldMapper.TypeParser());
mappers.put(BinaryFieldMapper.CONTENT_TYPE, new BinaryFieldMapper.TypeParser());
mappers.put(BooleanFieldMapper.CONTENT_TYPE, BooleanFieldMapper.PARSER);
mappers.put(BinaryFieldMapper.CONTENT_TYPE, BinaryFieldMapper.PARSER);
DateFieldMapper.Resolution milliseconds = DateFieldMapper.Resolution.MILLISECONDS;
mappers.put(milliseconds.type(), new DateFieldMapper.TypeParser(milliseconds));
mappers.put(milliseconds.type(), DateFieldMapper.MILLIS_PARSER);
DateFieldMapper.Resolution nanoseconds = DateFieldMapper.Resolution.NANOSECONDS;
mappers.put(nanoseconds.type(), new DateFieldMapper.TypeParser(nanoseconds));
mappers.put(nanoseconds.type(), DateFieldMapper.NANOS_PARSER);
mappers.put(IpFieldMapper.CONTENT_TYPE, new IpFieldMapper.TypeParser());
mappers.put(TextFieldMapper.CONTENT_TYPE, new TextFieldMapper.TypeParser());
mappers.put(KeywordFieldMapper.CONTENT_TYPE, new KeywordFieldMapper.TypeParser());
mappers.put(ObjectMapper.CONTENT_TYPE, new ObjectMapper.TypeParser());
mappers.put(ObjectMapper.NESTED_CONTENT_TYPE, new ObjectMapper.TypeParser());
mappers.put(CompletionFieldMapper.CONTENT_TYPE, new CompletionFieldMapper.TypeParser());
mappers.put(CompletionFieldMapper.CONTENT_TYPE, CompletionFieldMapper.PARSER);
mappers.put(FieldAliasMapper.CONTENT_TYPE, new FieldAliasMapper.TypeParser());
mappers.put(GeoPointFieldMapper.CONTENT_TYPE, new GeoPointFieldMapper.TypeParser());

View File

@ -198,7 +198,7 @@ public class ParametrizedMapperTests extends ESSingleNodeTestCase {
return new KeywordFieldMapper.TypeParser();
}
if (Objects.equals("binary", s)) {
return new BinaryFieldMapper.TypeParser();
return BinaryFieldMapper.PARSER;
}
return null;
}, version, () -> null, null);