diff --git a/resource/readers/resource_reader_jgf.cpp b/resource/readers/resource_reader_jgf.cpp index 920999d9d..466a68343 100644 --- a/resource/readers/resource_reader_jgf.cpp +++ b/resource/readers/resource_reader_jgf.cpp @@ -57,7 +57,7 @@ struct fetch_helper_t : public fetch_remap_support_t { int exclusive = -1; resource_pool_t::status_t status = resource_pool_t::status_t::UP; const char *type = NULL; - const char *name = NULL; + std::string name; const char *unit = NULL; const char *basename = NULL; const char *vertex_id = NULL; @@ -119,7 +119,7 @@ void fetch_remap_support_t::clear () const char *fetch_helper_t::get_proper_name () const { - return (is_name_remapped ()) ? get_remapped_name ().c_str () : name; + return (is_name_remapped ()) ? get_remapped_name ().c_str () : name.c_str (); } int64_t fetch_helper_t::get_proper_id () const @@ -141,7 +141,7 @@ void fetch_helper_t::scrub () exclusive = -1; status = resource_pool_t::status_t::UP; type = NULL; - name = NULL; + name.clear (); unit = NULL; basename = NULL; vertex_id = NULL; @@ -341,35 +341,66 @@ int resource_reader_jgf_t::remap_aware_unpack_vtx (fetch_helper_t &f, return 0; } +int resource_reader_jgf_t::apply_defaults (fetch_helper_t &f, const char *name) +{ + if (f.uniq_id == -1) { + try { + f.uniq_id = static_cast (std::stoll (std::string{f.vertex_id})); + } catch (std::invalid_argument const &ex) { + m_err_msg += __FUNCTION__; + m_err_msg += ": value for key (uniq_id) could not be inferred from outer "; + m_err_msg += "'id' field " + std::string (f.vertex_id) + ".\n"; + return -1; + } + } + if (f.id == -1) + f.id = f.uniq_id; + if (f.exclusive == -1) + f.exclusive = 0; + if (f.size == -1) + f.size = 1; + if (f.basename == NULL) + f.basename = f.type; + if (name == NULL) { + f.name = f.basename; + f.name.append (std::to_string (f.uniq_id)); + } else { + f.name = name; + } + if (f.unit == NULL) + f.unit = ""; + return 0; +} + int resource_reader_jgf_t::fill_fetcher (json_t *element, fetch_helper_t &f, json_t **paths, json_t **properties) { - int rc = -1; json_t *metadata = NULL; + const char *name = NULL; if ((json_unpack (element, "{ s:s }", "id", &f.vertex_id) < 0)) { errno = EINVAL; m_err_msg += __FUNCTION__; m_err_msg += ": JGF vertex id key is not found in a node.\n"; - goto done; + return -1; } if ((metadata = json_object_get (element, "metadata")) == NULL) { errno = EINVAL; m_err_msg += __FUNCTION__; m_err_msg += ": key (metadata) is not found in an JGF node for "; m_err_msg += std::string (f.vertex_id) + ".\n"; - goto done; + return -1; } if ((json_unpack (metadata, - "{ s:s s:s s:s s:I s:I s:I s?:i s:b s:s s:I s:o s?o }", + "{ s:s s?s s?s s?I s?I s?I s?i s?b s?s s?I s:o s?o }", "type", &f.type, "basename", &f.basename, "name", - &f.name, + &name, "id", &f.id, "uniq_id", @@ -393,18 +424,16 @@ int resource_reader_jgf_t::fill_fetcher (json_t *element, m_err_msg += __FUNCTION__; m_err_msg += ": malformed metadata in an JGF node for "; m_err_msg += std::string (f.vertex_id) + "\n"; - goto done; + return -1; } if (*properties && !json_is_object (*properties)) { errno = EINVAL; m_err_msg += __FUNCTION__; m_err_msg += ": key (properties) must be an object or null for "; m_err_msg += std::string (f.vertex_id) + ".\n"; - goto done; + return -1; } - rc = 0; -done: - return rc; + return apply_defaults (f, name); } int resource_reader_jgf_t::unpack_vtx (json_t *element, fetch_helper_t &f) diff --git a/resource/readers/resource_reader_jgf.hpp b/resource/readers/resource_reader_jgf.hpp index 29b7afc1f..c93f4f531 100644 --- a/resource/readers/resource_reader_jgf.hpp +++ b/resource/readers/resource_reader_jgf.hpp @@ -126,6 +126,7 @@ class resource_reader_jgf_t : public resource_reader_base_t { int fetch_jgf (const std::string &str, json_t **jgf_p, json_t **nodes_p, json_t **edges_p); int unpack_and_remap_vtx (fetch_helper_t &f, json_t *paths, json_t *properties); int remap_aware_unpack_vtx (fetch_helper_t &f, json_t *paths, json_t *properties); + int apply_defaults (fetch_helper_t &f, const char *name); int fill_fetcher (json_t *element, fetch_helper_t &f, json_t **path, json_t **properties); int unpack_vtx (json_t *element, fetch_helper_t &f); vtx_t create_vtx (resource_graph_t &g, const fetch_helper_t &fetcher);