use of oap.json.schema.validator.dictionary.DictionarySchemaAST in project oap by oaplatform.
the class ElasticSearchSchema method convert.
private static void convert(SchemaAST<?> schemaAST, JsonGenerator jsonGenerator, boolean top) throws IOException {
if (schemaAST instanceof ObjectSchemaAST) {
val objectSchemaAST = (ObjectSchemaAST) schemaAST;
if (!objectSchemaAST.common.index.orElse(true))
jsonGenerator.writeBooleanField("enabled", false);
val nested = objectSchemaAST.nested.orElse(false);
if (!top || nested)
jsonGenerator.writeStringField("type", !nested ? "object" : "nested");
if (objectSchemaAST.dynamic.isPresent()) {
switch(objectSchemaAST.dynamic.orElse(Dynamic.TRUE)) {
case TRUE:
jsonGenerator.writeStringField("dynamic", "true");
break;
case FALSE:
jsonGenerator.writeStringField("dynamic", "false");
break;
case STRICT:
jsonGenerator.writeStringField("dynamic", "strict");
break;
}
}
jsonGenerator.writeObjectFieldStart("properties");
for (val entry : objectSchemaAST.properties.entrySet()) {
if (entry.getKey().equals("_id"))
continue;
jsonGenerator.writeObjectFieldStart(entry.getKey());
convert(entry.getValue(), jsonGenerator, false);
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndObject();
} else if (schemaAST instanceof ArraySchemaAST) {
convert(((ArraySchemaAST) schemaAST).items, jsonGenerator, false);
} else {
val schemaType = schemaAST.common.schemaType;
if (schemaAST instanceof DefaultSchemaAST) {
switch(schemaType) {
case "boolean":
jsonGenerator.writeStringField("type", "boolean");
break;
case "date":
jsonGenerator.writeStringField("type", "date");
break;
default:
throw new IllegalArgumentException("Unknown schema type " + schemaType);
}
convertCommon(schemaAST, jsonGenerator);
} else if (schemaAST instanceof StringSchemaAST) {
if (schemaType.equals("string")) {
jsonGenerator.writeStringField("type", "keyword");
} else {
jsonGenerator.writeStringField("type", "text");
}
convertCommon(schemaAST, jsonGenerator);
} else if (schemaAST instanceof NumberSchemaAST) {
switch(schemaType) {
case "integer":
case "long":
jsonGenerator.writeStringField("type", "long");
break;
case "double":
jsonGenerator.writeStringField("type", "double");
break;
default:
throw new IllegalArgumentException("Unknown schema type " + schemaType);
}
convertCommon(schemaAST, jsonGenerator);
} else if (schemaAST instanceof DictionarySchemaAST) {
jsonGenerator.writeStringField("type", "keyword");
convertCommon(schemaAST, jsonGenerator);
} else {
throw new IllegalArgumentException("Unknown schema type " + schemaType);
}
}
}
Aggregations