Search in sources :

Example 1 with AtlasConverter

use of io.atlasmap.spi.AtlasConverter in project atlasmap by atlasmap.

the class DefaultAtlasConversionServiceTest method findMatchingConverterByFieldTypes.

@Test
public void findMatchingConverterByFieldTypes() {
    assertNotNull(service);
    Optional<AtlasConverter<?>> atlasConverter = service.findMatchingConverter(FieldType.STRING, FieldType.BOOLEAN);
    assertTrue(atlasConverter.isPresent());
    assertNotNull(atlasConverter);
    assertEquals(StringConverter.class, atlasConverter.get().getClass());
    StringConverter stringConverter = (StringConverter) atlasConverter.get();
    assertNotNull(stringConverter);
    assertEquals("io.atlasmap.converters.StringConverter", atlasConverter.get().getClass().getCanonicalName());
    Boolean t = stringConverter.toBoolean("T", null, null);
    assertNotNull(t);
    assertTrue(t);
    Boolean f = stringConverter.toBoolean("F", null, null);
    assertNotNull(f);
    assertFalse(f);
    service.findMatchingConverter(null, FieldType.BOOLEAN);
    service.findMatchingConverter(FieldType.STRING, null);
    FieldType fieldType = null;
    service.findMatchingConverter(fieldType, fieldType);
}
Also used : AtlasConverter(io.atlasmap.spi.AtlasConverter) StringConverter(io.atlasmap.converters.StringConverter) FieldType(io.atlasmap.v2.FieldType) Test(org.junit.jupiter.api.Test)

Example 2 with AtlasConverter

use of io.atlasmap.spi.AtlasConverter in project atlasmap by atlasmap.

the class JavaMappingFieldPairValidator method validateClassConversion.

private void validateClassConversion(String mappingId, JavaField inputField, JavaField outField, List<Validation> validations) {
    // skip converter check for COMPLEX source field (possible for conditional mapping)
    if (inputField.getFieldType() == FieldType.COMPLEX) {
        return;
    }
    Optional<AtlasConverter<?>> atlasConverter = conversionService.findMatchingConverter(inputField.getClassName(), outField.getClassName());
    if (!atlasConverter.isPresent()) {
        Validation validation = new Validation();
        validation.setScope(ValidationScope.MAPPING);
        validation.setId(mappingId);
        validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", inputField.getClassName(), outField.getClassName()));
        validation.setStatus(ValidationStatus.ERROR);
        validations.add(validation);
    } else {
        AtlasConversionInfo conversionInfo;
        // find the method that does the conversion
        Method[] methods = atlasConverter.get().getClass().getMethods();
        conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> atlasConversionInfo.sourceType().compareTo(inputField.getFieldType()) == 0 && atlasConversionInfo.targetType().compareTo(outField.getFieldType()) == 0).findFirst().orElse(null);
        if (conversionInfo != null) {
            populateConversionConcerns(validations, mappingId, conversionInfo, service.getModuleFieldName(inputField), service.getModuleFieldName(outField));
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Arrays(java.util.Arrays) AtlasConversionService(io.atlasmap.spi.AtlasConversionService) ValidationScope(io.atlasmap.v2.ValidationScope) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) ValidationStatus(io.atlasmap.v2.ValidationStatus) FieldType(io.atlasmap.v2.FieldType) AtlasConverter(io.atlasmap.spi.AtlasConverter) Validation(io.atlasmap.v2.Validation) List(java.util.List) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) Optional(java.util.Optional) MappingFieldPairValidator(io.atlasmap.core.validate.MappingFieldPairValidator) Method(java.lang.reflect.Method) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.spi.AtlasConverter) Method(java.lang.reflect.Method)

Example 3 with AtlasConverter

use of io.atlasmap.spi.AtlasConverter in project atlasmap by atlasmap.

the class MappingFieldPairValidator method doValidateFieldTypes.

/**
 * Validates field types.
 * @param validations a container to put the result {@link Validation}.
 * @param mappingId mapping ID
 * @param sourceField source field
 * @param targetField target field
 * @param sourceFieldType source field type
 */
protected void doValidateFieldTypes(List<Validation> validations, String mappingId, Field sourceField, Field targetField, FieldType sourceFieldType) {
    if (sourceField == null && targetField == null || sourceField.getFieldType() == targetField.getFieldType()) {
        return;
    }
    FieldType targetFieldType = targetField.getFieldType();
    if (sourceFieldType == null || targetFieldType == null) {
        return;
    }
    if (sourceFieldType == FieldType.ANY || targetFieldType == FieldType.ANY) {
        return;
    }
    // skip converter check for COMPLEX source field (possible for conditional mapping)
    if (sourceField.getFieldType() == FieldType.COMPLEX) {
        return;
    }
    Optional<AtlasConverter<?>> atlasConverter = service.getConversionService().findMatchingConverter(sourceFieldType, targetFieldType);
    if (!atlasConverter.isPresent()) {
        Validation validation = new Validation();
        validation.setScope(ValidationScope.MAPPING);
        validation.setId(mappingId);
        validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", sourceField.getFieldType(), targetField.getFieldType()));
        validation.setStatus(ValidationStatus.ERROR);
        validations.add(validation);
    } else {
        AtlasConversionInfo conversionInfo;
        // find the method that does the conversion
        FieldType sft = sourceFieldType;
        Method[] methods = atlasConverter.get().getClass().getMethods();
        conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> (atlasConversionInfo.sourceType().compareTo(sft) == 0 && atlasConversionInfo.targetType().compareTo(targetFieldType) == 0)).findFirst().orElse(null);
        if (conversionInfo != null) {
            populateConversionConcerns(validations, mappingId, conversionInfo, service.getFieldName(sourceField), service.getFieldName(targetField));
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Arrays(java.util.Arrays) FieldGroup(io.atlasmap.v2.FieldGroup) Logger(org.slf4j.Logger) AtlasException(io.atlasmap.api.AtlasException) ValidationScope(io.atlasmap.v2.ValidationScope) LoggerFactory(org.slf4j.LoggerFactory) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) ValidationStatus(io.atlasmap.v2.ValidationStatus) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern) ActionDetail(io.atlasmap.v2.ActionDetail) FieldType(io.atlasmap.v2.FieldType) AtlasConverter(io.atlasmap.spi.AtlasConverter) Validation(io.atlasmap.v2.Validation) List(java.util.List) Field(io.atlasmap.v2.Field) Action(io.atlasmap.v2.Action) Optional(java.util.Optional) Method(java.lang.reflect.Method) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.spi.AtlasConverter) Method(java.lang.reflect.Method) FieldType(io.atlasmap.v2.FieldType)

Aggregations

AtlasConverter (io.atlasmap.spi.AtlasConverter)3 FieldType (io.atlasmap.v2.FieldType)3 AtlasConversionInfo (io.atlasmap.spi.AtlasConversionInfo)2 Field (io.atlasmap.v2.Field)2 Validation (io.atlasmap.v2.Validation)2 ValidationScope (io.atlasmap.v2.ValidationScope)2 ValidationStatus (io.atlasmap.v2.ValidationStatus)2 Method (java.lang.reflect.Method)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 AtlasException (io.atlasmap.api.AtlasException)1 StringConverter (io.atlasmap.converters.StringConverter)1 MappingFieldPairValidator (io.atlasmap.core.validate.MappingFieldPairValidator)1 JavaField (io.atlasmap.java.v2.JavaField)1 AtlasConversionConcern (io.atlasmap.spi.AtlasConversionConcern)1 AtlasConversionService (io.atlasmap.spi.AtlasConversionService)1 Action (io.atlasmap.v2.Action)1 ActionDetail (io.atlasmap.v2.ActionDetail)1 FieldGroup (io.atlasmap.v2.FieldGroup)1