Search in sources :

Example 66 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class KafkaConnectFieldReader method read.

@Override
public Field read(AtlasInternalSession session) throws AtlasException {
    Field field = session.head().getSourceField();
    if (root == null) {
        AtlasUtil.addAudit(session, field, String.format("Cannot read a field '%s' of KafkaConnect document '%s', document is null", field.getPath(), field.getDocId()), AuditStatus.ERROR, null);
        return field;
    }
    AtlasPath path = new AtlasPath(field.getPath());
    List<Field> fields;
    if (path.getSegments(true).size() == 1) {
        if (field.getFieldType() == FieldType.COMPLEX) {
            FieldGroup group = (FieldGroup) field;
            if (path.isCollectionRoot()) {
                nestComplexCollection(session, (List<Object>) root, group, 0);
            } else {
                populateChildFields(session, (Struct) root, group);
            }
            return group;
        } else {
            fields = createValueFields(root, path.getRootSegment(), 0, (KafkaConnectField) field);
        }
    } else {
        fields = getFieldsForPath(session, root, field, path, 0);
    }
    if (path.hasCollection() && !path.isIndexedCollection()) {
        FieldGroup fieldGroup = AtlasModelFactory.createFieldGroupFrom(field, true);
        fieldGroup.getField().addAll(fields);
        session.head().setSourceField(fieldGroup);
        return fieldGroup;
    } else if (fields.size() == 1) {
        Field f = fields.get(0);
        session.head().setSourceField(f);
        return f;
    } else {
        return field;
    }
}
Also used : KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField)

Example 67 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class KafkaConnectFieldReader method populateChildFields.

private void populateChildFields(AtlasInternalSession session, Struct parent, FieldGroup fieldGroup) throws AtlasException {
    List<Field> newChildren = new ArrayList<>();
    for (Field child : fieldGroup.getField()) {
        AtlasPath childPath = new AtlasPath(child.getPath());
        Object childValue = parent.get(childPath.getLastSegment().getName());
        if (childPath.getLastSegment().getCollectionType() != CollectionType.NONE) {
            FieldGroup childGroup = populateCollectionItems(session, (List<Object>) childValue, child);
            newChildren.add(childGroup);
        } else {
            if (child instanceof FieldGroup) {
                populateChildFields(session, (Struct) childValue, (FieldGroup) child);
            } else {
                Object converted = conversionService.convertType(childValue, child.getFormat(), child.getFieldType(), null);
                child.setValue(converted);
            }
            newChildren.add(child);
        }
    }
    fieldGroup.getField().clear();
    fieldGroup.getField().addAll(newChildren);
}
Also used : KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) AtlasPath(io.atlasmap.core.AtlasPath)

Example 68 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class KafkaConnectFieldReader method getFieldsForPath.

private List<Field> getFieldsForPath(AtlasInternalSession session, Object parent, Field field, AtlasPath path, int depth) throws AtlasException {
    List<Field> fields = new ArrayList<>();
    List<SegmentContext> segments = path.getSegments(true);
    if (parent == null) {
        return fields;
    }
    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 instanceof FieldGroup && field.getFieldType() == FieldType.COMPLEX && (parent instanceof Struct)) {
            FieldGroup group = (FieldGroup) field;
            populateChildFields(session, (Struct) parent, group);
            fields.add(group);
        } else {
            field.setValue(parent);
            fields.add(field);
        }
        return fields;
    }
    // segments.size() > depth
    SegmentContext segmentContext = null;
    Object child = null;
    List<Object> collectionChild = null;
    if (depth == 0 && path.hasCollectionRoot()) {
        // if root is a collection
        collectionChild = (List<Object>) parent;
        segmentContext = segments.get(depth);
    } else {
        if (depth == 0) {
            // skip the root, if not a collection
            depth = 1;
        }
        segmentContext = segments.get(depth);
        String fieldName = segmentContext.getName();
        child = ((Struct) parent).get(fieldName);
        if (segmentContext.getCollectionType() != CollectionType.NONE) {
            collectionChild = (List<Object>) child;
        }
    }
    if (segmentContext.getCollectionType() == CollectionType.NONE) {
        List<Field> childFields = getFieldsForPath(session, child, field, path, depth + 1);
        fields.addAll(childFields);
        return fields;
    }
    // collection
    if (segmentContext.getCollectionIndex() != null) {
        if (collectionChild.size() <= segmentContext.getCollectionIndex()) {
            // index out of range
            return fields;
        }
        List<Field> arrayFields = getFieldsForPath(session, collectionChild.get(segmentContext.getCollectionIndex()), field, path, depth + 1);
        fields.addAll(arrayFields);
    } else {
        // if index not included, iterate over all
        for (int i = 0; i < collectionChild.size(); i++) {
            Field itemField;
            if (field instanceof FieldGroup) {
                itemField = AtlasKafkaConnectModelFactory.cloneFieldGroup((FieldGroup) field);
                AtlasPath.setCollectionIndexRecursively((FieldGroup) itemField, depth, i);
            } else {
                itemField = AtlasKafkaConnectModelFactory.cloneField((KafkaConnectField) field, false);
                AtlasPath itemPath = new AtlasPath(field.getPath());
                itemPath.setCollectionIndex(depth, i);
                itemField.setPath(itemPath.toString());
            }
            List<Field> arrayFields = getFieldsForPath(session, collectionChild.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) AtlasException(io.atlasmap.api.AtlasException) Struct(org.apache.kafka.connect.data.Struct) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField) Field(io.atlasmap.v2.Field) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasPath(io.atlasmap.core.AtlasPath) KafkaConnectField(io.atlasmap.kafkaconnect.v2.KafkaConnectField)

Example 69 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class XmlFieldReaderTest method testReadParentCollectionEmpty.

@Test
public void testReadParentCollectionEmpty() throws Exception {
    final Document document = getDocumentFromFile("src/test/resources/complex-repeated-empty.xml", false);
    reader.setDocument(document);
    FieldGroup orders = new FieldGroup();
    orders.setFieldType(FieldType.COMPLEX);
    orders.setDocId("xml");
    orders.setPath("/orders/order[]");
    orders.setCollectionType(CollectionType.ARRAY);
    FieldGroup address = new FieldGroup();
    address.setFieldType(FieldType.COMPLEX);
    address.setDocId("xml");
    address.setPath("/orders/order[]/address");
    orders.getField().add(address);
    XmlField addressLine1 = AtlasXmlModelFactory.createXmlField();
    addressLine1.setFieldType(FieldType.STRING);
    addressLine1.setDocId("xml");
    addressLine1.setPath("/orders/order[]/address/addressLine1");
    address.getField().add(addressLine1);
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(address);
    Field readField = reader.read(session);
    assertNotNull(readField);
    assertEquals(FieldGroup.class, readField.getClass());
    FieldGroup readGroup = FieldGroup.class.cast(readField);
    assertEquals(0, readGroup.getField().size());
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) FieldGroup(io.atlasmap.v2.FieldGroup) XmlField(io.atlasmap.xml.v2.XmlField) Document(org.w3c.dom.Document) Test(org.junit.jupiter.api.Test)

Example 70 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class AtlasXmlModelFactory method cloneFieldGroup.

/**
 * Clones the FieldGroup.
 * @param group FieldGroup
 * @return cloned
 */
public static FieldGroup cloneFieldGroup(FieldGroup group) {
    FieldGroup clone = AtlasModelFactory.copyFieldGroup(group);
    List<Field> newChildren = new ArrayList<>();
    for (Field child : group.getField()) {
        if (child instanceof FieldGroup) {
            newChildren.add(cloneFieldGroup((FieldGroup) child));
        } else {
            newChildren.add(cloneField((XmlField) child, true));
        }
    }
    clone.getField().addAll(newChildren);
    return clone;
}
Also used : Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList)

Aggregations

FieldGroup (io.atlasmap.v2.FieldGroup)110 Field (io.atlasmap.v2.Field)89 Test (org.junit.jupiter.api.Test)48 SimpleField (io.atlasmap.v2.SimpleField)32 AtlasPath (io.atlasmap.core.AtlasPath)28 ArrayList (java.util.ArrayList)24 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)17 CsvField (io.atlasmap.csv.v2.CsvField)16 AtlasException (io.atlasmap.api.AtlasException)15 Audits (io.atlasmap.v2.Audits)14 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)13 ConstantField (io.atlasmap.v2.ConstantField)13 Head (io.atlasmap.spi.AtlasInternalSession.Head)12 JsonField (io.atlasmap.json.v2.JsonField)11 Mapping (io.atlasmap.v2.Mapping)11 PropertyField (io.atlasmap.v2.PropertyField)11 JavaField (io.atlasmap.java.v2.JavaField)10 XmlField (io.atlasmap.xml.v2.XmlField)9 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)8 LinkedList (java.util.LinkedList)8