use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class ExtensionDefinitionParser method getExpressionBasedValueResolver.
/**
* Generates the {@link ValueResolver} for expression based values
*/
private ValueResolver getExpressionBasedValueResolver(MetadataType expectedType, String value, Set<ModelProperty> modelProperties, Class<?> expectedClass) {
ValueResolver resolver;
Optional<StackedTypesModelProperty> stackedTypesModelProperty = getStackedTypesModelProperty(modelProperties);
if (stackedTypesModelProperty.isPresent()) {
resolver = stackedTypesModelProperty.get().getValueResolverFactory().getExpressionBasedValueResolver(value, expectedClass);
// TODO MULE-13518: Add support for stacked value resolvers for @Parameter inside pojos // The following "IFs" should be removed once implemented
} else if (isParameterResolver(expectedType)) {
resolver = new ExpressionBasedParameterResolverValueResolver<>(value, expectedClass, expectedType);
} else if (isTypedValue(expectedType)) {
resolver = new ExpressionTypedValueValueResolver<>(value, expectedClass);
} else if (isLiteral(expectedType) || isTargetParameter(modelProperties)) {
resolver = new StaticLiteralValueResolver<>(value, expectedClass);
} else {
resolver = new TypeSafeExpressionValueResolver<>(value, expectedClass, expectedType);
}
return resolver;
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver 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.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class StackableTypesValueResolverFactory method getExpressionBasedValueResolver.
/**
* Given a expression value, the expected type and the stack of {@link StackableType wrapper types}, iterates it and
* creates all the required {@link ValueResolver} and stacks them is necessary.
*
* @param expression Expression value for the expression based value resolver.
* @param expectedType The expected type of the expression resolution
* @return The expression based {@link ValueResolver}
*/
public ValueResolver getExpressionBasedValueResolver(String expression, Class expectedType) {
Stack<StackableType> stackableTypes = types.get();
StackableType stackableType = stackableTypes.pop();
StackableType.ExpressionBasedResolverFactory resolverFactory = stackableType.getExpressionBasedResolverFactory().orElseThrow(() -> new IllegalStateException(format("Unable to create an Expression Based ValueResolver of '%s' type. No ExpressionBasedResolverFactory was registered for this type.", stackableType.getType().getTypeName())));
ValueResolver resolver = resolverFactory.getResolver(expression, expectedType);
resolver = getWrapperValueResolver(resolver, stackableTypes);
return resolver;
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class StackableTypesValueResolverFactory method getStaticValueResolver.
/**
* Given a static value and the stack of {@link StackableType wrapper types}, iterates it and creates all the required
* {@link ValueResolver} and stacks them is necessary.
*
* @param value The static value
* @return The static {@link ValueResolver}
*/
public ValueResolver getStaticValueResolver(Object value) {
Stack<StackableType> stackableTypes = types.get();
StackableType stackableType = stackableTypes.pop();
StackableType.StaticResolverFactory resolverFactory = stackableType.getStaticResolverFactory().orElseThrow(() -> new IllegalStateException(format("Unable to create an Static ValueResolver of '%s' type. No StaticResolverFactory was registered for this type.", stackableType.getType().getTypeName())));
ValueResolver resolver = resolverFactory.getResolver(value);
resolver = getWrapperValueResolver(resolver, stackableTypes);
return resolver;
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class DefaultExtensionsClient method resolveParameters.
private Map<String, ValueResolver> resolveParameters(Map<String, Object> parameters, CoreEvent event) {
LinkedHashMap<String, ValueResolver> values = new LinkedHashMap<>();
parameters.forEach((name, value) -> {
if (value instanceof ComplexParameter) {
ComplexParameter complex = (ComplexParameter) value;
DefaultObjectBuilder<?> builder = new DefaultObjectBuilder<>(complex.getType());
resolveParameters(complex.getParameters(), event).forEach((propertyName, valueResolver) -> {
try {
initialiseIfNeeded(valueResolver, true, muleContext);
builder.addPropertyResolver(propertyName, valueResolver);
} catch (InitialisationException e) {
throw new MuleRuntimeException(e);
}
});
try {
values.put(name, new StaticValueResolver<>(builder.build(from(event))));
} catch (MuleException e) {
throw new MuleRuntimeException(createStaticMessage(format("Could not construct parameter [%s]", name)), e);
}
} else {
if (value instanceof String && parser.isContainsTemplate((String) value)) {
values.put(name, new ExpressionValueResolver((String) value));
} else {
values.put(name, new StaticValueResolver<>(value));
}
}
});
return values;
}
Aggregations