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

Raw expression support #12007

Merged
merged 1 commit into from
May 28, 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 @@ -5,8 +5,10 @@
import android.support.annotation.Nullable;
import android.support.annotation.Size;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonPrimitive;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
Expand Down Expand Up @@ -210,7 +212,8 @@ public static Expression literal(@NonNull Object[] array) {
* @return the color expression
*/
public static Expression color(@ColorInt int color) {
return toColor(literal(PropertyFactory.colorToRgbaString(color)));
int[] rgba = PropertyFactory.colorToRgbaArray(color);
return rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
}

/**
Expand Down Expand Up @@ -2426,6 +2429,7 @@ public static Expression ceil(Expression expression) {
* );
* }
* </pre>
*
* @param number number to get value from
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-abs">Style specification</a>
Expand Down Expand Up @@ -3452,6 +3456,10 @@ private Object toValue(ExpressionLiteral expressionValue) {

/**
* Returns a string representation of the object that matches the definition set in the style specification.
* <p>
* If this expression contains a coma (,) delimited literal, like 'rgba(r, g, b, a)`,
* it will be enclosed with double quotes (").
* </p>
*
* @return a string representation of the object.
*/
Expand All @@ -3463,7 +3471,17 @@ public String toString() {
for (Object argument : arguments) {
builder.append(", ");
if (argument instanceof ExpressionLiteral) {
builder.append(((ExpressionLiteral) argument).toValue());
Object literalValue = ((ExpressionLiteral) argument).toValue();

// special case for handling unusual input like 'rgba(r, g, b, a)'
if (literalValue instanceof String) {
if (((String) literalValue).contains(",")) {
builder.append("\"").append(literalValue).append("\"");
continue;
}
}

builder.append(literalValue);
} else {
builder.append(argument.toString());
}
Expand All @@ -3473,6 +3491,26 @@ public String toString() {
return builder.toString();
}

/**
* Returns a DSL equivalent of a raw expression.
* <p>
* If your raw expression contains a coma (,) delimited literal it has to be enclosed with double quotes ("),
* for example
* </p>
* <pre>
* {@code
* ["to-color", "rgba(255, 0, 0, 255)"]
* }
* </pre>
*
* @param rawExpression the raw expression
* @return the resulting expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/">Style specification</a>
*/
public static Expression raw(@NonNull String rawExpression) {
return Converter.convert(rawExpression);
}

/**
* Indicates whether some other object is "equal to" this one.
*
Expand Down Expand Up @@ -3527,6 +3565,11 @@ public static class ExpressionLiteral extends Expression {
* @param object the object to be treated as literal
*/
public ExpressionLiteral(@NonNull Object object) {
if (object instanceof String) {
object = unwrapStringLiteral((String) object);
} else if (object instanceof Number) {
object = ((Number) object).floatValue();
Copy link
Member Author

@LukasPaczos LukasPaczos May 28, 2018

Choose a reason for hiding this comment

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

Noting, that in order to make serialization/deserialization and testing of raw expressions easier I'm converting every Number literal to a Float to keep it consistent since JSON format isn't integer-aware. This forced adjustments in a lot of test cases.

}
this.literal = object;
}

Expand Down Expand Up @@ -3583,6 +3626,15 @@ public int hashCode() {
result = 31 * result + (literal != null ? literal.hashCode() : 0);
return result;
}

private String unwrapStringLiteral(String value) {
if (value.length() > 1 &&
value.charAt(0) == '\"' && value.charAt(value.length() - 1) == '\"') {
return value.substring(1, value.length() - 1);
} else {
return value;
}
}
}

/**
Expand Down Expand Up @@ -3652,10 +3704,12 @@ static Expression[] toExpressionArray(Stop... stops) {
}

/**
* Converts a JsonArray to an expression.
* Converts a JsonArray or a raw expression to a Java expression.
*/
public final static class Converter {

private static final Gson gson = new Gson();

/**
* Converts a JsonArray to an expression
*
Expand All @@ -3677,6 +3731,8 @@ public static Expression convert(@NonNull JsonArray jsonArray) {
arguments.add(convert((JsonArray) jsonElement));
} else if (jsonElement instanceof JsonPrimitive) {
arguments.add(convert((JsonPrimitive) jsonElement));
} else if (jsonElement instanceof JsonNull) {
arguments.add(new Expression.ExpressionLiteral(""));
} else {
throw new RuntimeException("Unsupported expression conversion for " + jsonElement.getClass());
}
Expand All @@ -3701,6 +3757,17 @@ private static Expression convert(@NonNull JsonPrimitive jsonPrimitive) {
throw new RuntimeException("Unsupported literal expression conversion for " + jsonPrimitive.getClass());
}
}

/**
* Converts a raw expression to a DSL equivalent.
*
* @param rawExpression the raw expression to convert
* @return the resulting expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/">Style specification</a>
*/
public static Expression convert(@NonNull String rawExpression) {
return convert(gson.fromJson(rawExpression, JsonArray.class));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2375,9 +2375,24 @@ public static PropertyValue<Expression> textOptional(Expression value) {
return new LayoutPropertyValue<>("text-optional", value);
}

public static String colorToRgbaString(@ColorInt int value) {
return String.format(Locale.US,"rgba(%d, %d, %d, %d)",
(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF, (value >> 24) & 0xFF);
/**
* Converts Android color int to "rbga(r, g, b, a)" String equivalent.
*
* @param color Android color int
* @return String rgba color
*/
public static String colorToRgbaString(@ColorInt int color) {
return String.format(Locale.US, "rgba(%d, %d, %d, %d)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF);
}

/**
* Converts Android color int to rgba int array.
*
* @param color Android color int
* @return int rgba array
*/
public static int[] colorToRgbaArray(@ColorInt int color) {
return new int[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,24 @@ public class PropertyFactory {
}

<% } -%>
public static String colorToRgbaString(@ColorInt int value) {
return String.format(Locale.US,"rgba(%d, %d, %d, %d)",
(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF, (value >> 24) & 0xFF);
/**
* Converts Android color int to "rbga(r, g, b, a)" String equivalent.
*
* @param color Android color int
* @return String rgba color
*/
public static String colorToRgbaString(@ColorInt int color) {
return String.format(Locale.US, "rgba(%d, %d, %d, %d)",
(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF);
}

/**
* Converts Android color int to rgba int array.
*
* @param color Android color int
* @return int rgba array
*/
public static int[] colorToRgbaArray(@ColorInt int color) {
return new int[] {(color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, (color >> 24) & 0xFF};
}
}
Loading