com.dimajix.shaded.everit.schema.loader.CombinedSchemaLoader Maven / Gradle / Ivy
package com.dimajix.shaded.everit.schema.loader;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import com.dimajix.shaded.everit.schema.CombinedSchema;
import com.dimajix.shaded.everit.schema.Schema;
/**
* @author erosb
*/
class CombinedSchemaLoader implements SchemaExtractor {
/**
* Alias for {@code Function, CombinedSchema.Builder>}.
*/
@FunctionalInterface
private interface CombinedSchemaProvider
extends Function, CombinedSchema.Builder> {
}
private static final Map COMB_SCHEMA_PROVIDERS = new HashMap<>(3);
static {
COMB_SCHEMA_PROVIDERS.put("allOf", CombinedSchema::allOf);
COMB_SCHEMA_PROVIDERS.put("anyOf", CombinedSchema::anyOf);
COMB_SCHEMA_PROVIDERS.put("oneOf", CombinedSchema::oneOf);
}
private final SchemaLoader defaultLoader;
public CombinedSchemaLoader(SchemaLoader defaultLoader) {
this.defaultLoader = requireNonNull(defaultLoader, "defaultLoader cannot be null");
}
@Override
public ExtractionResult extract(JsonObject schemaJson) {
Set presentKeys = COMB_SCHEMA_PROVIDERS.keySet().stream()
.filter(schemaJson::containsKey)
.collect(toSet());
Collection> extractedSchemas = presentKeys.stream().map(key -> loadCombinedSchemaForKeyword(schemaJson, key))
.collect(toList());
return new ExtractionResult(presentKeys, extractedSchemas);
}
private CombinedSchema.Builder loadCombinedSchemaForKeyword(JsonObject schemaJson, String key) {
Collection subschemas = new ArrayList<>();
schemaJson.require(key).requireArray()
.forEach((i, subschema) -> subschemas.add(defaultLoader.loadChild(subschema).build()));
return COMB_SCHEMA_PROVIDERS.get(key).apply(subschemas);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy