Search in sources :

Example 51 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqReduceFunctionUnitTest method testReduce_Map.

@Test
public void testReduce_Map() {
    Integer[] a = new Integer[10];
    for (int i = 0; i < a.length; i++) {
        a[i] = i;
    }
    SeqReduceFunction fun = new SeqReduceFunction();
    AviatorObject result = fun.call(this.env, AviatorRuntimeJavaType.valueOf(a), new AviatorJavaType("+"), AviatorRuntimeJavaType.valueOf(0));
    assertNotNull(result);
    assertEquals(45, result.getValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) Test(org.junit.Test)

Example 52 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project aviatorscript by killme2008.

the class SeqReduceFunctionUnitTest method testReduce_Array.

@Test
public void testReduce_Array() {
    final Map<String, String> m = new HashMap<String, String>();
    m.put("a", "1");
    m.put("b", "2");
    SeqReduceFunction fun = new SeqReduceFunction();
    AviatorObject result = fun.call(this.env, AviatorRuntimeJavaType.valueOf(m), (AviatorObject) AviatorEvaluator.execute("lambda(x, y) -> x + long(y.value) end"), AviatorRuntimeJavaType.valueOf(5));
    assertNotNull(result);
    assertEquals(8, result.getValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 53 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project graphql-calculator by graphql-calculator.

the class ListMapper method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject listNameObj, AviatorObject expressionObj) {
    String expression = ((AviatorString) expressionObj).getLexeme(Collections.emptyMap());
    if (expression == null || expression.isEmpty()) {
        return AviatorNil.NIL;
    }
    String listPath = ((AviatorString) listNameObj).getLexeme(Collections.emptyMap());
    List<Object> listValue = (List<Object>) AviatorEvaluator.execute(listPath, env);
    if (listValue == null) {
        return AviatorNil.NIL;
    }
    Map<Object, Object> result = listValue.stream().collect(Collectors.toMap(Function.identity(), ele -> {
        HashMap<String, Object> expEnv = new HashMap<>(env);
        expEnv.put("ele", ele);
        return AviatorEvaluator.execute(expression, expEnv, true);
    }));
    return AviatorRuntimeJavaType.valueOf(result);
}
Also used : AviatorString(com.googlecode.aviator.runtime.type.AviatorString) List(java.util.List) AbstractFunction(com.googlecode.aviator.runtime.function.AbstractFunction) AviatorEvaluator(com.googlecode.aviator.AviatorEvaluator) AviatorNil(com.googlecode.aviator.runtime.type.AviatorNil) Map(java.util.Map) HashMap(java.util.HashMap) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Collections(java.util.Collections) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) AviatorRuntimeJavaType(com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType) HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) List(java.util.List) AviatorString(com.googlecode.aviator.runtime.type.AviatorString)

Example 54 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project graphql-calculator by graphql-calculator.

the class ToMap method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject listElement, AviatorObject fieldNameObj) {
    String fieldName = ((AviatorString) fieldNameObj).getLexeme(Collections.emptyMap());
    Collection<Object> list = (Collection<Object>) listElement.getValue(Collections.emptyMap());
    if (list == null || list.isEmpty()) {
        return AviatorRuntimeJavaType.valueOf(Collections.emptyMap());
    }
    Map<Object, Object> result = new LinkedHashMap<>();
    for (Object ele : list) {
        Map<String, Object> objectMap = toMap(ele);
        result.putIfAbsent(objectMap.get(fieldName), objectMap);
    }
    return AviatorRuntimeJavaType.valueOf(result);
}
Also used : AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Collection(java.util.Collection) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) LinkedHashMap(java.util.LinkedHashMap)

Example 55 with AviatorObject

use of com.googlecode.aviator.runtime.type.AviatorObject in project rebuild by getrebuild.

the class DateAddFunction method call.

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
    Object o = arg1.getValue(env);
    final Date $date = o instanceof Date ? (Date) o : CalendarUtils.parse(o.toString());
    if ($date == null) {
        return AviatorNil.NIL;
    }
    String $number = arg2.getValue(env) == null ? null : arg2.getValue(env).toString();
    if ($number == null) {
        return AviatorNil.NIL;
    }
    String $du = arg3.getValue(env) == null ? null : arg3.getValue(env).toString();
    // compatible: v2.8
    String numberLast = $number.substring($number.length() - 1);
    if (!NumberUtils.isNumber(numberLast)) {
        $du = numberLast;
        $number = $number.substring(0, $number.length() - 1);
    }
    // default
    int du4cal = Calendar.DATE;
    if (AviatorDate.DU_MINUTE.equalsIgnoreCase($du))
        du4cal = Calendar.MINUTE;
    else if (AviatorDate.DU_HOUR.equalsIgnoreCase($du))
        du4cal = Calendar.HOUR_OF_DAY;
    else if (AviatorDate.DU_MONTH.equalsIgnoreCase($du))
        du4cal = Calendar.MONTH;
    else if (AviatorDate.DU_YEAR.equalsIgnoreCase($du))
        du4cal = Calendar.YEAR;
    Date newDate = dateAdd($date, ObjectUtils.toInt($number), du4cal);
    return new AviatorDate(newDate);
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Date(java.util.Date)

Aggregations

AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)110 Test (org.junit.Test)51 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)25 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)22 AviatorFunction (com.googlecode.aviator.runtime.type.AviatorFunction)16 HashMap (java.util.HashMap)12 Map (java.util.Map)11 List (java.util.List)10 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)9 FunctionNotFoundException (com.googlecode.aviator.exception.FunctionNotFoundException)7 Env (com.googlecode.aviator.utils.Env)6 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)5 LinkedList (java.util.LinkedList)5 Collection (java.util.Collection)4 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)3 AviatorRuntimeJavaType (com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType)3 Sequence (com.googlecode.aviator.runtime.type.Sequence)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3