Search in sources :

Example 6 with MappingVariable

use of com.dtflys.forest.mapping.MappingVariable in project forest by dromara.

the class QueryLifeCycle method onParameterInitialized.

@Override
public void onParameterInitialized(ForestMethod method, MappingParameter parameter, Query annotation) {
    Map<String, Object> attrs = ReflectUtils.getAttributesFromAnnotation(annotation);
    String name = (String) attrs.get("name");
    String filterName = (String) attrs.get("filter");
    String defaultValue = (String) attrs.get("defaultValue");
    if (StringUtils.isNotEmpty(name)) {
        parameter.setName(name);
        MappingVariable variable = new MappingVariable(name, parameter.getType());
        method.processParameterFilter(variable, filterName);
        variable.setIndex(parameter.getIndex());
        method.addVariable(annotation.value(), variable);
        parameter.setObjectProperties(false);
    } else if (CharSequence.class.isAssignableFrom(parameter.getType())) {
        parameter.setName(null);
        parameter.setObjectProperties(false);
    } else {
        parameter.setObjectProperties(true);
    }
    if (StringUtils.isNotEmpty(defaultValue)) {
        parameter.setDefaultValue(defaultValue);
    }
    method.processParameterFilter(parameter, filterName);
    parameter.setTarget(TARGET_QUERY);
    method.addNamedParameter(parameter);
}
Also used : MappingVariable(com.dtflys.forest.mapping.MappingVariable)

Example 7 with MappingVariable

use of com.dtflys.forest.mapping.MappingVariable in project forest by dromara.

the class TestMappingReference method testParameter.

@Test
public void testParameter() {
    ForestMethod forestMethod = Mockito.mock(ForestMethod.class);
    MappingVariable nameVar = new MappingVariable("name", String.class);
    nameVar.setIndex(0);
    MappingVariable ageVar = new MappingVariable("age", String.class);
    ageVar.setIndex(1);
    Mockito.when(forestMethod.getVariable("name")).thenReturn(nameVar);
    Mockito.when(forestMethod.getVariable("age")).thenReturn(ageVar);
    Mockito.when(forestMethod.getVariableValue("name", forestMethod)).thenReturn("Marry");
    Mockito.when(forestMethod.getVariableValue("motherName", forestMethod)).thenReturn("Linda");
    Mockito.when(forestMethod.getVariableValue("age", forestMethod)).thenReturn(12);
    Mockito.when(forestMethod.isVariableDefined("motherName")).thenReturn(true);
    MappingReference nameRef = new MappingReference(forestMethod, forestMethod, "name");
    MappingReference ageRef = new MappingReference(forestMethod, forestMethod, "age");
    MappingReference motherNameRef = new MappingReference(forestMethod, forestMethod, "motherName");
    Assert.assertEquals("Peter", nameRef.render(new Object[] { "Peter", 15 }));
    Mockito.verify(forestMethod).getVariable("name");
    Mockito.verify(forestMethod, Mockito.never()).getVariableValue("name");
    Assert.assertEquals(15, ageRef.render(new Object[] { "Peter", 15 }));
    Mockito.verify(forestMethod, Mockito.never()).getVariableValue("motherName");
    Assert.assertEquals("Linda", motherNameRef.render(new Object[] { "Peter", 15 }));
    Mockito.verify(forestMethod).getVariableValue("motherName", forestMethod);
}
Also used : MappingReference(com.dtflys.forest.mapping.MappingReference) ForestMethod(com.dtflys.forest.reflection.ForestMethod) MappingVariable(com.dtflys.forest.mapping.MappingVariable) Test(org.junit.Test)

Aggregations

MappingVariable (com.dtflys.forest.mapping.MappingVariable)7 MappingReference (com.dtflys.forest.mapping.MappingReference)1 ForestMethod (com.dtflys.forest.reflection.ForestMethod)1 Test (org.junit.Test)1