[core] Stringify expression syntax, not function syntax

This commit is contained in:
John Firebaugh
2018-02-19 13:09:31 -08:00
parent be2fae838c
commit 2912a1423d
2 changed files with 19 additions and 137 deletions

View File

@@ -290,138 +290,19 @@ void stringify(Writer& writer, const Undefined&) {
writer.Null();
}
template <class Writer>
void stringify(Writer& writer, const CategoricalValue& v) {
CategoricalValue::visit(v, [&] (const auto& v_) { stringify(writer, v_); });
}
template <class Writer>
class StringifyStops {
public:
Writer& writer;
template <class T>
void operator()(const ExponentialStops<T>& f) {
writer.Key("type");
writer.String("exponential");
writer.Key("base");
writer.Double(f.base);
writer.Key("stops");
stringifyStops(f.stops);
}
template <class T>
void operator()(const IntervalStops<T>& f) {
writer.Key("type");
writer.String("interval");
writer.Key("stops");
stringifyStops(f.stops);
}
template <class T>
void operator()(const CategoricalStops<T>& f) {
writer.Key("type");
writer.String("categorical");
writer.Key("stops");
stringifyStops(f.stops);
}
template <class T>
void operator()(const IdentityStops<T>&) {
writer.Key("type");
writer.String("identity");
}
template <class T>
void operator()(const CompositeExponentialStops<T>& f) {
writer.Key("type");
writer.String("exponential");
writer.Key("base");
writer.Double(f.base);
writer.Key("stops");
stringifyCompositeStops(f.stops);
}
template <class T>
void operator()(const CompositeIntervalStops<T>& f) {
writer.Key("type");
writer.String("interval");
writer.Key("stops");
stringifyCompositeStops(f.stops);
}
template <class T>
void operator()(const CompositeCategoricalStops<T>& f) {
writer.Key("type");
writer.String("categorical");
writer.Key("stops");
stringifyCompositeStops(f.stops);
}
private:
template <class K, class V>
void stringifyStops(const std::map<K, V>& stops) {
writer.StartArray();
for (const auto& stop : stops) {
writer.StartArray();
stringify(writer, stop.first);
stringify(writer, stop.second);
writer.EndArray();
}
writer.EndArray();
}
template <class InnerStops>
void stringifyCompositeStops(const std::map<float, InnerStops>& stops) {
writer.StartArray();
for (const auto& outer : stops) {
for (const auto& inner : outer.second) {
writer.StartArray();
writer.StartObject();
writer.Key("zoom");
writer.Double(outer.first);
writer.Key("value");
stringify(writer, inner.first);
writer.EndObject();
stringify(writer, inner.second);
writer.EndArray();
}
}
writer.EndArray();
}
};
template <class Writer, class T>
void stringify(Writer& writer, const CameraFunction<T>& f) {
writer.StartObject();
CameraFunction<T>::Stops::visit(f.stops, StringifyStops<Writer> { writer });
writer.EndObject();
void stringify(Writer& writer, const CameraFunction<T>& fn) {
stringify(writer, fn.getExpression().serialize());
}
template <class Writer, class T>
void stringify(Writer& writer, const SourceFunction<T>& f) {
writer.StartObject();
writer.Key("property");
writer.String(f.property);
SourceFunction<T>::Stops::visit(f.stops, StringifyStops<Writer> { writer });
if (f.defaultValue) {
writer.Key("default");
stringify(writer, *f.defaultValue);
}
writer.EndObject();
void stringify(Writer& writer, const SourceFunction<T>& fn) {
stringify(writer, fn.getExpression().serialize());
}
template <class Writer, class T>
void stringify(Writer& writer, const CompositeFunction<T>& f) {
writer.StartObject();
writer.Key("property");
writer.String(f.property);
CompositeFunction<T>::Stops::visit(f.stops, StringifyStops<Writer> { writer });
if (f.defaultValue) {
writer.Key("default");
stringify(writer, *f.defaultValue);
}
writer.EndObject();
void stringify(Writer& writer, const CompositeFunction<T>& fn) {
stringify(writer, fn.getExpression().serialize());
}
template <class Writer, class T>

View File

@@ -81,22 +81,22 @@ TEST(Stringify, Filter) {
TEST(Stringify, CameraFunction) {
ASSERT_EQ(stringify(CameraFunction<float>(ExponentialStops<float> { {{0, 1}}, 2 })),
"{\"type\":\"exponential\",\"base\":2.0,\"stops\":[[0.0,1.0]]}");
"[\"interpolate\",[\"exponential\",2.0],[\"zoom\"],0.0,1.0]");
ASSERT_EQ(stringify(CameraFunction<float>(IntervalStops<float> { {{0, 1}} })),
"{\"type\":\"interval\",\"stops\":[[0.0,1.0]]}");
"[\"step\",[\"zoom\"],0.0,1.0]");
}
TEST(Stringify, SourceFunction) {
ASSERT_EQ(stringify(SourceFunction<float>("property", ExponentialStops<float> { {{0, 1}}, 2 })),
"{\"property\":\"property\",\"type\":\"exponential\",\"base\":2.0,\"stops\":[[0.0,1.0]]}");
"[\"interpolate\",[\"exponential\",2.0],[\"number\",[\"get\",\"property\"]],0.0,1.0]");
ASSERT_EQ(stringify(SourceFunction<float>("property", IntervalStops<float> { {{0, 1}} })),
"{\"property\":\"property\",\"type\":\"interval\",\"stops\":[[0.0,1.0]]}");
"[\"step\",[\"number\",[\"get\",\"property\"]],0.0,1.0]");
ASSERT_EQ(stringify(SourceFunction<float>("property", CategoricalStops<float> { {{CategoricalValue(true), 1}} })),
"{\"property\":\"property\",\"type\":\"categorical\",\"stops\":[[true,1.0]]}");
"[\"case\",[\"boolean\",[\"get\",\"property\"]],1.0,[\"error\"]]");
ASSERT_EQ(stringify(SourceFunction<float>("property", IdentityStops<float> {})),
"{\"property\":\"property\",\"type\":\"identity\"}");
"[\"number\",[\"get\",\"property\"]]");
ASSERT_EQ(stringify(SourceFunction<float>("property", IdentityStops<float> {}, 0.0f)),
"{\"property\":\"property\",\"type\":\"identity\",\"default\":0.0}");
"[\"number\",[\"get\",\"property\"]]");
}
TEST(Stringify, CompositeFunction) {
@@ -108,16 +108,17 @@ TEST(Stringify, CompositeFunction) {
},
2
}, 0.0f)),
"{\"property\":\"property\",\"type\":\"exponential\",\"base\":2.0,"
"\"stops\":["
"[{\"zoom\":0.0,\"value\":0.0},1.0],"
"[{\"zoom\":1.0,\"value\":0.0},1.0]],\"default\":0.0}");
"[\"interpolate\","
"[\"exponential\",1.0],"
"[\"zoom\"],"
"0.0,[\"interpolate\",[\"exponential\",2.0],[\"number\",[\"get\",\"property\"]],0.0,1.0],"
"1.0,[\"interpolate\",[\"exponential\",2.0],[\"number\",[\"get\",\"property\"]],0.0,1.0]]");
}
TEST(Stringify, PropertyValue) {
ASSERT_EQ(stringify(PropertyValue<float>(1)), "1.0");
ASSERT_EQ(stringify(PropertyValue<float>(CameraFunction<float>(ExponentialStops<float> { {{0, 1}}, 2 }))),
"{\"type\":\"exponential\",\"base\":2.0,\"stops\":[[0.0,1.0]]}");
"[\"interpolate\",[\"exponential\",2.0],[\"zoom\"],0.0,1.0]");
}
TEST(Stringify, Layout) {