Search in sources :

Example 1 with NumberSchemaAST

use of oap.json.schema.validator.number.NumberSchemaAST 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);
        }
    }
}
Also used : lombok.val(lombok.val) DefaultSchemaAST(oap.json.schema.DefaultSchemaAST) StringSchemaAST(oap.json.schema.validator.string.StringSchemaAST) NumberSchemaAST(oap.json.schema.validator.number.NumberSchemaAST) DictionarySchemaAST(oap.json.schema.validator.dictionary.DictionarySchemaAST) ObjectSchemaAST(oap.json.schema.validator.object.ObjectSchemaAST) ArraySchemaAST(oap.json.schema.validator.array.ArraySchemaAST)

Aggregations

lombok.val (lombok.val)1 DefaultSchemaAST (oap.json.schema.DefaultSchemaAST)1 ArraySchemaAST (oap.json.schema.validator.array.ArraySchemaAST)1 DictionarySchemaAST (oap.json.schema.validator.dictionary.DictionarySchemaAST)1 NumberSchemaAST (oap.json.schema.validator.number.NumberSchemaAST)1 ObjectSchemaAST (oap.json.schema.validator.object.ObjectSchemaAST)1 StringSchemaAST (oap.json.schema.validator.string.StringSchemaAST)1