use of org.mule.metadata.api.model.DateTimeType in project mule by mulesoft.
the class ExtensionDefinitionParser method parseCollectionParameter.
/**
* Registers a definition for a {@link ParameterModel} which represents an {@link ArrayType}
*
* @param key the key that the parsed value should have on the parsed parameter's map
* @param name the parameter's name
* @param arrayType the parameter's {@link ArrayType}
* @param defaultValue the parameter's default value
* @param expressionSupport the parameter's {@link ExpressionSupport}
* @param required whether the parameter is required
*/
protected void parseCollectionParameter(String key, String name, ArrayType arrayType, Object defaultValue, ExpressionSupport expressionSupport, boolean required, DslElementSyntax parameterDsl, Set<ModelProperty> modelProperties) {
parseAttributeParameter(key, name, arrayType, defaultValue, expressionSupport, required, modelProperties);
Class<?> collectionType = ExtensionMetadataTypeUtils.getType(arrayType).orElse(null);
if (Set.class.equals(collectionType)) {
collectionType = HashSet.class;
} else if (Collection.class.equals(collectionType) || Iterable.class.equals(collectionType) || collectionType == null) {
collectionType = List.class;
}
final String collectionElementName = parameterDsl.getElementName();
addParameter(getChildKey(key), fromChildConfiguration(collectionType).withWrapperIdentifier(collectionElementName));
addDefinition(baseDefinitionBuilder.withIdentifier(collectionElementName).withTypeDefinition(fromType(collectionType)).build());
Optional<DslElementSyntax> collectionItemDsl = parameterDsl.getGeneric(arrayType.getType());
if (parameterDsl.supportsChildDeclaration() && collectionItemDsl.isPresent()) {
String itemIdentifier = collectionItemDsl.get().getElementName();
String itemNamespace = collectionItemDsl.get().getPrefix();
arrayType.getType().accept(new BasicTypeMetadataVisitor() {
private void addBasicTypeDefinition(MetadataType metadataType) {
Builder itemDefinitionBuilder = baseDefinitionBuilder.withIdentifier(itemIdentifier).withNamespace(itemNamespace).withTypeDefinition(fromType(ExtensionMetadataTypeUtils.getType(metadataType).orElse(Object.class))).withTypeConverter(value -> resolverOf(name, metadataType, value, getDefaultValue(metadataType).orElse(null), getExpressionSupport(metadataType), false, emptySet()));
addDefinition(itemDefinitionBuilder.build());
}
@Override
protected void visitBasicType(MetadataType metadataType) {
addBasicTypeDefinition(metadataType);
}
@Override
public void visitDate(DateType dateType) {
addBasicTypeDefinition(dateType);
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
addBasicTypeDefinition(dateTimeType);
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
return;
}
DslElementSyntax itemDsl = collectionItemDsl.get();
if ((itemDsl.supportsTopLevelDeclaration() || itemDsl.supportsChildDeclaration()) && !parsingContext.isRegistered(itemDsl.getElementName(), itemDsl.getPrefix())) {
try {
parsingContext.registerObjectType(itemDsl.getElementName(), itemDsl.getPrefix(), objectType);
new ObjectTypeParameterParser(baseDefinitionBuilder, objectType, getContextClassLoader(), dslResolver, parsingContext).parse().forEach(definition -> addDefinition(definition));
} catch (ConfigurationException e) {
throw new MuleRuntimeException(createStaticMessage("Could not create parser for collection complex type"), e);
}
}
}
});
}
}
use of org.mule.metadata.api.model.DateTimeType in project mule by mulesoft.
the class ExtensionDefinitionParser method getValueResolverFromMetadataType.
private ValueResolver getValueResolverFromMetadataType(final String parameterName, MetadataType expectedType, final Object value, final Object defaultValue, final boolean acceptsReferences, final Class<?> expectedClass) {
final Reference<ValueResolver> resolverValueHolder = new Reference<>();
expectedType.accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
if (conversionService.canConvert(value.getClass(), expectedClass)) {
resolverValueHolder.set(new StaticValueResolver<>(convertSimpleValue(value, expectedClass, parameterName)));
} else {
defaultVisit(metadataType);
}
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
resolverValueHolder.set(parseDate(value, dateTimeType, defaultValue));
}
@Override
public void visitDate(DateType dateType) {
resolverValueHolder.set(parseDate(value, dateType, defaultValue));
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
defaultVisit(objectType);
return;
}
ValueResolver valueResolver;
Optional<? extends ParsingDelegate> delegate = locateParsingDelegate(valueResolverParsingDelegates, objectType);
Optional<DslElementSyntax> typeDsl = dslResolver.resolve(objectType);
if (delegate.isPresent() && typeDsl.isPresent()) {
valueResolver = (ValueResolver) delegate.get().parse(value.toString(), objectType, typeDsl.get());
} else {
valueResolver = acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), objectType, null) : new StaticValueResolver<>(value);
}
resolverValueHolder.set(valueResolver);
}
@Override
protected void defaultVisit(MetadataType metadataType) {
ValueResolver delegateResolver = locateParsingDelegate(valueResolverParsingDelegates, metadataType).map(delegate -> delegate.parse(value.toString(), metadataType, null)).orElseGet(() -> acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), metadataType, null) : new TypeSafeValueResolverWrapper(new StaticValueResolver<>(value), expectedClass));
resolverValueHolder.set(delegateResolver);
}
});
return resolverValueHolder.get();
}
use of org.mule.metadata.api.model.DateTimeType in project mule by mulesoft.
the class SchemaTypeConversion method convertType.
public static QName convertType(final MetadataType type, ExpressionSupport expressionSupport) {
final boolean dynamic = acceptsExpressions(expressionSupport);
final Reference<QName> qName = new Reference<>(null);
type.accept(new MetadataTypeVisitor() {
@Override
public void visitBoolean(BooleanType booleanType) {
qName.set(dynamic ? EXPRESSION_BOOLEAN : SUBSTITUTABLE_BOOLEAN);
}
@Override
public void visitNumber(NumberType numberType) {
if (getId(numberType).isPresent()) {
Class<Number> type = JavaTypeUtils.getType(numberType);
if (anyOf(type, Integer.class, int.class)) {
qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
} else if (anyOf(type, Double.class, double.class)) {
qName.set(dynamic ? EXPRESSION_DOUBLE : SUBSTITUTABLE_DECIMAL);
} else if (anyOf(type, Long.class, long.class)) {
qName.set(dynamic ? EXPRESSION_LONG : SUBSTITUTABLE_LONG);
} else {
qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
}
} else {
if (numberType.getAnnotation(IntAnnotation.class).isPresent()) {
qName.set(dynamic ? EXPRESSION_INTEGER : SUBSTITUTABLE_INT);
} else {
qName.set(dynamic ? EXPRESSION_DECIMAL : SUBSTITUTABLE_DECIMAL);
}
}
}
@Override
public void visitString(StringType stringType) {
qName.set(dynamic ? EXPRESSION_STRING : STRING);
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
onDate();
}
@Override
public void visitDate(DateType dateType) {
onDate();
}
@Override
public void visitArrayType(ArrayType arrayType) {
qName.set(dynamic ? EXPRESSION_LIST : SUBSTITUTABLE_NAME);
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
qName.set(dynamic ? EXPRESSION_MAP : SUBSTITUTABLE_NAME);
} else {
defaultVisit(objectType);
}
}
@Override
protected void defaultVisit(MetadataType metadataType) {
qName.set(STRING);
}
private void onDate() {
qName.set(dynamic ? EXPRESSION_DATE_TIME : SUBSTITUTABLE_DATE_TIME);
}
private boolean anyOf(Class<Number> type, Class<?>... targets) {
for (Class<?> target : targets) {
if (type.equals(target)) {
return true;
}
}
return false;
}
});
return qName.get();
}
Aggregations