Skip to content

Commit 360054a

Browse files
committed
Refactor subfield type
1 parent 1084c7b commit 360054a

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

test/framework/src/main/java/org/elasticsearch/datageneration/datasource/DefaultMappingParametersHandler.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.ZoneOffset;
2323
import java.time.format.DateTimeFormatter;
2424
import java.util.HashMap;
25+
import java.util.List;
2526
import java.util.Locale;
2627
import java.util.Map;
2728
import java.util.function.Supplier;
@@ -260,29 +261,7 @@ private Supplier<Map<String, Object>> wildcardMapping(boolean hasParent, DataSou
260261
};
261262
}
262263

263-
private Map<String, Object> stringSubField(FieldType parent, DataSourceRequest.LeafMappingParametersGenerator request) {
264-
var subFields = new HashMap<FieldType, Supplier<Map<String, Object>>>();
265-
subFields.put(FieldType.KEYWORD, () -> {
266-
var mapping = keywordMapping(true, request).get();
267-
mapping.remove("copy_to");
268-
return mapping;
269-
});
270-
subFields.put(FieldType.TEXT, () -> textMapping(true, request).get());
271-
subFields.put(FieldType.MATCH_ONLY_TEXT, () -> matchOnlyTextMapping(true, request).get());
272-
subFields.put(FieldType.WILDCARD, () -> wildcardMapping(true, request).get());
273-
274-
var options = subFields.entrySet().stream().filter(e -> e.getKey().equals(parent) == false).toList();
275-
var child = ESTestCase.randomFrom(options);
276-
FieldType childType = child.getKey();
277-
var childValue = child.getValue().get();
278-
childValue.put("type", childType.toString());
279-
return Map.of("subfield_" + childType, childValue);
280-
}
281-
282-
private Supplier<Map<String, Object>> matchOnlyTextMapping(
283-
boolean hasParent,
284-
DataSourceRequest.LeafMappingParametersGenerator request
285-
) {
264+
private Supplier<Map<String, Object>> matchOnlyTextMapping(boolean hasParent, DataSourceRequest.LeafMappingParametersGenerator request) {
286265
return () -> {
287266
var mapping = new HashMap<String, Object>();
288267
if (hasParent == false && ESTestCase.randomDouble() <= 0.2) {
@@ -292,6 +271,26 @@ private Supplier<Map<String, Object>> matchOnlyTextMapping(
292271
};
293272
}
294273

274+
private Map<String, Object> stringSubField(FieldType parent, DataSourceRequest.LeafMappingParametersGenerator request) {
275+
276+
List<FieldType> stringTypes = List.of(FieldType.TEXT, FieldType.MATCH_ONLY_TEXT, FieldType.KEYWORD, FieldType.WILDCARD);
277+
var childType = ESTestCase.randomValueOtherThan(parent, () -> ESTestCase.randomFrom(stringTypes));
278+
var child = switch (childType) {
279+
case TEXT -> textMapping(true, request).get();
280+
case MATCH_ONLY_TEXT -> matchOnlyTextMapping(true, request).get();
281+
case WILDCARD -> wildcardMapping(true, request).get();
282+
case KEYWORD -> {
283+
var mapping = keywordMapping(true, request).get();
284+
mapping.remove("copy_to");
285+
yield mapping;
286+
}
287+
default -> throw new AssertionError("unreachable");
288+
};
289+
290+
child.put("type", childType.toString());
291+
return Map.of("subfield_" + childType, child);
292+
}
293+
295294
public static HashMap<String, Object> commonMappingParameters() {
296295
var map = new HashMap<String, Object>();
297296
map.put("store", ESTestCase.randomBoolean());

0 commit comments

Comments
 (0)