Skip to content

Commit

Permalink
Wrap up building parametrized TypeParsers (#59977)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
romseygeek authored Jul 21, 2020
1 parent b8e3ba7 commit 4ef3342
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,7 @@ public BinaryFieldMapper build(BuilderContext context) {
}
}

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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,7 @@ public BooleanFieldMapper build(BuilderContext context) {
}
}

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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,7 @@ private void checkCompletionContextsLimit(BuilderContext context) {
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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,15 @@ public DateFieldMapper build(BuilderContext context) {
}
}

public static class TypeParser implements Mapper.TypeParser {

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, resolution, parserContext.getDateFormatter(), ignoreMalformedByDefault);
builder.parse(name, parserContext, node);
return builder;
}
}
public static final TypeParser MILLIS_PARSER = new TypeParser((n, c) -> {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
return new Builder(n, Resolution.MILLISECONDS, c.getDateFormatter(), ignoreMalformedByDefault);
});

public static final TypeParser NANOS_PARSER = new TypeParser((n, c) -> {
boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING.get(c.getSettings());
return new Builder(n, Resolution.NANOSECONDS, c.getDateFormatter(), ignoreMalformedByDefault);
});

public static final class DateFieldType extends MappedFieldType {
protected final DateFormatter dateTimeFormatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;
Expand Down Expand Up @@ -503,4 +504,27 @@ private static boolean isDeprecatedParameter(String propName, Version indexCreat
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ public static Map<String, Mapper.TypeParser> getMappers(List<MapperPlugin> mappe
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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private static TestMapper fromMapping(String mapping, Version version) {
return new KeywordFieldMapper.TypeParser();
}
if (Objects.equals("binary", s)) {
return new BinaryFieldMapper.TypeParser();
return BinaryFieldMapper.PARSER;
}
return null;
}, version, () -> null, null);
Expand Down

0 comments on commit 4ef3342

Please sign in to comment.