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

Commit

Permalink
[core] Implement array assertion fallback behavior
Browse files Browse the repository at this point in the history
This was added in gl-js in #7095.
  • Loading branch information
jfirebaugh committed Sep 13, 2018
1 parent c044dd8 commit 2c48b5a
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 174 deletions.
2 changes: 0 additions & 2 deletions cmake/core-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ src/mbgl/style/conversion/tileset.cpp
src/mbgl/style/conversion/transition_options.cpp

# style/expression
include/mbgl/style/expression/array_assertion.hpp
include/mbgl/style/expression/assertion.hpp
include/mbgl/style/expression/at.hpp
include/mbgl/style/expression/boolean_operator.hpp
Expand Down Expand Up @@ -479,7 +478,6 @@ include/mbgl/style/expression/parsing_context.hpp
include/mbgl/style/expression/step.hpp
include/mbgl/style/expression/type.hpp
include/mbgl/style/expression/value.hpp
src/mbgl/style/expression/array_assertion.cpp
src/mbgl/style/expression/assertion.cpp
src/mbgl/style/expression/at.cpp
src/mbgl/style/expression/boolean_operator.cpp
Expand Down
47 changes: 0 additions & 47 deletions include/mbgl/style/expression/array_assertion.hpp

This file was deleted.

1 change: 1 addition & 0 deletions include/mbgl/style/expression/assertion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Assertion : public Expression {

std::vector<optional<Value>> possibleOutputs() const override;

mbgl::Value serialize() const override;
std::string getOperator() const override;

private:
Expand Down
1 change: 0 additions & 1 deletion include/mbgl/style/expression/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ enum class Kind : int32_t {
Coalesce,
CompoundExpression,
Literal,
ArrayAssertion,
At,
Interpolate,
Assertion,
Expand Down
4 changes: 1 addition & 3 deletions src/mbgl/style/conversion/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <mbgl/style/expression/interpolate.hpp>
#include <mbgl/style/expression/match.hpp>
#include <mbgl/style/expression/case.hpp>
#include <mbgl/style/expression/array_assertion.hpp>
#include <mbgl/util/string.hpp>

#include <cassert>
Expand Down Expand Up @@ -679,8 +678,7 @@ optional<std::unique_ptr<Expression>> convertFunctionToExpression(type::Type typ
return toColor(get(literal(*property)));
},
[&] (const type::Array& array) -> optional<std::unique_ptr<Expression>> {
return std::unique_ptr<Expression>(
std::make_unique<ArrayAssertion>(array, get(literal(*property))));
return assertion(array, get(literal(*property)));
},
[&] (const auto&) -> optional<std::unique_ptr<Expression>> {
assert(false); // No properties use this type.
Expand Down
106 changes: 0 additions & 106 deletions src/mbgl/style/expression/array_assertion.cpp

This file was deleted.

79 changes: 73 additions & 6 deletions src/mbgl/style/expression/assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,64 @@ ParseResult Assertion::parse(const Convertible& value, ParsingContext& ctx) {
return ParseResult();
}

auto it = types.find(*toString(arrayMember(value, 0)));
assert(it != types.end());

std::size_t i = 1;
type::Type type;

std::string name = *toString(arrayMember(value, 0));
if (name == "array") {
optional<type::Type> itemType;
if (length > 2) {
optional<std::string> itemTypeName = toString(arrayMember(value, 1));
auto it = itemTypeName ? types.find(*itemTypeName) : types.end();
if (it == types.end() || it->second == type::Object) {
ctx.error(
R"(The item type argument of "array" must be one of string, number, boolean)",
1
);
return ParseResult();
}
itemType = it->second;
i++;
} else {
itemType = {type::Value};
}

optional<std::size_t> N;
if (length > 3) {
auto m = arrayMember(value, 2);
optional<float> n = toNumber(m);
if (!isUndefined(m) &&
(!n || *n < 0 || *n != std::floor(*n))) {
ctx.error(
R"(The length argument to "array" must be a positive integer literal.)",
2
);
return ParseResult();
}
if (n) {
N = optional<std::size_t>(*n);
}
i++;
}

type = type::Array(*itemType, N);
} else {
type = types.at(name);
}

std::vector<std::unique_ptr<Expression>> parsed;
parsed.reserve(length - 1);
for (std::size_t i = 1; i < length; i++) {
for (; i < length; i++) {
ParseResult input = ctx.parse(arrayMember(value, i), i, {type::Value});
if (!input) return ParseResult();
parsed.push_back(std::move(*input));
}

return ParseResult(std::make_unique<Assertion>(it->second, std::move(parsed)));
return ParseResult(std::make_unique<Assertion>(type, std::move(parsed)));
}

std::string Assertion::getOperator() const {
return type::toString(getType());
return getType().is<type::Array>() ? "array" : type::toString(getType());
}

EvaluationResult Assertion::evaluate(const EvaluationContext& params) const {
Expand Down Expand Up @@ -90,6 +132,31 @@ std::vector<optional<Value>> Assertion::possibleOutputs() const {
return result;
}

mbgl::Value Assertion::serialize() const {
std::vector<mbgl::Value> serialized;
serialized.emplace_back(getOperator());

if (getType().is<type::Array>()) {
const auto array = getType().get<type::Array>();
if (array.itemType.is<type::StringType>()
|| array.itemType.is<type::NumberType>()
|| array.itemType.is<type::BooleanType>()) {
serialized.emplace_back(type::toString(array.itemType));
if (array.N) {
serialized.emplace_back(uint64_t(*array.N));
} else if (inputs.size() > 1) {
serialized.emplace_back(mbgl::NullValue());
}
}
}

for (const auto& input : inputs) {
serialized.emplace_back(input->serialize());
}

return serialized;
}

} // namespace expression
} // namespace style
} // namespace mbgl
Expand Down
12 changes: 3 additions & 9 deletions src/mbgl/style/expression/parsing_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/at.hpp>
#include <mbgl/style/expression/array_assertion.hpp>
#include <mbgl/style/expression/assertion.hpp>
#include <mbgl/style/expression/boolean_operator.hpp>
#include <mbgl/style/expression/case.hpp>
Expand Down Expand Up @@ -46,8 +45,7 @@ bool isConstant(const Expression& expression) {
}

bool isTypeAnnotation = expression.getKind() == Kind::Coercion ||
expression.getKind() == Kind::Assertion ||
expression.getKind() == Kind::ArrayAssertion;
expression.getKind() == Kind::Assertion;

bool childrenConstant = true;
expression.eachChild([&](const Expression& child) {
Expand Down Expand Up @@ -105,7 +103,7 @@ const ExpressionRegistry& getExpressionRegistry() {
{"<=", parseComparison},
{"all", All::parse},
{"any", Any::parse},
{"array", ArrayAssertion::parse},
{"array", Assertion::parse},
{"at", At::parse},
{"boolean", Assertion::parse},
{"case", Case::parse},
Expand Down Expand Up @@ -171,14 +169,10 @@ ParseResult ParsingContext::parse(const Convertible& value, TypeAnnotationOption

if (expected) {
const type::Type actual = (*parsed)->getType();
if ((*expected == type::String || *expected == type::Number || *expected == type::Boolean || *expected == type::Object) && actual == type::Value) {
if ((*expected == type::String || *expected == type::Number || *expected == type::Boolean || *expected == type::Object || expected->is<type::Array>()) && actual == type::Value) {
if (typeAnnotationOption == includeTypeAnnotations) {
parsed = { std::make_unique<Assertion>(*expected, array(std::move(*parsed))) };
}
} else if (expected->is<type::Array>() && actual == type::Value) {
if (typeAnnotationOption == includeTypeAnnotations) {
parsed = { std::make_unique<ArrayAssertion>(expected->get<type::Array>(), std::move(*parsed)) };
}
} else if (*expected == type::Color && (actual == type::Value || actual == type::String)) {
if (typeAnnotationOption == includeTypeAnnotations) {
parsed = { std::make_unique<Coercion>(*expected, array(std::move(*parsed))) };
Expand Down

0 comments on commit 2c48b5a

Please sign in to comment.