use of org.mule.runtime.api.meta.model.parameter.ParameterRole 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.runtime.api.meta.model.parameter.ParameterRole in project mule by mulesoft.
the class MacroExpansionModuleModel method extractParameters.
/**
* Iterates over the collection of {@link ParameterModel}s making a clear distinction between {@link ParameterRole#BEHAVIOUR}
* and {@link ParameterRole#CONTENT} or {@link ParameterRole#PRIMARY_CONTENT} roles, where the former maps to simple attributes
* while the latter are child elements.
* <p/>
* If the value of the parameter is missing, then it will try to pick up a default value (also from the
* {@link ParameterModel#getDefaultValue()})
*
* @param componentModel to look for the values
* @param parameters collection of parameters to look for in the parametrized {@link ComponentModel}
* @return a {@link Map} with the values to be macro expanded in the final mule application
*/
private Map<String, String> extractParameters(ComponentModel componentModel, List<ParameterModel> parameters) {
Map<String, String> valuesMap = new HashMap<>();
for (ParameterModel parameterExtension : parameters) {
String paramName = parameterExtension.getName();
String value = null;
switch(parameterExtension.getRole()) {
case BEHAVIOUR:
if (componentModel.getParameters().containsKey(paramName)) {
value = componentModel.getParameters().get(paramName);
}
break;
case CONTENT:
case PRIMARY_CONTENT:
final Optional<ComponentModel> childComponentModel = componentModel.getInnerComponents().stream().filter(cm -> paramName.equals(cm.getIdentifier().getName())).findFirst();
if (childComponentModel.isPresent()) {
value = childComponentModel.get().getTextContent();
}
break;
}
if (value == null && (parameterExtension.getDefaultValue() != null)) {
value = String.valueOf(parameterExtension.getDefaultValue());
}
if (value != null) {
valuesMap.put(paramName, value);
}
}
return valuesMap;
}
Aggregations