Search in sources :

Example 1 with JsonEnumField

use of io.atlasmap.json.v2.JsonEnumField in project atlasmap by atlasmap.

the class JsonFieldReader method getJsonFieldsForPath.

private List<Field> getJsonFieldsForPath(AtlasInternalSession session, JsonNode node, Field field, AtlasPath path, int depth) throws AtlasException {
    List<Field> fields = new ArrayList<>();
    List<SegmentContext> segments = path.getSegments(true);
    if (segments.size() < depth) {
        throw new AtlasException(String.format("depth '%s' exceeds segment size '%s'", depth, segments.size()));
    }
    if (segments.size() == depth) {
        // if traversed the entire path and found value
        if (field.getFieldType() == FieldType.COMPLEX && !node.isValueNode()) {
            FieldGroup group = (FieldGroup) field;
            populateChildFields(session, node, group, path);
            fields.add(group);
        } else {
            JsonField jsonField = new JsonField();
            AtlasModelFactory.copyField(field, jsonField, true);
            if (field instanceof JsonEnumField && field.getFieldType() == FieldType.COMPLEX) {
                // enum has COMPLEX by default
                jsonField.setFieldType(FieldType.STRING);
            }
            Object value = handleValueNode(session, node, jsonField);
            jsonField.setValue(value);
            // reset index for subfields
            jsonField.setIndex(null);
            fields.add(jsonField);
        }
        return fields;
    }
    // segments.size() > depth
    SegmentContext segmentContext;
    JsonNode child;
    if (depth == 0 && path.hasCollectionRoot()) {
        // if root is a collection
        child = node;
        segmentContext = segments.get(depth);
    } else {
        if (depth == 0) {
            if (node.size() == 1 && !path.getSegments(false).get(0).getExpression().startsWith(rootNode.fieldNames().next())) {
                // peel off a rooted object, i.e. mapping /orderId works for document { source: { orderId: 123 } }
                node = node.elements().next();
            }
            if (segments.size() > 1) {
                // skip the root, if not a collection
                depth = 1;
            }
        }
        segmentContext = segments.get(depth);
        String fieldName = segmentContext.getName();
        child = node.get(fieldName);
    }
    if (child == null) {
        return fields;
    }
    if (segmentContext.getCollectionType() == CollectionType.NONE) {
        List<Field> childFields = getJsonFieldsForPath(session, child, field, path, depth + 1);
        fields.addAll(childFields);
        return fields;
    }
    // collection
    if (segmentContext.getCollectionIndex() != null) {
        if (child.size() <= segmentContext.getCollectionIndex()) {
            // index out of range
            return fields;
        }
        List<Field> arrayFields = getJsonFieldsForPath(session, child.get(segmentContext.getCollectionIndex()), field, path, depth + 1);
        fields.addAll(arrayFields);
    } else {
        // if index not included, iterate over all
        for (int i = 0; i < child.size(); i++) {
            Field itemField;
            if (field instanceof FieldGroup) {
                itemField = AtlasJsonModelFactory.cloneFieldGroup((FieldGroup) field);
                AtlasPath.setCollectionIndexRecursively((FieldGroup) itemField, depth, i);
            } else {
                itemField = AtlasJsonModelFactory.cloneField((JsonField) field, false);
                AtlasPath itemPath = new AtlasPath(field.getPath());
                itemPath.setCollectionIndex(depth, i);
                itemField.setPath(itemPath.toString());
            }
            List<Field> arrayFields = getJsonFieldsForPath(session, child.get(i), itemField, new AtlasPath(itemField.getPath()), depth + 1);
            fields.addAll(arrayFields);
        }
    }
    return fields;
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) AtlasException(io.atlasmap.api.AtlasException) JsonEnumField(io.atlasmap.json.v2.JsonEnumField) JsonEnumField(io.atlasmap.json.v2.JsonEnumField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JsonField(io.atlasmap.json.v2.JsonField) AtlasPath(io.atlasmap.core.AtlasPath)

Example 2 with JsonEnumField

use of io.atlasmap.json.v2.JsonEnumField in project atlasmap by atlasmap.

the class JsonSchemaInspector method getJsonFieldBuilder.

private JsonFieldBuilder getJsonFieldBuilder(String name, JsonNode value, String parentPath, Map<String, JsonNode> definitionMap, Set<String> definitionTrace, boolean isArray) throws JsonInspectionException {
    LOG.trace("--> Field:[name=[{}], value=[{}], parentPath=[{}]", name, value, parentPath);
    JsonFieldBuilder builder = new JsonFieldBuilder();
    if (name != null) {
        builder.name = name;
        builder.path = (parentPath != null && !parentPath.equals("/") ? parentPath.concat("/") : "/").concat(name);
    }
    if (isArray) {
        builder.path += "<>";
        builder.collectionType = CollectionType.LIST;
    }
    builder.status = FieldStatus.SUPPORTED;
    JsonNode nodeValue = value;
    populateDefinitions(nodeValue, definitionMap);
    if (isRecursive(nodeValue, definitionTrace)) {
        builder.type = FieldType.COMPLEX;
        builder.status = FieldStatus.CACHED;
        return builder;
    } else {
        definitionTrace = new HashSet<>(definitionTrace);
        nodeValue = resolveReference(nodeValue, definitionMap, definitionTrace);
    }
    JsonNode fieldEnum = nodeValue.get("enum");
    if (fieldEnum != null) {
        builder.type = FieldType.COMPLEX;
        if (fieldEnum.isArray()) {
            final JsonFieldBuilder finalBuilder = builder;
            ((ArrayNode) fieldEnum).forEach(item -> {
                JsonEnumField itemField = new JsonEnumField();
                itemField.setName(item.isNull() ? null : item.asText());
                finalBuilder.enumFields.getJsonEnumField().add(itemField);
            });
        } else if (!fieldEnum.isEmpty()) {
            JsonEnumField itemField = new JsonEnumField();
            itemField.setName(fieldEnum.isNull() ? null : fieldEnum.asText());
            builder.enumFields.getJsonEnumField().add(itemField);
        }
        return builder;
    }
    JsonNode fieldType = nodeValue.get("type");
    if (fieldType == null || fieldType.asText() == null) {
        LOG.warn("'type' is not defined for node '{}', assuming as an object", name);
        builder.type = FieldType.COMPLEX;
        builder.subFields.getJsonField().addAll(loadProperties(nodeValue, builder.path, definitionMap, definitionTrace));
        return builder;
    } else if ("array".equals(fieldType.asText())) {
        JsonNode arrayItems = nodeValue.get("items");
        if (arrayItems == null || !arrayItems.fields().hasNext()) {
            LOG.warn("'{}' is an array node, but no 'items' found in it. It will be ignored", name);
            builder.status = FieldStatus.UNSUPPORTED;
        } else {
            builder = getJsonFieldBuilder(name, arrayItems, parentPath, definitionMap, definitionTrace, true);
        }
        return builder;
    }
    List<String> jsonTypes = new LinkedList<>();
    if (fieldType instanceof ArrayNode) {
        ((ArrayNode) fieldType).spliterator().forEachRemaining(node -> jsonTypes.add(node.asText()));
    } else {
        jsonTypes.add(fieldType.asText());
    }
    processFieldType(builder, jsonTypes, nodeValue, definitionMap, definitionTrace);
    return builder;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonEnumField(io.atlasmap.json.v2.JsonEnumField) LinkedList(java.util.LinkedList)

Example 3 with JsonEnumField

use of io.atlasmap.json.v2.JsonEnumField in project atlasmap by atlasmap.

the class JsonSchemaInspectorTest method inspectJsonSchemaAddress.

@Test
public void inspectJsonSchemaAddress() throws Exception {
    final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/address.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("post-office-box", f.getName());
    assertEquals("/post-office-box", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("extended-address", f.getName());
    assertEquals("/extended-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(2);
    assertEquals("street-address", f.getName());
    assertEquals("/street-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(3);
    assertEquals("locality", f.getName());
    assertEquals("/locality", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(4);
    assertEquals("region", f.getName());
    assertEquals("/region", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    JsonComplexType c = (JsonComplexType) f;
    assertEquals(true, c.isEnumeration());
    List<String> regions = new ArrayList<>(Arrays.asList("NA", "EMEA", "LATAM", "APAC"));
    for (JsonEnumField e : c.getJsonEnumFields().getJsonEnumField()) {
        if (!regions.remove(e.getName())) {
            fail("Unknown enum value: " + e.getName());
        }
        ;
    }
    if (!regions.isEmpty()) {
        fail("Not found: " + regions);
    }
    f = (JsonField) fields.get(5);
    assertEquals("postal-code", f.getName());
    assertEquals("/postal-code", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(6);
    assertEquals("country-name", f.getName());
    assertEquals("/country-name", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
}
Also used : JsonEnumField(io.atlasmap.json.v2.JsonEnumField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) ArrayList(java.util.ArrayList) JsonEnumField(io.atlasmap.json.v2.JsonEnumField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.jupiter.api.Test)

Aggregations

JsonEnumField (io.atlasmap.json.v2.JsonEnumField)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonField (io.atlasmap.json.v2.JsonField)2 Field (io.atlasmap.v2.Field)2 ArrayList (java.util.ArrayList)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 AtlasException (io.atlasmap.api.AtlasException)1 AtlasPath (io.atlasmap.core.AtlasPath)1 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)1 JsonComplexType (io.atlasmap.json.v2.JsonComplexType)1 JsonDocument (io.atlasmap.json.v2.JsonDocument)1 FieldGroup (io.atlasmap.v2.FieldGroup)1 LinkedList (java.util.LinkedList)1 Test (org.junit.jupiter.api.Test)1