From 8635dab4c38fcd67962819224093d0be95f5ed43 Mon Sep 17 00:00:00 2001 From: Chris Loer Date: Thu, 8 Feb 2018 15:23:29 -0800 Subject: [core] Implement Expression::serialize() Issue #10714 - Each expression stores its operator as a string, and default serialization is [operator, serialize(child1), ...] - Custom implementations of `serialize` for Expression types that don't follow the pattern - expression::Value -> mbgl::Value converter - node_expression bindings to expose `serialize` --- platform/node/src/node_expression.cpp | 27 +++++++++++++++++++-------- platform/node/src/node_expression.hpp | 3 +++ platform/node/test/expression.test.js | 4 +++- 3 files changed, 25 insertions(+), 9 deletions(-) (limited to 'platform') diff --git a/platform/node/src/node_expression.cpp b/platform/node/src/node_expression.cpp index 8958d5c6c7..84515060a3 100644 --- a/platform/node/src/node_expression.cpp +++ b/platform/node/src/node_expression.cpp @@ -1,5 +1,6 @@ #include "node_conversion.hpp" #include "node_expression.hpp" +#include "node_feature.hpp" #include #include @@ -24,6 +25,8 @@ void NodeExpression::Init(v8::Local target) { Nan::SetPrototypeMethod(tpl, "isFeatureConstant", IsFeatureConstant); Nan::SetPrototypeMethod(tpl, "isZoomConstant", IsZoomConstant); + Nan::SetPrototypeMethod(tpl, "serialize", Serialize); + Nan::SetMethod(tpl, "parse", Parse); constructor.Reset(tpl->GetFunction()); // what is this doing? @@ -39,31 +42,31 @@ type::Type parseType(v8::Local type) { {"color", type::Color}, {"value", type::Value} }; - + v8::Local v8kind = Nan::Get(type, Nan::New("kind").ToLocalChecked()).ToLocalChecked(); std::string kind(*v8::String::Utf8Value(v8kind)); - + if (kind == "array") { type::Type itemType = parseType(Nan::Get(type, Nan::New("itemType").ToLocalChecked()).ToLocalChecked()->ToObject()); mbgl::optional N; - + v8::Local Nkey = Nan::New("N").ToLocalChecked(); if (Nan::Has(type, Nkey).FromMaybe(false)) { N = Nan::Get(type, Nkey).ToLocalChecked()->ToInt32()->Value(); } return type::Array(itemType, N); } - + return types[kind]; } void NodeExpression::Parse(const Nan::FunctionCallbackInfo& info) { v8::Local cons = Nan::New(constructor); - + if (info.Length() < 1 || info[0]->IsUndefined()) { return Nan::ThrowTypeError("Requires a JSON style expression argument."); } - + mbgl::optional expected; if (info.Length() > 1 && info[1]->IsObject()) { expected = parseType(info[1]->ToObject()); @@ -84,7 +87,7 @@ void NodeExpression::Parse(const Nan::FunctionCallbackInfo& info) { info.GetReturnValue().Set(wrapped); return; } - + v8::Local result = Nan::New(); for (std::size_t i = 0; i < ctx.getErrors().size(); i++) { const auto& error = ctx.getErrors()[i]; @@ -140,7 +143,7 @@ struct ToValue { } return scope.Escape(result); } - + v8::Local operator()(const mbgl::Color& color) { return operator()(std::vector { static_cast(color.r), @@ -227,4 +230,12 @@ void NodeExpression::IsZoomConstant(const Nan::FunctionCallbackInfo& info.GetReturnValue().Set(Nan::New(isZoomConstant(*expression))); } +void NodeExpression::Serialize(const Nan::FunctionCallbackInfo& info) { + NodeExpression* nodeExpr = ObjectWrap::Unwrap(info.Holder()); + const std::unique_ptr& expression = nodeExpr->expression; + + const mbgl::Value serialized = expression->serialize(); + info.GetReturnValue().Set(toJS(serialized)); +} + } // namespace node_mbgl diff --git a/platform/node/src/node_expression.hpp b/platform/node/src/node_expression.hpp index 7af5b7ab51..05af217bde 100644 --- a/platform/node/src/node_expression.hpp +++ b/platform/node/src/node_expression.hpp @@ -32,6 +32,9 @@ private: static void GetType(const Nan::FunctionCallbackInfo&); static void IsFeatureConstant(const Nan::FunctionCallbackInfo&); static void IsZoomConstant(const Nan::FunctionCallbackInfo&); + + static void Serialize(const Nan::FunctionCallbackInfo&); + static Nan::Persistent constructor; std::unique_ptr expression; diff --git a/platform/node/test/expression.test.js b/platform/node/test/expression.test.js index aac039ce18..d7a44abbaa 100644 --- a/platform/node/test/expression.test.js +++ b/platform/node/test/expression.test.js @@ -45,6 +45,9 @@ suite.run('native', {ignores: ignores, tests: tests}, (fixture) => { compiled.isZoomConstant = expression.isZoomConstant(); compiled.type = expression.getType(); + console.log("input: " + JSON.stringify(fixture.expression)); + console.log("output: " + JSON.stringify(expression.serialize())); + const evaluate = fixture.inputs || []; const evaluateResults = []; for (const input of evaluate) { @@ -68,4 +71,3 @@ suite.run('native', {ignores: ignores, tests: tests}, (fixture) => { return result; }); - -- cgit v1.2.1