use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class ExtensionDefinitionParser method parseMapParameters.
/**
* Registers a definition for a {@link ParameterModel} which represents an open {@link ObjectType}
*
* @param key the key that the parsed value should have on the parsed parameter's map
* @param name the parameter's name
* @param dictionaryType the parameter's open {@link ObjectType}
* @param defaultValue the parameter's default value
* @param expressionSupport the parameter's {@link ExpressionSupport}
* @param required whether the parameter is required
*/
protected void parseMapParameters(String key, String name, ObjectType dictionaryType, Object defaultValue, ExpressionSupport expressionSupport, boolean required, DslElementSyntax paramDsl, Set<ModelProperty> modelProperties) {
parseAttributeParameter(key, name, dictionaryType, defaultValue, expressionSupport, required, modelProperties);
Class<? extends Map> mapType = getType(dictionaryType);
if (ConcurrentMap.class.equals(mapType)) {
mapType = ConcurrentHashMap.class;
} else if (Map.class.equals(mapType)) {
mapType = LinkedHashMap.class;
}
final MetadataType valueType = dictionaryType.getOpenRestriction().orElse(typeLoader.load(Object.class));
final Class<?> valueClass = getType(valueType);
final MetadataType keyType = typeLoader.load(String.class);
final Class<?> keyClass = String.class;
final String mapElementName = paramDsl.getElementName();
addParameter(getChildKey(key), fromChildMapConfiguration(String.class, valueClass).withWrapperIdentifier(mapElementName).withDefaultValue(defaultValue));
addDefinition(baseDefinitionBuilder.withIdentifier(mapElementName).withTypeDefinition(fromType(mapType)).build());
Optional<DslElementSyntax> mapValueChildDsl = paramDsl.getGeneric(valueType);
if (!mapValueChildDsl.isPresent()) {
return;
}
DslElementSyntax valueDsl = mapValueChildDsl.get();
valueType.accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
String parameterName = paramDsl.getAttributeName();
addDefinition(baseDefinitionBuilder.withIdentifier(valueDsl.getElementName()).withTypeDefinition(fromMapEntryType(keyClass, valueClass)).withKeyTypeConverter(value -> resolverOf(parameterName, keyType, value, null, expressionSupport, true, emptySet(), false)).withTypeConverter(value -> resolverOf(parameterName, valueType, value, null, expressionSupport, true, emptySet(), false)).build());
}
@Override
public void visitObject(ObjectType objectType) {
defaultVisit(objectType);
Optional<DslElementSyntax> containedElement = valueDsl.getContainedElement(VALUE_ATTRIBUTE_NAME);
if (isMap(objectType) || !containedElement.isPresent()) {
return;
}
DslElementSyntax valueChild = containedElement.get();
if ((valueChild.supportsTopLevelDeclaration() || (valueChild.supportsChildDeclaration() && !valueChild.isWrapped())) && !parsingContext.isRegistered(valueChild.getElementName(), valueChild.getPrefix())) {
try {
parsingContext.registerObjectType(valueChild.getElementName(), valueChild.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 map complex type"), e);
}
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
defaultVisit(arrayType);
Optional<DslElementSyntax> valueListGenericDsl = valueDsl.getGeneric(arrayType.getType());
if (valueDsl.supportsChildDeclaration() && valueListGenericDsl.isPresent()) {
arrayType.getType().accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
String parameterName = paramDsl.getAttributeName();
addDefinition(baseDefinitionBuilder.withIdentifier(valueListGenericDsl.get().getElementName()).withTypeDefinition(fromType(getType(metadataType))).withTypeConverter(value -> resolverOf(parameterName, metadataType, value, getDefaultValue(metadataType), getExpressionSupport(metadataType), false, emptySet())).build());
}
@Override
protected void defaultVisit(MetadataType metadataType) {
addDefinition(baseDefinitionBuilder.withIdentifier(valueListGenericDsl.get().getElementName()).withTypeDefinition(fromType(ValueResolver.class)).withObjectFactoryType(TopLevelParameterObjectFactory.class).withConstructorParameterDefinition(fromFixedValue(arrayType.getType()).build()).withConstructorParameterDefinition(fromFixedValue(getContextClassLoader()).build()).build());
}
});
}
}
});
}
use of org.mule.metadata.api.model.MetadataType 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.MetadataType 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();
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class ReflectiveFunctionExecutorFactory method createExecutor.
@Override
public FunctionExecutor createExecutor(FunctionModel functionModel, FunctionParameterDefaultValueResolverFactory defaultResolverFactory) {
DataType returnType = fromType(getType(functionModel.getOutput().getType()).orElseThrow(() -> new MuleRuntimeException(createStaticMessage(format("Failed to obtain the return type for function [%s]", functionModel.getName())))));
List<FunctionParameter> functionParameters = functionModel.getAllParameterModels().stream().map(p -> {
MetadataType paramType = p.getType();
DataType type = isTypedValue(paramType) ? fromType(TypedValue.class) : toDataType(paramType);
if (p.isRequired()) {
return new FunctionParameter(p.getName(), type);
}
Object defaultValue = p.getDefaultValue();
if (defaultValue == null) {
return new FunctionParameter(p.getName(), type, context -> Defaults.defaultValue(type.getType()));
}
return new FunctionParameter(p.getName(), type, defaultResolverFactory.create(defaultValue, type));
}).collect(toList());
return new ReflectiveExpressionFunctionExecutor(functionModel, returnType, functionParameters, method, getDelegateInstance());
}
use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.
the class InputParametersTypeModelValidator method validateType.
private void validateType(String message, NamedObject namedObject, MetadataType type, ProblemsReporter problems, Set<Class<?>> validatedTypes) {
ReflectionCache reflectionCache = new ReflectionCache();
getClassForValidation(type).ifPresent(parameterType -> type.accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
if (validatedTypes.add(parameterType)) {
Collection<ObjectFieldType> parameters = objectType.getFields();
Set<String> fieldsWithGetters = getFieldsWithGetters(parameterType, reflectionCache).stream().map(TypeUtils::getAlias).map(String::toLowerCase).collect(toSet());
Set<String> parameterWithoutGetters = parameters.stream().filter(p -> {
StereotypeTypeAnnotation stereotypes = p.getAnnotation(StereotypeTypeAnnotation.class).orElse(null);
return stereotypes != null ? stereotypes.getAllowedStereotypes().isEmpty() : true;
}).map(f -> f.getKey().getName().getLocalPart()).filter(fieldName -> !fieldsWithGetters.contains(fieldName.toLowerCase())).collect(toSet());
if (!parameterWithoutGetters.isEmpty()) {
problems.addError(new Problem(namedObject, format("%s of type '%s' which contains fields (%s) that doesn't have the corresponding getter methods or getter methods that doesn't correspond to any of the present fields", message, parameterType.getName(), parameterWithoutGetters.stream().collect(joining(", ")))));
}
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
validateType(message, namedObject, arrayType.getType(), problems, validatedTypes);
}
}));
}
Aggregations