use of org.mule.metadata.api.model.StringType in project mule by mulesoft.
the class ExtensionDefinitionParser method parseField.
private void parseField(ObjectType type, DslElementSyntax typeDsl, ObjectFieldType objectField, Map<String, ParameterRole> parametersRole) {
final MetadataType fieldType = objectField.getValue();
final String fieldName = objectField.getKey().getName().getLocalPart();
final boolean acceptsReferences = ExtensionMetadataTypeUtils.acceptsReferences(objectField);
final Object defaultValue = getDefaultValue(fieldType).orElse(null);
final ExpressionSupport expressionSupport = getExpressionSupport(objectField);
Optional<DslElementSyntax> fieldDsl = typeDsl.getContainedElement(fieldName);
if (!fieldDsl.isPresent() && !isFlattenedParameterGroup(objectField)) {
return;
}
Optional<String> keyName = getInfrastructureParameterName(fieldType);
if (keyName.isPresent()) {
parseObject(fieldName, keyName.get(), (ObjectType) fieldType, defaultValue, expressionSupport, false, acceptsReferences, fieldDsl.get(), emptySet());
return;
}
final boolean isContent = isContent(parametersRole.getOrDefault(fieldName, BEHAVIOUR));
fieldType.accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
if (!parseAsContent(isContent, metadataType)) {
parseAttributeParameter(fieldName, fieldName, metadataType, defaultValue, expressionSupport, false, emptySet());
}
}
@Override
public void visitString(StringType stringType) {
if (fieldDsl.get().supportsChildDeclaration()) {
String elementName = fieldDsl.get().getElementName();
addParameter(fieldName, fromChildConfiguration(String.class).withWrapperIdentifier(elementName));
addDefinition(baseDefinitionBuilder.withIdentifier(elementName).withTypeDefinition(fromType(String.class)).withTypeConverter(value -> resolverOf(elementName, stringType, value, defaultValue, expressionSupport, false, emptySet(), acceptsReferences)).build());
} else {
defaultVisit(stringType);
}
}
@Override
public void visitObject(ObjectType objectType) {
if (objectType.isOpen()) {
if (!parseAsContent(isContent, objectType)) {
parseMapParameters(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, fieldDsl.get(), emptySet());
}
return;
}
if (isFlattenedParameterGroup(objectField)) {
dslResolver.resolve(objectType).ifPresent(objectDsl -> objectType.getFields().forEach(field -> parseField(objectType, objectDsl, field, parametersRole)));
return;
}
if (parseAsContent(isContent, objectType)) {
return;
}
DslElementSyntax dsl = fieldDsl.get();
if (!parsingContext.isRegistered(dsl.getElementName(), dsl.getPrefix())) {
parsingContext.registerObjectType(dsl.getElementName(), dsl.getPrefix(), type);
parseObjectParameter(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, acceptsReferences, dsl, emptySet());
} else {
parseObject(fieldName, fieldName, objectType, defaultValue, expressionSupport, false, acceptsReferences, dsl, emptySet());
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
if (!parseAsContent(isContent, arrayType)) {
parseCollectionParameter(fieldName, fieldName, arrayType, defaultValue, expressionSupport, false, fieldDsl.get(), emptySet());
}
}
private boolean parseAsContent(boolean isContent, MetadataType type) {
if (isContent) {
parseFromTextExpression(fieldName, fieldDsl.get(), () -> value -> resolverOf(fieldName, type, value, defaultValue, expressionSupport, false, emptySet(), false));
return true;
}
return false;
}
});
}
use of org.mule.metadata.api.model.StringType in project mule by mulesoft.
the class ExtensionDefinitionParser method parseParameters.
/**
* Parsers the given {@code parameters} and generates matching definitions
*
* @param parameters a list of {@link ParameterModel}
*/
protected void parseParameters(List<ParameterModel> parameters) {
parameters.forEach(parameter -> {
final DslElementSyntax paramDsl = dslResolver.resolve(parameter);
final boolean isContent = isContent(parameter);
parameter.getType().accept(new MetadataTypeVisitor() {
@Override
protected void defaultVisit(MetadataType metadataType) {
if (!parseAsContent(metadataType)) {
parseAttributeParameter(parameter);
}
}
@Override
public void visitString(StringType stringType) {
if (paramDsl.supportsChildDeclaration()) {
parseFromTextExpression(parameter, paramDsl, () -> {
Optional<QueryParameterModelProperty> query = parameter.getModelProperty(QueryParameterModelProperty.class);
return value -> {
ValueResolver<String> resolver = resolverOf(parameter.getName(), stringType, value, parameter.getDefaultValue(), parameter.getExpressionSupport(), parameter.isRequired(), parameter.getModelProperties(), acceptsReferences(parameter));
return query.map(p -> (ValueResolver<String>) new NativeQueryParameterValueResolver(resolver, p.getQueryTranslator())).orElse(resolver);
};
});
} else {
defaultVisit(stringType);
}
}
@Override
public void visitObject(ObjectType objectType) {
if (parseAsContent(objectType)) {
return;
}
if (isMap(objectType)) {
parseMapParameters(parameter, objectType, paramDsl);
return;
}
if (!parsingContext.isRegistered(paramDsl.getElementName(), paramDsl.getPrefix())) {
if (!paramDsl.supportsTopLevelDeclaration() && paramDsl.supportsChildDeclaration()) {
parsingContext.registerObjectType(paramDsl.getElementName(), paramDsl.getPrefix(), objectType);
}
parseObjectParameter(parameter, paramDsl);
} else {
parseObject(getKey(parameter), parameter.getName(), objectType, parameter.getDefaultValue(), parameter.getExpressionSupport(), parameter.isRequired(), acceptsReferences(parameter), paramDsl, parameter.getModelProperties());
}
}
@Override
public void visitArrayType(ArrayType arrayType) {
if (!parseAsContent(arrayType)) {
parseCollectionParameter(parameter, arrayType, paramDsl);
}
}
private boolean parseAsContent(MetadataType type) {
if (isContent) {
parseFromTextExpression(parameter, paramDsl, () -> value -> resolverOf(parameter.getName(), type, value, parameter.getDefaultValue(), parameter.getExpressionSupport(), parameter.isRequired(), parameter.getModelProperties(), acceptsReferences(parameter)));
return true;
}
return false;
}
});
});
}
use of org.mule.metadata.api.model.StringType in project mule by mulesoft.
the class SchemaBuilder method createAttribute.
private Attribute createAttribute(final String name, String description, final MetadataType type, Object defaultValue, boolean required, final ExpressionSupport expressionSupport) {
final Attribute attribute = new Attribute();
attribute.setUse(required ? USE_REQUIRED : USE_OPTIONAL);
attribute.setAnnotation(createDocAnnotation(description));
if ((defaultValue instanceof String && isNotBlank(defaultValue.toString())) || defaultValue instanceof Enum) {
attribute.setDefault(defaultValue.toString());
}
type.accept(new MetadataTypeVisitor() {
@Override
public void visitString(StringType stringType) {
Optional<EnumAnnotation> enumAnnotation = stringType.getAnnotation(EnumAnnotation.class);
if (enumAnnotation.isPresent()) {
visitEnum(stringType);
} else {
defaultVisit(stringType);
}
}
private void visitEnum(StringType enumType) {
attribute.setName(name);
String typeName = getTypeId(enumType);
if (OperationTransactionalAction.class.getName().equals(typeName)) {
attribute.setType(MULE_OPERATION_TRANSACTIONAL_ACTION_TYPE);
} else if (TransactionType.class.getName().equals(typeName)) {
attribute.setType(MULE_TRANSACTION_TYPE);
attribute.setDefault(LOCAL.name());
} else {
attribute.setType(new QName(schema.getTargetNamespace(), sanitizeName(typeName) + ENUM_TYPE_SUFFIX));
registeredEnums.add(enumType);
}
}
@Override
protected void defaultVisit(MetadataType metadataType) {
attribute.setName(name);
attribute.setType(SchemaTypeConversion.convertType(type, expressionSupport));
}
});
return attribute;
}
use of org.mule.metadata.api.model.StringType in project mule by mulesoft.
the class ValueProviderModelValidator method validateOptionsResolver.
private void validateOptionsResolver(ParameterModel param, ValueProviderFactoryModelProperty modelProperty, ParameterizedModel model, ProblemsReporter problemsReporter, boolean supportsConnectionsAndConfigs, ReflectionCache reflectionCache) {
Class<? extends ValueProvider> valueProvider = modelProperty.getValueProvider();
String providerName = valueProvider.getSimpleName();
Map<String, MetadataType> allParameters = model.getAllParameterModels().stream().collect(toMap(IntrospectionUtils::getImplementingName, ParameterModel::getType));
String modelName = NameUtils.getModelName(model);
String modelTypeName = NameUtils.getComponentModelTypeName(model);
if (!isInstantiable(valueProvider, reflectionCache)) {
problemsReporter.addError(new Problem(model, format("The Value Provider [%s] is not instantiable but it should", providerName)));
}
if (!(param.getType() instanceof StringType)) {
problemsReporter.addError(new Problem(model, format("The parameter [%s] of the %s '%s' is not of String type. Parameters that provides Values should be of String type.", param.getName(), modelTypeName, modelName)));
}
for (InjectableParameterInfo parameterInfo : modelProperty.getInjectableParameters()) {
if (!allParameters.containsKey(parameterInfo.getParameterName())) {
problemsReporter.addError(new Problem(model, format("The Value Provider [%s] declares a parameter '%s' which doesn't exist in the %s '%s'", providerName, parameterInfo.getParameterName(), modelTypeName, modelName)));
} else {
MetadataType metadataType = allParameters.get(parameterInfo.getParameterName());
Class<?> expectedType = getType(metadataType).orElseThrow(() -> new IllegalStateException(format("Unable to get Class for parameter: %s", parameterInfo.getParameterName())));
Class<?> gotType = getType(parameterInfo.getType()).orElseThrow(() -> new IllegalStateException(format("Unable to get Class for parameter: %s", parameterInfo.getParameterName())));
if (!expectedType.equals(gotType)) {
problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines a parameter '%s' of type '%s' but in the %s '%s' is of type '%s'", providerName, parameterInfo.getParameterName(), gotType, modelTypeName, modelName, expectedType)));
}
}
}
if (supportsConnectionsAndConfigs && modelProperty.usesConnection() && model instanceof ConnectableComponentModel) {
boolean requiresConnection = ((ConnectableComponentModel) model).requiresConnection();
if (requiresConnection != modelProperty.usesConnection()) {
problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines that requires a connection, but is used in the %s '%s' which is connection less", providerName, modelTypeName, modelName)));
}
}
if (!supportsConnectionsAndConfigs) {
if (modelProperty.usesConnection()) {
problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines that requires a connection which is not allowed for a Value Provider of a %s's parameter [%s]", providerName, modelTypeName, modelName)));
}
if (modelProperty.usesConfig()) {
problemsReporter.addError(new Problem(model, format("The Value Provider [%s] defines that requires a configuration which is not allowed for a Value Provider of a %s's parameter [%s]", providerName, modelTypeName, modelName)));
}
}
}
use of org.mule.metadata.api.model.StringType in project mule by mulesoft.
the class IntrospectionUtils method collectRelativeClassesAsString.
/**
* Given a {@link MetadataType} it adds all the {@link Class} that are related from that type. This includes generics of an
* {@link ArrayType}, open restriction of an {@link ObjectType} as well as its fields.
*
* @param type {@link MetadataType} to inspect
* @return {@link Set<Class>>} with the classes reachable from the {@code type}
*/
public static Set<String> collectRelativeClassesAsString(MetadataType type) {
Set<String> relativeClasses = new HashSet<>();
type.accept(new MetadataTypeVisitor() {
@Override
public void visitArrayType(ArrayType arrayType) {
arrayType.getType().accept(this);
}
@Override
public void visitObjectField(ObjectFieldType objectFieldType) {
objectFieldType.getValue().accept(this);
}
@Override
public void visitObject(ObjectType objectType) {
if (objectType.getMetadataFormat() != JAVA) {
return;
}
final String clazz = getId(objectType).orElse(null);
if (clazz == null || relativeClasses.contains(clazz)) {
return;
}
Optional<ClassInformationAnnotation> classInformation = objectType.getAnnotation(ClassInformationAnnotation.class);
classInformation.ifPresent(classInformationAnnotation -> relativeClasses.addAll(classInformationAnnotation.getGenericTypes()));
relativeClasses.add(clazz);
objectType.getFields().forEach(objectFieldType -> objectFieldType.accept(this));
objectType.getOpenRestriction().ifPresent(t -> t.accept(this));
}
@Override
public void visitString(StringType stringType) {
if (stringType.getMetadataFormat() == JAVA && isEnum(stringType)) {
getId(stringType).ifPresent(relativeClasses::add);
}
}
});
return relativeClasses;
}
Aggregations