Search in sources :

Example 1 with FunctionParameter

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);
}
Also used : ExpressionFunction(org.mule.runtime.api.el.ExpressionFunction) DataType(org.mule.runtime.api.metadata.DataType) BindingContext(org.mule.runtime.api.el.BindingContext) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 2 with FunctionParameter

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"));
}
Also used : DefaultFunctionDataType(org.mule.runtime.core.internal.metadata.DefaultFunctionDataType) FunctionDataType(org.mule.runtime.api.metadata.FunctionDataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) Test(org.junit.Test)

Example 3 with FunctionParameter

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));
}
Also used : DefaultFunctionDataType(org.mule.runtime.core.internal.metadata.DefaultFunctionDataType) SimpleDataType(org.mule.runtime.core.internal.metadata.SimpleDataType) FunctionDataType(org.mule.runtime.api.metadata.FunctionDataType) DefaultMapDataType(org.mule.runtime.core.internal.metadata.DefaultMapDataType) DataType(org.mule.runtime.api.metadata.DataType) DefaultCollectionDataType(org.mule.runtime.core.internal.metadata.DefaultCollectionDataType) DefaultFunctionDataType(org.mule.runtime.core.internal.metadata.DefaultFunctionDataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) Test(org.junit.Test)

Example 4 with 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));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) ExpressionFunction(org.mule.runtime.api.el.ExpressionFunction) BindingContext(org.mule.runtime.api.el.BindingContext) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 5 with FunctionParameter

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());
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) DataType(org.mule.runtime.api.metadata.DataType) Defaults(com.google.common.base.Defaults) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) ReflectiveMethodOperationExecutor(org.mule.runtime.module.extension.internal.runtime.operation.ReflectiveMethodOperationExecutor) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) TypedValue(org.mule.runtime.api.metadata.TypedValue) Collectors.toList(java.util.stream.Collectors.toList) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) List(java.util.List) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) MetadataType(org.mule.metadata.api.model.MetadataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) Method(java.lang.reflect.Method) ExtensionMetadataTypeUtils.isTypedValue(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isTypedValue) ComponentExecutorFactory(org.mule.runtime.extension.api.runtime.operation.ComponentExecutorFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MetadataType(org.mule.metadata.api.model.MetadataType) DataType(org.mule.runtime.api.metadata.DataType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter)

Aggregations

FunctionParameter (org.mule.runtime.api.metadata.FunctionParameter)5 Test (org.junit.Test)4 DataType (org.mule.runtime.api.metadata.DataType)4 TypedValue (org.mule.runtime.api.metadata.TypedValue)3 Description (io.qameta.allure.Description)2 BindingContext (org.mule.runtime.api.el.BindingContext)2 ExpressionFunction (org.mule.runtime.api.el.ExpressionFunction)2 FunctionDataType (org.mule.runtime.api.metadata.FunctionDataType)2 DefaultFunctionDataType (org.mule.runtime.core.internal.metadata.DefaultFunctionDataType)2 Defaults (com.google.common.base.Defaults)1 String.format (java.lang.String.format)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 MetadataType (org.mule.metadata.api.model.MetadataType)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)1 FunctionModel (org.mule.runtime.api.meta.model.function.FunctionModel)1 DataType.fromType (org.mule.runtime.api.metadata.DataType.fromType)1 Preconditions.checkArgument (org.mule.runtime.api.util.Preconditions.checkArgument)1