Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.github.jamsesso.jsonlogic.evaluator.JsonLogicEvaluationException;
import io.github.jamsesso.jsonlogic.evaluator.JsonLogicEvaluator;
import io.github.jamsesso.jsonlogic.evaluator.JsonLogicExpression;
import io.github.jamsesso.jsonlogic.utils.LocalContext;

import java.util.HashMap;
import java.util.Map;

public class ReduceExpression implements JsonLogicExpression {
Expand Down Expand Up @@ -35,7 +35,7 @@ public Object evaluate(JsonLogicEvaluator evaluator, JsonLogicArray arguments, O
return accumulator;
}

Map<String, Object> context = new HashMap<>();
Map<String, Object> context = new LocalContext(data);
context.put("accumulator", accumulator);

for (Object item : new ArrayLike(maybeArray)) {
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/io/github/jamsesso/jsonlogic/utils/LocalContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.github.jamsesso.jsonlogic.utils;

import java.util.*;

public class LocalContext implements Map<String,Object>{
private final Map<String, Object> delegate = new HashMap<>();
private final Map<?, ?> parent;

public LocalContext(Object parent) {
if( parent instanceof Map) {
this.parent = (Map<?, ?>) parent;
}else{
this.parent = new HashMap<>();
}
}


@Override
public int size() {
return delegate.size() + parent.size();
}

@Override
public boolean isEmpty() {
return delegate.isEmpty() && parent.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return delegate.containsKey(key) || parent.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return delegate.containsValue(value) || parent.containsValue(value);
}

@Override
public Object get(Object key) {
return delegate.get(key) != null ? delegate.get(key) : parent.get(key);
}

@Override
public Object put(String key, Object value) {
return delegate.put(key, value);
}

@Override
public Object remove(Object key) {
return delegate.remove(key);
}

@Override
public void putAll(Map<? extends String, ?> m) {
delegate.putAll(m);
}

@Override
public void clear() {
delegate.clear();
}

@Override
public Set<String> keySet() {
final Set<String> keys = new HashSet<>(delegate.keySet());
for( Object key : parent.keySet()){
keys.add(""+key);
}
return keys;
}

@Override
public Collection<Object> values() {
final List<Object> values = new ArrayList<>(delegate.values());
values.addAll(parent.values());
return values;
}

@Override
public Set<Entry<String, Object>> entrySet() {
final Set<Entry<String, Object>> entries = new HashSet<>(delegate.entrySet());
for( Map.Entry<?, ?> entry : parent.entrySet()){
entries.add(new AbstractMap.SimpleEntry<>(""+entry.getKey(), entry.getValue()));
}
return entries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class ReduceExpressionTests {
Expand All @@ -19,4 +22,39 @@ public void testReduce() throws JsonLogicException {

assertEquals(21.0, result);
}

@Test
public void testReduceWVariableAccess() throws JsonLogicException {
String json = "{\n" +
" \"reduce\": [\n" +
" {\n" +
" \"var\": \"data\"\n" +
" },\n" +
" {\n" +
" \"*\": [\n" +
" {\n" +
" \"var\": \"multiplicator\"\n" +
" },\n" +
" {\n" +
" \"+\": [\n" +
" {\n" +
" \"var\": \"current\"\n" +
" },\n" +
" {\n" +
" \"var\": \"accumulator\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" },\n" +
" 0\n" +
" ]\n" +
"}";
final Map<String,Object> data = new HashMap<>();
data.put("multiplicator", 2);
data.put("data", new int[] {1, 2});
Object result = jsonLogic.apply(json, data);

assertEquals(8.0, result);
}
}