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

Commit

Permalink
[android] Minimal Android wrapper on 'format' expression
Browse files Browse the repository at this point in the history
For converting to Java types, flatten 'text-field' back to a string.
  • Loading branch information
ChrisLoer committed Sep 25, 2018
1 parent d86741f commit c54a3d8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3224,6 +3224,11 @@ public static Expression collator(Expression caseSensitive, Expression diacritic
return new Expression("collator", new ExpressionMap(map));
}

public static Expression format(Expression input) {
Map<String, Expression> map = new HashMap<>();
return new Expression("format", input, new ExpressionMap(map));
}

/**
* Asserts that the input value is an object. If it is not, the expression is an error
* The asserted input value is returned as result.
Expand Down Expand Up @@ -4441,4 +4446,4 @@ static Object[] toObjectArray(Object object) {
}
return objects;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,8 @@ public void testTextFieldAsConstant() {
assertEquals((String) layer.getTextField().getValue(), (String) "");

layer.setProperties(textField("{token}"));
assertEquals(layer.getTextField().getExpression(), Expression.toString(Expression.get("token")));
assertEquals(layer.getTextField().getExpression(),
Expression.format(Expression.toString(Expression.get("token"))));
});
}

Expand All @@ -490,7 +491,7 @@ public void testTextFieldAsExpression() {
assertNotNull(layer);

// Set and Get
Expression expression = string(Expression.get("undefined"));
Expression expression = Expression.format(string(Expression.get("undefined")));
layer.setProperties(textField(expression));
assertEquals(layer.getTextField().getExpression(), expression);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ public class <%- camelize(type) %>LayerTest extends BaseActivityTest {
<% if (property.tokens) { -%>
layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>("{token}"));
<% if (property.type === 'formatted') { -%>
assertEquals(layer.get<%- camelize(property.name) %>().getExpression(),
Expression.format(Expression.toString(Expression.get("token"))));
<% } else { -%>
assertEquals(layer.get<%- camelize(property.name) %>().getExpression(), Expression.toString(Expression.get("token")));
<% } -%>
<% } -%>
});
}
Expand All @@ -181,7 +186,11 @@ public class <%- camelize(type) %>LayerTest extends BaseActivityTest {
assertNotNull(layer);
// Set and Get
<% if (property.type === 'formatted') { -%>
Expression expression = Expression.format(<%- defaultExpressionJava(property) %>(Expression.get("undefined")));
<% } else { -%>
Expression expression = <%- defaultExpressionJava(property) %>(Expression.get("undefined"));
<% } -%>
layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>(expression));
assertEquals(layer.get<%- camelize(property.name) %>().getExpression(), expression);
});
Expand Down
4 changes: 4 additions & 0 deletions platform/android/src/conversion/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, Color>::o
return jni::Make<jni::String>(env, sstream.str());
}

Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, style::expression::Formatted>::operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const {
return jni::Make<jni::String>(env, value.toString());
}

Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, std::vector<std::string>>::operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const {
auto result = jni::Array<jni::String>::New(env, value.size());

Expand Down
7 changes: 7 additions & 0 deletions platform/android/src/conversion/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>

#include <mbgl/style/expression/formatted.hpp>

#include <jni/jni.hpp>

#include <string>
Expand Down Expand Up @@ -53,6 +55,11 @@ struct Converter<jni::Local<jni::Object<>>, Color> {
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const Color& value) const;
};

template <>
struct Converter<jni::Local<jni::Object<>>, style::expression::Formatted> {
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const;
};

template <std::size_t N>
struct Converter<jni::Local<jni::Object<>>, std::array<float, N>> {
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::array<float, N>& value) const {
Expand Down

0 comments on commit c54a3d8

Please sign in to comment.