Skip to content

Commit

Permalink
Closes #56: Fixed error handling for non-list/array entries in source…
Browse files Browse the repository at this point in the history
… list
  • Loading branch information
jaccomoc committed Jan 10, 2024
1 parent 85fdd5d commit f187866
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/jactl/runtime/Reducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private int sizeOf(Object object) {
if (object.getClass().isArray()) {
return Array.getLength(object);
}
throw new RuntimeError("Entries in source list must all be lists for transpose()", source, offset);
throw new RuntimeError("Entries in source list must be lists for transpose()", source, offset);
}

private Object listGet(Object object, int idx) {
Expand All @@ -293,6 +293,6 @@ private Object listGet(Object object, int idx) {
if (object.getClass().isArray()) {
return Array.get(object, idx);
}
throw new RuntimeError("Entries in source list must all be lists for transpose()", source, offset);
throw new RuntimeError("Entries in source list must be lists for transpose()", source, offset);
}
}
6 changes: 3 additions & 3 deletions src/main/java/io/jactl/runtime/RuntimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ public static Object storeMapField(Object value, Map parent, Object field, Strin
* @return an iterator that iterates over the object
*/
public static JactlIterator createIteratorFlatMap(Object obj) {
JactlIterator iter = doCreateIterator(obj);
JactlIterator iter = createIteratorOrNull(obj);
if (iter != null) {
return iter;
}
Expand All @@ -1921,7 +1921,7 @@ public static JactlIterator createIteratorFlatMap(Object obj) {
}

public static JactlIterator createIterator(Object obj) {
JactlIterator iter = doCreateIterator(obj);
JactlIterator iter = createIteratorOrNull(obj);
if (iter != null) {
return iter;
}
Expand All @@ -1930,7 +1930,7 @@ public static JactlIterator createIterator(Object obj) {
throw new IllegalStateException("Internal error: unexpected type " + obj.getClass().getName() + " for iterable");
}

private static JactlIterator doCreateIterator(Object obj) {
public static JactlIterator createIteratorOrNull(Object obj) {
if (obj instanceof JactlIterator) { return (JactlIterator)obj; }
if (obj instanceof List) { return JactlIterator.of((List)obj); }
if (obj instanceof Map) { return JactlIterator.of((Map)obj); }
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/jactl/runtime/TranposeIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public class TranposeIterator extends JactlIterator {
int i = 0;
for (Object input: inputs) {
i++;
JactlIterator iter = RuntimeUtils.createIterator(input);
JactlIterator iter = RuntimeUtils.createIteratorOrNull(input);
if (iter == null) {
throw new RuntimeError(Utils.nth(i) + " input to zip() is not iterable (type is " + RuntimeUtils.className(input) + ")", source, offset);
throw new RuntimeError(Utils.nth(i) + " input to transpose() is invalid. Must be List/array (type is " + RuntimeUtils.className(input) + ")", source, offset);
}
inputIters.add(iter);
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/io/jactl/BuiltinFunctionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ public class BuiltinFunctionTests extends BaseTest {

@Test public void listTranspose() {
test("[].transpose()", Utils.listOf());
testError("[{ -> 1 }].transpose()", "must be list");
testError("[{ -> 1 }].map().transpose()", "must be list");
testError("[1].transpose()", "must be list");
testError("[1].map().transpose()", "must be list");
testError("['abc'].transpose()", "must be list");
testError("['abc'].map().transpose()", "must be list");
test("[[1]].transpose()", Utils.listOf(Utils.listOf(1)));
test("[[1],[2]].transpose()", Utils.listOf(Utils.listOf(1,2)));
test("[[1],[]].transpose()", Utils.listOf(Utils.listOf(1,null)));
Expand Down

0 comments on commit f187866

Please sign in to comment.