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 @@ -3,6 +3,7 @@
import io.github.jamsesso.jsonlogic.ast.*;
import io.github.jamsesso.jsonlogic.utils.ArrayLike;

import java.lang.reflect.Array;
import java.util.*;

public class JsonLogicEvaluator {
Expand Down Expand Up @@ -62,11 +63,8 @@ public Object evaluate(JsonLogicVariable variable, Object data, String jsonPath)
int index = ((Number) key).intValue();

if (ArrayLike.isEligible(data)) {
ArrayLike list = new ArrayLike(data);

if (index >= 0 && index < list.size()) {
return transform(list.get(index));
}
Object item = indexedValue(data, index);
return item == MISSING ? defaultValue : item;
}

return defaultValue;
Expand Down Expand Up @@ -99,9 +97,29 @@ public Object evaluate(JsonLogicVariable variable, Object data, String jsonPath)
throw new JsonLogicEvaluationException("var first argument must be null, number, or string", jsonPath + "[0]");
}

private static Object indexedValue(Object data, int index) {
if (data.getClass().isArray()) {
if (index < 0 || index >= Array.getLength(data)) {
return MISSING;
}
return transform(Array.get(data, index));
}

List<?> values;
if (data instanceof List<?> && data instanceof RandomAccess) {
values = (List<?>) data;
} else {
values = new ArrayLike(data);
}

if (index < 0 || index >= values.size()) {
return MISSING;
}
return transform(values.get(index));
}

private Object evaluatePartialVariable(String key, Object data, String jsonPath) throws JsonLogicEvaluationException {
if (ArrayLike.isEligible(data)) {
ArrayLike list = new ArrayLike(data);
int index;

try {
Expand All @@ -111,11 +129,7 @@ private Object evaluatePartialVariable(String key, Object data, String jsonPath)
throw new JsonLogicEvaluationException(e, jsonPath);
}

if (index < 0 || index >= list.size()) {
return MISSING;
}

return transform(list.get(index));
return indexedValue(data, index);
}

if (data instanceof Map) {
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/io/github/jamsesso/jsonlogic/VariableTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.junit.Test;

import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -205,6 +207,39 @@ public void emptyVarKey_returnsWholeDataObject() throws JsonLogicException {
assertSame("Should return the same data instance", data, result);
}

@Test
public void indexedVarReadsOnlyTheRequestedListElement() throws JsonLogicException {
class CountingList extends AbstractList<Integer> implements RandomAccess {
int reads;

@Override
public Integer get(int index) {
reads++;
return index;
}

@Override
public int size() {
return 64;
}
}

CountingList items = new CountingList();
assertEquals(42.0, jsonLogic.apply("{\"var\":42}", items));
assertEquals(1, items.reads);

items.reads = 0;
assertEquals(42.0, jsonLogic.apply("{\"var\":\"items.42\"}", map("items", items)));
assertEquals(1, items.reads);
}

@Test
public void indexedPrimitiveArrayHandlesValuesAndMissingIndexes() throws JsonLogicException {
int[] items = {10, 20};
assertEquals(20.0, jsonLogic.apply("{\"var\":1}", items));
assertEquals("missing", jsonLogic.apply("{\"var\":[\"items.2\",\"missing\"]}", map("items", items)));
}

/** Helper to make small maps concisely. */
private static Map<String, Object> map(Object... kv) {
Map<String, Object> m = new HashMap<>();
Expand Down