JSValue pushYamlScalarAsJsFunctionOrString(JSScope& jsScope, const YAML::Node& node) { auto value = jsScope.newFunction(node.Scalar()); if (value) { return value; } return jsScope.newString(node.Scalar()); }
void StyleContext::parseSceneGlobals(const YAML::Node& node, const std::string& key, int seqIndex, duk_idx_t dukObject) { switch(node.Type()) { case YAML::NodeType::Scalar: if (key.size() == 0) { duk_push_string(m_ctx, node.Scalar().c_str()); duk_put_prop_index(m_ctx, dukObject, seqIndex); } else { auto nodeValue = node.Scalar(); if (nodeValue.compare(0, 8, "function") == 0) { duk_push_string(m_ctx, key.c_str()); // push property key duk_push_string(m_ctx, nodeValue.c_str()); // push function string duk_push_string(m_ctx, ""); if (duk_pcompile(m_ctx, DUK_COMPILE_FUNCTION) == 0) { // compile function duk_put_prop(m_ctx, -3); // put {key: function()} } else { LOGW("Compile failed in global function: %s\n%s\n---", duk_safe_to_string(m_ctx, -1), nodeValue.c_str()); duk_pop(m_ctx); //pop error duk_pop(m_ctx); //pop key // push property as a string duk_push_string(m_ctx, nodeValue.c_str()); duk_put_prop_string(m_ctx, dukObject, key.c_str()); } } else { duk_push_string(m_ctx, nodeValue.c_str()); duk_put_prop_string(m_ctx, dukObject, key.c_str()); } } break; case YAML::NodeType::Sequence: { auto seqObj = duk_push_array(m_ctx); for (int i = 0; i < node.size(); i++) { parseSceneGlobals(node[i], "", i, seqObj); } duk_put_prop_string(m_ctx, seqObj-1, key.c_str()); break; } case YAML::NodeType::Map: { //duk_push_string(m_ctx, key.c_str()); auto mapObj = duk_push_object(m_ctx); for (auto& mapNode : node) { auto itemKey = mapNode.first.Scalar(); parseSceneGlobals(mapNode.second, itemKey, 0, mapObj); } duk_put_prop_string(m_ctx, mapObj-1, key.c_str()); } default: break; } return; }
Enum enum_from_yaml(const YAML::Node& value, std::true_type /*is_enum_reflectable*/) { if (!value.IsScalar()) throw deserialize_error{"type must be a scalar but is " + yaml_type_name(value)}; if (auto enum_value = kl::from_string<Enum>(value.Scalar())) return enum_value.get(); throw deserialize_error{"invalid enum value: " + value.Scalar()}; }
inline std::string from_yaml(type_t<std::string>, const YAML::Node& value) { if (!value.IsScalar()) throw deserialize_error{"type must be a scalar but is " + yaml_type_name(value)}; return value.Scalar(); }
JSValue parseSceneGlobals(JSScope& jsScope, const YAML::Node& node) { switch(node.Type()) { case YAML::NodeType::Scalar: { auto& scalar = node.Scalar(); if (scalar.compare(0, 8, "function") == 0) { return pushYamlScalarAsJsFunctionOrString(jsScope, node); } return pushYamlScalarAsJsPrimitive(jsScope, node); } case YAML::NodeType::Sequence: { auto jsArray = jsScope.newArray(); for (size_t i = 0; i < node.size(); i++) { jsArray.setValueAtIndex(i, parseSceneGlobals(jsScope, node[i])); } return jsArray; } case YAML::NodeType::Map: { auto jsObject = jsScope.newObject(); for (const auto& entry : node) { if (!entry.first.IsScalar()) { continue; // Can't put non-scalar keys in JS objects. } jsObject.setValueForProperty(entry.first.Scalar(), parseSceneGlobals(jsScope, entry.second)); } return jsObject; } default: return jsScope.newNull(); } }
// Convert a scalar node to a boolean, double, or string (in that order) // and for the first conversion that works, push it to the top of the JS stack. JSValue pushYamlScalarAsJsPrimitive(JSScope& jsScope, const YAML::Node& node) { bool booleanValue = false; double numberValue = 0.; if (YamlUtil::getBool(node, booleanValue)) { return jsScope.newBoolean(booleanValue); } else if (YamlUtil::getDouble(node, numberValue)) { return jsScope.newNumber(numberValue); } else { return jsScope.newString(node.Scalar()); } }
void YamlConfiguration::read_config_doc(const YAML::Node &doc, YamlConfigurationNode *&node) { if (! node) { node = new YamlConfigurationNode("root"); } if (doc.Type() == YAML::NodeType::Map) { #ifdef HAVE_YAMLCPP_0_5 for (YAML::const_iterator it = doc.begin(); it != doc.end(); ++it) { std::string key = it->first.as<std::string>(); #else for (YAML::Iterator it = doc.begin(); it != doc.end(); ++it) { std::string key; it.first() >> key; #endif YamlConfigurationNode *in = node; if (key.find("/") != std::string::npos) { // we need to split and find the proper insertion node std::vector<std::string> pel = str_split(key); for (size_t i = 0; i < pel.size() - 1; ++i) { YamlConfigurationNode *n = (*in)[pel[i]]; if (! n) { n = new YamlConfigurationNode(pel[i]); in->add_child(pel[i], n); } in = n; } key = pel.back(); } YamlConfigurationNode *tmp = (*in)[key]; if (tmp) { #ifdef HAVE_YAMLCPP_0_5 if (tmp->is_scalar() && it->second.Type() != YAML::NodeType::Scalar) #else if (tmp->is_scalar() && it.second().Type() != YAML::NodeType::Scalar) #endif { throw Exception("YamlConfig: scalar %s cannot be overwritten by non-scalar", tmp->name().c_str()); } #ifdef HAVE_YAMLCPP_0_5 tmp->set_scalar(it->second.Scalar()); #else std::string s; if (it.second().GetScalar(s)) { tmp->set_scalar(s); } #endif } else { #ifdef HAVE_YAMLCPP_0_5 YamlConfigurationNode *tmp = new YamlConfigurationNode(key, it->second); in->add_child(key, tmp); read_config_doc(it->second, tmp); #else YamlConfigurationNode *tmp = new YamlConfigurationNode(key, it.second()); in->add_child(key, tmp); read_config_doc(it.second(), tmp); #endif } } } else if (doc.Type() == YAML::NodeType::Scalar) { if (doc.Tag() == "tag:fawkesrobotics.org,cfg/tcp-port" || doc.Tag() == "tag:fawkesrobotics.org,cfg/udp-port") { unsigned int p = 0; try { p = node->get_uint(); } catch (Exception &e) { e.prepend("YamlConfig: Invalid TCP/UDP port number (not an unsigned int)"); throw; } if (p <= 0 || p >= 65535) { throw Exception("YamlConfig: Invalid TCP/UDP port number " "(%u out of allowed range)", p); } } else if (doc.Tag() == "tag:fawkesrobotics.org,cfg/url") { #ifdef HAVE_YAMLCPP_0_5 std::string scalar = doc.Scalar(); #else std::string scalar; doc.GetScalar(scalar); #endif #ifdef USE_REGEX_CPP if (regex_search(scalar, __url_regex)) { # if 0 // just for emacs auto-indentation } # endif #else if (regexec(&__url_regex, scalar.c_str(), 0, NULL, 0) == REG_NOMATCH) { throw Exception("YamlConfig: %s is not a valid URL", scalar.c_str()); } #endif } else if (doc.Tag() == "tag:fawkesrobotics.org,cfg/frame") { #ifdef HAVE_YAMLCPP_0_5 std::string scalar = doc.Scalar(); #else std::string scalar; doc.GetScalar(scalar); #endif #ifdef USE_REGEX_CPP if (regex_search(scalar, __frame_regex)) { # if 0 // just for emacs auto-indentation } # endif #else if (regexec(&__frame_regex, scalar.c_str(), 0, NULL, 0) == REG_NOMATCH) { throw Exception("YamlConfig: %s is not a valid frame ID", scalar.c_str()); } #endif } } }
bool YAML::convert<PolarizationPtr>::decode(const YAML::Node &node, PolarizationPtr &rhs) { rhs->polarization_ = Polarization::FromString(node.Scalar()); rhs->pString_ = Polarization::ToString(rhs->polarization_); return (rhs->polarization_ != Polarization::invalid); }