use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class IntrospectionUtils method toDataType.
/**
* Transforms a {@link MetadataType} and generates the correspondent {@link DataType}
*
* @param metadataType to introspect a create a {@link DataType} from it.
* @return a {@link DataType} based on the given {@link MetadataType}
*/
public static DataType toDataType(MetadataType metadataType) {
Class<?> type = getType(metadataType).orElse(null);
if (type == null) {
return DataType.fromType(Object.class);
}
Reference<DataType> dataType = new Reference<>();
metadataType.accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
dataType.set(DataType.fromType(type));
}
@Override
public void visitArrayType(ArrayType arrayType) {
Class itemClass = getType(arrayType.getType()).get();
if (Collection.class.isAssignableFrom(type)) {
dataType.set(DataType.builder().collectionType((Class<? extends Collection>) type).itemType(itemClass).build());
} else if (Iterator.class.isAssignableFrom(type)) {
dataType.set(DataType.builder().streamType((Class<? extends Iterator>) type).itemType(itemClass).build());
} else {
defaultVisit(arrayType);
}
}
@Override
public void visitObject(ObjectType objectType) {
if (Map.class.isAssignableFrom(type)) {
dataType.set(DataType.builder().mapType((Class<? extends Map>) type).keyType(String.class).valueType(objectType.getOpenRestriction().map(restriction -> {
if (restriction.getAnnotation(TypedValueTypeAnnotation.class).isPresent()) {
return TypedValue.class;
}
return getType(restriction).get();
}).orElse(Object.class)).build());
} else {
defaultVisit(objectType);
}
}
});
return dataType.get();
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class IntrospectionUtilsTestCase method getDataTypeFromString.
@Test
public void getDataTypeFromString() {
StringTypeBuilder typeBuilder = BaseTypeBuilder.create(JAVA).stringType();
DataType dataType = toDataType(typeBuilder.build());
assertThat(dataType.getType(), is(equalTo(String.class)));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class OperationMessageProcessorTestCase method operationReturnsResultMapWithCorrectDataType.
@Test
public void operationReturnsResultMapWithCorrectDataType() throws Exception {
Object payload = new HashMap<>();
setUpOperationReturning(Result.builder().output(payload).build(), new TypeToken<Map<String, String>>() {
}.getType());
Message message = messageProcessor.process(event).getMessage();
assertThat(message, is(notNullValue()));
assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
DataType dataType = message.getPayload().getDataType();
assertThat(dataType, instanceOf(MapDataType.class));
assertThat(((MapDataType) dataType).getKeyDataType(), like(String.class, ANY.withCharset(null)));
assertThat(((MapDataType) dataType).getValueDataType(), like(String.class, ANY));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class OperationMessageProcessorTestCase method operationReturnsMapWithCorrectDataType.
@Test
public void operationReturnsMapWithCorrectDataType() throws Exception {
Object payload = new HashMap<>();
setUpOperationReturning(payload, new TypeToken<Map<String, String>>() {
}.getType());
Message message = messageProcessor.process(event).getMessage();
assertThat(message, is(notNullValue()));
assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
DataType dataType = message.getPayload().getDataType();
assertThat(dataType, instanceOf(MapDataType.class));
assertThat(((MapDataType) dataType).getKeyDataType(), like(String.class, ANY.withCharset(null)));
assertThat(((MapDataType) dataType).getValueDataType(), like(String.class, ANY));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class OperationMessageProcessorTestCase method operationReturnsResultCollectionWithCorrectDataType.
@Test
public void operationReturnsResultCollectionWithCorrectDataType() throws Exception {
Object payload = new ArrayList<>();
setUpOperationReturning(Result.builder().output(payload).build(), new TypeToken<List<Integer>>() {
}.getType());
Message message = messageProcessor.process(event).getMessage();
assertThat(message, is(notNullValue()));
assertThat(message.getPayload().getValue(), is(sameInstance(payload)));
DataType dataType = message.getPayload().getDataType();
assertThat(dataType, instanceOf(CollectionDataType.class));
assertThat(((CollectionDataType) dataType).getItemDataType(), like(Integer.class, ANY.withCharset(null)));
}
Aggregations