Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Array expression literal #11457

Merged
merged 1 commit into from
Mar 16, 2018
Merged
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 @@ -115,6 +115,16 @@ public static Expression literal(@NonNull Object object) {
return new ExpressionLiteral(object);
}

/**
* Create a literal array expression
*
* @param array the array
* @return the expression
*/
public static Expression literal(@NonNull Object[] array) {
return new ExpressionArray(array);
}

/**
* Expression literal utility method to convert a color int to an color expression
*
Expand Down Expand Up @@ -1600,7 +1610,7 @@ public static Expression interpolate(@NonNull Interpolator interpolation,
@NonNull Expression number, Stop... stops) {
return interpolate(interpolation, number, Stop.toExpressionArray(stops));
}

/**
* interpolates linearly between the pair of stops just less than and just greater than the input.
*
Expand Down Expand Up @@ -2006,4 +2016,41 @@ private static Expression convert(@NonNull JsonPrimitive jsonPrimitive) {
}
}
}

private static class ExpressionArray extends Expression {

private Object[] array;

ExpressionArray(Object[] array) {
this.array = array;
}

@NonNull
@Override
public Object[] toArray() {
return new Object[] {
"literal", array
};
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder("[\"literal\"], [");
Object argument;
for (int i = 0; i < array.length; i++) {
argument = array[i];
if (argument instanceof String) {
builder.append("\"").append(argument).append("\"");
} else {
builder.append(argument);
}

if (i != array.length - 1) {
builder.append(", ");
}
}
builder.append("]]");
return builder.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,14 @@ public void testHeatmapDensity() throws Exception {

@Test
public void testAt() throws Exception {
Object[] expected = new Object[] {"at", 3, new Object[] {"one", "two"}};
Object[] expected = new Object[] {"at", 3, new Object[] {"literal", new Object[] {"one", "two"}}};
Object[] actual = at(literal(3), literal(new Object[] {"one", "two"})).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testAtLiteral() throws Exception {
Object[] expected = new Object[] {"at", 3, new Object[] {"one", "two"}};
Object[] expected = new Object[] {"at", 3, new Object[] {"literal", new Object[] {"one", "two"}}};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to update this line as this syntax would be required to pass in an Object[] to core

Object[] actual = at(3, literal(new Object[] {"one", "two"})).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
Expand Down Expand Up @@ -955,12 +955,13 @@ public void testStepExpressionLiteral() throws Exception {

@Test
public void testLinear() throws Exception {
Object[] stopZero = new Object[] {0, 1};
Object[] stopOne = new Object[] {1, 2};
Object[] stopTwo = new Object[] {2, 3};
Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12, stopZero, stopOne, stopTwo};
Copy link
Member Author

@tobrun tobrun Mar 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

above construction for stops wasn't correct, stops are not allowed to be nested arrays

Object[] actual = interpolate(linear(), literal(12),
literal(stopZero), literal(stopOne), literal(stopTwo)).toArray();
Object[] expected = new Object[] {"interpolate", new Object[] {"linear"}, 12, 0, 1, 1, 2, 2, 3};
Object[] actual = interpolate(
linear(), literal(12),
literal(0), literal(1),
literal(1), literal(2),
literal(2), literal(3))
.toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

Expand Down Expand Up @@ -1064,4 +1065,20 @@ public void testExpressionExponentialToString() throws Exception {
get(literal("x")), literal(0), literal(100), literal(100), literal(200)).toString();
assertEquals("toString should match", expected, actual);
}

@Test
public void testLiteralArray() throws Exception {
Object[] array = new Object[] {1, "text"};
Object[] expected = new Object[] {"literal", array};
Object[] actual = literal(array).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}

@Test
public void testLiteralArrayString() throws Exception {
Object[] array = new Object[] {1, "text"};
String expected = "[\"literal\"], [1, \"text\"]]";
String actual = literal(array).toString();
assertEquals("literal array should match", expected, actual);
}
}