Search in sources :

Example 36 with FieldGroup

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

the class DefaultAtlasValidationService method validateMapMapping.

private void validateMapMapping(List<Mapping> fieldMappings, List<Validation> validations, Set<String> usedIds) {
    for (Mapping fieldMapping : fieldMappings) {
        String mappingId = fieldMapping.getId();
        FieldGroup sourceFieldGroup = fieldMapping.getInputFieldGroup();
        List<Field> sourceFields = sourceFieldGroup != null ? sourceFieldGroup.getField() : fieldMapping.getInputField();
        validateMappingId(mappingId, usedIds, validations);
        Validators.MAP_INPUT_NOT_NULL.get().validate(sourceFields, validations, mappingId);
        if (fieldMapping.getInputField() != null) {
            Validators.MAP_INPUT_FIELD_NOT_EMPTY.get().validate(sourceFields, validations, mappingId);
        }
        Validators.MAP_OUTPUT_NOT_NULL.get().validate(fieldMapping.getOutputField(), validations, mappingId, ValidationStatus.WARN);
        if (fieldMapping.getOutputField() != null) {
            Validators.MAP_OUTPUT_FIELD_NOT_EMPTY.get().validate(fieldMapping.getOutputField(), validations, mappingId, ValidationStatus.WARN);
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping)

Example 37 with FieldGroup

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

the class JsonModule method writeTargetValue.

@Override
public void writeTargetValue(AtlasInternalSession session) throws AtlasException {
    JsonFieldWriter writer = session.getFieldWriter(getDocId(), JsonFieldWriter.class);
    if (session.head().getTargetField() instanceof FieldGroup) {
        FieldGroup targetFieldGroup = (FieldGroup) session.head().getTargetField();
        if (targetFieldGroup.getField().size() > 0) {
            for (Field f : targetFieldGroup.getField()) {
                session.head().setTargetField(f);
                writer.write(session);
            }
            return;
        }
    }
    writer.write(session);
}
Also used : JsonEnumField(io.atlasmap.json.v2.JsonEnumField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonFieldWriter(io.atlasmap.json.core.JsonFieldWriter) FieldGroup(io.atlasmap.v2.FieldGroup)

Example 38 with FieldGroup

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

the class JsonModule method populateTargetField.

@Override
public void populateTargetField(AtlasInternalSession session) throws AtlasException {
    Field sourceField = session.head().getSourceField();
    Field targetField = session.head().getTargetField();
    AtlasPath path = new AtlasPath(targetField.getPath());
    FieldGroup targetFieldGroup = null;
    if (path.hasCollection() && !path.isIndexedCollection()) {
        targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
        session.head().setTargetField(targetFieldGroup);
    }
    // Attempt to Auto-detect field type based on input value
    if (targetField.getFieldType() == null && sourceField.getValue() != null) {
        targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
    }
    if (targetFieldGroup == null) {
        if (sourceField instanceof FieldGroup) {
            List<Field> subFields = ((FieldGroup) sourceField).getField();
            if (subFields != null && subFields.size() > 0) {
                Integer index = targetField.getIndex();
                if (index != null) {
                    if (subFields.size() > index) {
                        sourceField = subFields.get(index);
                    } else {
                        AtlasUtil.addAudit(session, getDocId(), String.format("The number of source fields (%s) is smaller than target index (%s) - ignoring", subFields.size(), index), AuditStatus.WARN, null);
                        return;
                    }
                } else {
                    // The last one wins for compatibility
                    sourceField = subFields.get(subFields.size() - 1);
                }
                session.head().setSourceField(sourceField);
            }
        }
        super.populateTargetField(session);
    } else if (sourceField instanceof FieldGroup) {
        Field previousTargetSubField = null;
        for (int i = 0; i < ((FieldGroup) sourceField).getField().size(); i++) {
            Field sourceSubField = ((FieldGroup) sourceField).getField().get(i);
            JsonField targetSubField = new JsonField();
            AtlasJsonModelFactory.copyField(targetField, targetSubField, false);
            getCollectionHelper().copyCollectionIndexes(sourceField, sourceSubField, targetSubField, previousTargetSubField);
            previousTargetSubField = targetSubField;
            targetFieldGroup.getField().add(targetSubField);
            session.head().setSourceField(sourceSubField);
            session.head().setTargetField(targetSubField);
            super.populateTargetField(session);
        }
        session.head().setSourceField(sourceField);
        session.head().setTargetField(targetFieldGroup);
    } else {
        JsonField targetSubField = new JsonField();
        AtlasJsonModelFactory.copyField(targetField, targetSubField, false);
        path.setVacantCollectionIndex(0);
        targetSubField.setPath(path.toString());
        targetFieldGroup.getField().add(targetSubField);
        session.head().setTargetField(targetSubField);
        super.populateTargetField(session);
        session.head().setTargetField(targetFieldGroup);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processTargetFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}], TargetField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue(), targetField.getDocId(), targetField.getPath(), targetField.getFieldType(), targetField.getValue());
    }
}
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) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath)

Example 39 with FieldGroup

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

the class AtlasJsonModelFactory 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((JsonField) child, true));
        }
    }
    clone.getField().addAll(newChildren);
    return clone;
}
Also used : Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList)

Example 40 with FieldGroup

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

the class JsonFieldReaderTest method testJsonFieldTopmostArrayObject.

@Test
public void testJsonFieldTopmostArrayObject() throws Exception {
    final String document = "[\n" + "\t{\n" + "\t\t\"color\": \"red\",\n" + "\t\t\"value\": \"#f00\"\n" + "\t},\n" + "\t{\n" + "\t\t\"color\": \"green\",\n" + "\t\t\"value\": \"#0f0\"\n" + "\t},\n" + "\t{\n" + "\t\t\"color\": \"blue\",\n" + "\t\t\"value\": \"#00f\"\n" + "\t}]";
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/<0>/color");
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(field);
    reader.read(session);
    assertNotNull(field.getValue());
    assertEquals("red", field.getValue());
    field.setPath("/<1>/value");
    reader.read(session);
    assertNotNull(field.getValue());
    assertEquals("#0f0", field.getValue());
    field.setFieldType(null);
    field.setPath("/<>/color");
    Field readField = reader.read(session);
    assertEquals(FieldGroup.class, readField.getClass());
    FieldGroup readFieldGroup = (FieldGroup) readField;
    assertEquals(3, readFieldGroup.getField().size());
    assertEquals("red", readFieldGroup.getField().get(0).getValue());
    assertEquals("green", readFieldGroup.getField().get(1).getValue());
    assertEquals("blue", readFieldGroup.getField().get(2).getValue());
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) FieldGroup(io.atlasmap.v2.FieldGroup) Test(org.junit.jupiter.api.Test)

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