use of org.mule.runtime.api.metadata.FunctionParameter in project mule by mulesoft.
the class ExtendedExpressionLanguageAdapterTestCase method globalContext.
@Test
@Description("Verifies that global binding context only work for DW.")
public void globalContext() throws Exception {
ExpressionFunction expressionFunction = new ExpressionFunction() {
private DataType dataType = STRING;
@Override
public Object call(Object[] objects, BindingContext bindingContext) {
return ((String) objects[0]).toUpperCase();
}
@Override
public Optional<DataType> returnType() {
return Optional.of(dataType);
}
@Override
public List<FunctionParameter> parameters() {
return asList(new FunctionParameter("x", dataType));
}
};
String global = "global";
String value = "var";
BindingContext context = BindingContext.builder().addBinding(global, new TypedValue(value, STRING)).addBinding("upper", new TypedValue(expressionFunction, fromFunction(expressionFunction))).build();
expressionLanguageAdapter.addGlobalBindings(context);
assertThat(expressionLanguageAdapter.evaluate(global, testEvent(), emptyBindingContext).getValue(), is(value));
assertThat(expressionLanguageAdapter.evaluate("upper('hey')", testEvent(), emptyBindingContext).getValue(), is("HEY"));
assertThat(expressionLanguageAdapter.evaluate(melify(global), testEvent(), context).getValue(), is(value));
expectedException.expect(ExpressionRuntimeException.class);
expressionLanguageAdapter.evaluate(melify(global), testEvent(), emptyBindingContext);
}
use of org.mule.runtime.api.metadata.FunctionParameter in project mule by mulesoft.
the class DataTypeBuilderTestCase method buildFunction.
@Test
public void buildFunction() {
FunctionDataType dataType = (FunctionDataType) DataType.fromFunction(new SomeFunction());
// Return type
assertThat(dataType.getReturnType().isPresent(), is(true));
assertThat(dataType.getReturnType().get(), equalTo(STRING));
// Parameters
assertThat(dataType.getParameters(), hasSize(2));
FunctionParameter first = dataType.getParameters().get(0);
assertThat(first.getName(), is("fst"));
assertThat(first.getType(), equalTo(NUMBER));
assertThat(first.getDefaultValueResolver(), nullValue());
FunctionParameter second = dataType.getParameters().get(1);
assertThat(second.getName(), is("snd"));
assertThat(second.getType(), equalTo(OBJECT));
// Default
assertThat(second.getDefaultValueResolver().getDefaultValue(builder().build()), is("wow"));
}
use of org.mule.runtime.api.metadata.FunctionParameter in project mule by mulesoft.
the class DataTypeBuilderTestCase method templateFunction.
@Test
public void templateFunction() {
FunctionParameter functionParameter = new FunctionParameter("fst", NUMBER);
final DataType template = DataType.builder().functionType(SomeFunction.class).returnType(STRING).parametersType(singletonList(functionParameter)).build();
final DataType dataType = DataType.builder(template).build();
assertThat(dataType, instanceOf(DefaultFunctionDataType.class));
assertThat(dataType.getType(), is(equalTo(SomeFunction.class)));
assertThat(((DefaultFunctionDataType) dataType).getReturnType().get(), is(STRING));
assertThat(((DefaultFunctionDataType) dataType).getParameters(), hasItems(functionParameter));
}
use of org.mule.runtime.api.metadata.FunctionParameter in project mule by mulesoft.
the class DefaultExpressionManagerTestCase method globals.
@Test
@Description("Verifies that global bindings can be added.")
public void globals() {
DataType integerType = fromType(Integer.class);
ExpressionFunction multiply = new ExpressionFunction() {
@Override
public Object call(Object[] objects, BindingContext bindingContext) {
return ((Integer) objects[0]) * ((Integer) objects[1]);
}
@Override
public Optional<DataType> returnType() {
return of(integerType);
}
@Override
public List<FunctionParameter> parameters() {
return asList(new FunctionParameter("x", integerType), new FunctionParameter("y", integerType));
}
};
BindingContext globalContext = builder().addBinding("aNum", new TypedValue<>(4, fromType(Integer.class))).addBinding("times", new TypedValue<>(multiply, fromFunction(multiply))).build();
expressionManager.addGlobalBindings(globalContext);
TypedValue result = expressionManager.evaluate("aNum times 5");
assertThat(result.getValue(), is(20));
expressionManager.addGlobalBindings(builder().addBinding("otherNum", new TypedValue(3, integerType)).build());
result = expressionManager.evaluate("(times(7, 3) + otherNum) / aNum");
assertThat(result.getValue(), is(6));
}
use of org.mule.runtime.api.metadata.FunctionParameter 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());
}
Aggregations