[core] #6071 - extract GeoJSONOptions conversion from GeoJSONSource conversion
* [core] geojson_options - retain original error message * [core] tests - initial style conversion stub methods * [core] geojsonoptions conversion - initial unit tests * [core] tests - fix forward reference issue * [core] geojsonoptions conversion - unit tests * [core] geojsonoptions conversion - renamed Holder to Value
This commit is contained in:
111
test/src/mbgl/test/conversion_stubs.hpp
Normal file
111
test/src/mbgl/test/conversion_stubs.hpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/util/feature.hpp>
|
||||
#include <mbgl/util/optional.hpp>
|
||||
#include <mbgl/util/variant.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
namespace conversion {
|
||||
|
||||
class Value;
|
||||
using ValueMap = std::map<std::string, Value>;
|
||||
using ValueVector = std::vector<Value>;
|
||||
class Value : public mbgl::variant<std::string, float, bool, ValueMap, ValueVector> {
|
||||
using variant::variant;
|
||||
};
|
||||
|
||||
inline bool isUndefined(const Value&) {
|
||||
//Variant is always intialized
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isArray(const Value& value) {
|
||||
return value.is<ValueVector>();
|
||||
}
|
||||
|
||||
inline std::size_t arrayLength(const Value& value) {
|
||||
return value.get<ValueVector>().size();
|
||||
}
|
||||
|
||||
inline Value arrayMember(const Value& value, std::size_t i) {
|
||||
return value.get<ValueVector>()[i];
|
||||
}
|
||||
|
||||
inline bool isObject(const Value& value) {
|
||||
return value.is<ValueMap>();
|
||||
}
|
||||
|
||||
inline optional<Value> objectMember(const Value& value, const char* key) {
|
||||
auto map = value.get<ValueMap>();
|
||||
auto iter = map.find(key);
|
||||
|
||||
if (iter != map.end()) {
|
||||
return iter->second;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
using EachMemberFn = std::function<optional<Error>(const std::string&, const Value&)>;
|
||||
|
||||
optional<Error> eachMember(const Value& value, EachMemberFn&& fn) {
|
||||
auto map = value.get<ValueMap>();
|
||||
auto iter = map.begin();
|
||||
|
||||
while (iter != map.end()) {
|
||||
optional<Error> result = fn(iter->first, iter->second);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
++iter;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
inline optional<bool> toBool(const Value& value) {
|
||||
if (value.is<bool>()) {
|
||||
return value.get<bool>();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<float> toNumber(const Value& value) {
|
||||
if (value.is<float>()) {
|
||||
return value.get<float>();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
inline optional<std::string> toString(const Value& value) {
|
||||
if (value.is<std::string>()) {
|
||||
return value.get<std::string>();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
inline optional<mbgl::Value> toValue(const Value& value) {
|
||||
if (value.is<bool>()) {
|
||||
return { value.get<bool>() };
|
||||
} else if (value.is<std::string>()) {
|
||||
return { value.get<std::string>() };
|
||||
} else if (value.is<float>()) {
|
||||
return { value.get<float>() };
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace conversion
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
Reference in New Issue
Block a user