use of oap.json.schema.validator.object.ObjectSchemaAST in project oap by oaplatform.
the class JsonDiff method diffArray.
private static void diffArray(String prefix, ArraySchemaAST schema, ArrayList<Line> result, Object to, Object from) {
if (!(to instanceof List) || !(from instanceof List))
throw new IllegalArgumentException(prefix + ": invalid json");
final List<?> toList = (List<?>) to;
final List<?> fromList = (List<?>) from;
final Collection<?> diffDel = subtract(fromList, toList);
final Collection<?> diffAdd = subtract(toList, fromList);
final SchemaAST items = schema.items;
if (items instanceof ObjectSchemaAST) {
final String idField = schema.idField.orElseThrow(() -> new IllegalArgumentException(prefix + ": schema: id field is required"));
List added = unique(toList, fromList, idField);
List removed = unique(fromList, toList, idField);
for (Object item : added) {
diffField(prefixWithIndex(prefix, getId(idField, item)), items, result, item, null);
}
for (Object item : removed) {
diffField(prefixWithIndex(prefix, getId(idField, item)), items, result, null, item);
}
for (Object fromItem : fromList) {
final Map fromItemMap = (Map) fromItem;
final Object id = fromItemMap.get(idField);
if (id == null)
throw new IllegalArgumentException(prefix + ": id field " + idField + ": not found");
toList.stream().filter(toItem -> {
final Map toItemMap = (Map) toItem;
final Object toId = toItemMap.get(idField);
return Objects.equals(toId, id);
}).findAny().ifPresent(toItem -> diff(prefixWithIndex(prefix, id), items, result, toItem, fromItemMap));
}
} else if (items instanceof ArraySchemaAST) {
throw new IllegalArgumentException(prefix + ": sub-array");
} else {
diffField(prefix, schema, result, diffAdd.isEmpty() ? null : diffAdd, diffDel.isEmpty() ? null : diffDel);
}
}
use of oap.json.schema.validator.object.ObjectSchemaAST in project oap by oaplatform.
the class SchemaPath method traverse.
public static Result traverse(SchemaAST root, String path) {
SchemaAST schemaAST = root;
val additionalProperties = new MutableObject<Boolean>(null);
final Supplier<Result> empty = () -> new Result(Optional.empty(), Optional.ofNullable(additionalProperties.getValue()));
for (val item : StringUtils.split(path, '.')) {
if (schemaAST instanceof ObjectSchemaAST) {
val objectSchemaAST = (ObjectSchemaAST) schemaAST;
schemaAST = objectSchemaAST.properties.get(item);
objectSchemaAST.additionalProperties.ifPresent(additionalProperties::setValue);
if (schemaAST == null)
return empty.get();
} else if (schemaAST instanceof ArraySchemaAST) {
if (!"items".equals(item))
return empty.get();
schemaAST = ((ArraySchemaAST) schemaAST).items;
} else {
return empty.get();
}
}
return new Result(Optional.of(schemaAST), Optional.ofNullable(additionalProperties.getValue()));
}
use of oap.json.schema.validator.object.ObjectSchemaAST 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);
}
}
}
use of oap.json.schema.validator.object.ObjectSchemaAST in project oap by oaplatform.
the class JsonDiff method diffObject.
private static void diffObject(String prefix, ObjectSchemaAST schema, ArrayList<Line> result, Object to, Object from) {
final Map toMap = (Map) to;
final Map fromMap = (Map) from;
for (Map.Entry<String, SchemaAST> child : schema.properties.entrySet()) {
final String property = child.getKey();
final Object fromProperty = fromMap.get(property);
final Object toProperty = toMap.get(property);
final String newPrefix = prefix.length() > 0 ? prefix + "." + property : property;
final SchemaAST schemaAST = child.getValue();
if ((fromProperty == null && toProperty != null) || (toProperty == null && fromProperty != null)) {
diffField(newPrefix, schemaAST, result, toProperty, fromProperty);
} else if (fromProperty != null) {
diff(newPrefix, schemaAST, result, toProperty, fromProperty);
}
}
}
Aggregations