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;
}
}
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);
}
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;
}
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());
}
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;
}
Aggregations