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));
}
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));
}
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);
}
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);
}
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);
}
Aggregations