Search in sources :

Example 11 with AviatorString

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

the class PrintlnFunctionUnitTest method testCall_WithOneArgument.

@Test
public void testCall_WithOneArgument() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    System.setOut(new PrintStream(out));
    this.fun.call(null, new AviatorString("hello"));
    out.flush();
    out.close();
    String lineSeparator = System.getProperty("line.separator");
    byte[] data = out.toByteArray();
    assertEquals("hello" + lineSeparator, new String(data));
}
Also used : PrintStream(java.io.PrintStream) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 12 with AviatorString

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

the class SeqFilterFunctionUnitTest method testFilter_map.

@Test
public void testFilter_map() {
    Map<Integer, String> data = new HashMap<>();
    for (int i = 0; i < 5; i++) {
        data.put(i, "a" + i);
    }
    Map<String, Object> env = new HashMap<>();
    SeqPredicateFunction predicate = new SeqPredicateFunction("eq_temp_1", OperatorType.EQ, new AviatorString("a1"), new AviatorString("value"));
    env.putAll(AviatorEvaluator.FUNC_MAP);
    env.put("eq_temp_1", predicate);
    SeqFilterFunction fun = new SeqFilterFunction();
    AviatorObject result = fun.call(env, AviatorRuntimeJavaType.valueOf(data), new AviatorJavaType("eq_temp_1"));
    Map map = ((Map) result.getValue(null));
    assertEquals(1, map.size());
    for (Object value : map.values()) {
        assertEquals("a1", value.toString());
    }
}
Also used : HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with AviatorString

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

the class SeqPredicateFunctionUnitTest method testPredicate_property.

@Test
public void testPredicate_property() {
    Map<String, String> data = new HashMap<>();
    for (int i = 0; i < 5; i++) {
        data.put("key" + i, "value" + i);
    }
    SeqPredicateFunction predicate = new SeqPredicateFunction("eq_temp_1", OperatorType.EQ, new AviatorString("value1"), new AviatorString("key1"));
    AviatorObject result = predicate.call(null, AviatorRuntimeJavaType.valueOf(data));
    assertTrue(result.booleanValue(null));
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) HashMap(java.util.HashMap) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) AviatorString(com.googlecode.aviator.runtime.type.AviatorString) Test(org.junit.Test)

Example 14 with AviatorString

use of com.googlecode.aviator.runtime.type.AviatorString 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 15 with AviatorString

use of com.googlecode.aviator.runtime.type.AviatorString 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)

Aggregations

AviatorString (com.googlecode.aviator.runtime.type.AviatorString)33 Test (org.junit.Test)19 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)14 HashMap (java.util.HashMap)11 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 AbstractFunction (com.googlecode.aviator.runtime.function.AbstractFunction)3 Date (java.util.Date)3 List (java.util.List)3 Map (java.util.Map)3 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)2 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)2 AviatorPattern (com.googlecode.aviator.runtime.type.AviatorPattern)2 PrintStream (java.io.PrintStream)2 Collection (java.util.Collection)2 LinkedList (java.util.LinkedList)2 ID (cn.devezhao.persist4j.engine.ID)1 AviatorEvaluator (com.googlecode.aviator.AviatorEvaluator)1 AviatorNil (com.googlecode.aviator.runtime.type.AviatorNil)1 AviatorNumber (com.googlecode.aviator.runtime.type.AviatorNumber)1