Commit 5d4534fa by Maarten L. Hekkelman

Added rename_column

Added item_alias
Rename columns in reconstruct
parent f4506438
...@@ -1041,6 +1041,9 @@ class category ...@@ -1041,6 +1041,9 @@ class category
*/ */
void remove_column(std::string_view column_name); void remove_column(std::string_view column_name);
/** @brief Rename column @a from_name to @a to_name */
void rename_column(std::string_view from_name, std::string_view to_name);
/// @brief Return whether a column with name @a name exists in this category /// @brief Return whether a column with name @a name exists in this category
/// @param name The name of the column /// @param name The name of the column
/// @return True if the column exists /// @return True if the column exists
......
...@@ -146,6 +146,16 @@ struct type_validator ...@@ -146,6 +146,16 @@ struct type_validator
int compare(std::string_view a, std::string_view b) const; int compare(std::string_view a, std::string_view b) const;
}; };
/** @brief Item alias, items can be renamed over time
*/
struct item_alias
{
std::string m_name; ///< The alias_name
std::string m_dict; ///< The dictionary in which it was known
std::string m_vers; ///< The version of the dictionary
};
/** /**
* @brief An item_validator binds a type_validator to an item in * @brief An item_validator binds a type_validator to an item in
* a category along with other information found in the dictionary. * a category along with other information found in the dictionary.
...@@ -163,6 +173,7 @@ struct item_validator ...@@ -163,6 +173,7 @@ struct item_validator
cif::iset m_enums; ///< If filled, the set of allowed values cif::iset m_enums; ///< If filled, the set of allowed values
std::string m_default; ///< If filled, a default value for this item std::string m_default; ///< If filled, a default value for this item
category_validator *m_category = nullptr; ///< The category_validator this item_validator belongs to category_validator *m_category = nullptr; ///< The category_validator this item_validator belongs to
std::vector<item_alias> m_aliases; ///< The aliases for this item
/// @brief Compare based on the name /// @brief Compare based on the name
bool operator<(const item_validator &rhs) const bool operator<(const item_validator &rhs) const
...@@ -206,6 +217,9 @@ struct category_validator ...@@ -206,6 +217,9 @@ struct category_validator
/// @brief Return the item_validator for item @a tag, may return nullptr /// @brief Return the item_validator for item @a tag, may return nullptr
const item_validator *get_validator_for_item(std::string_view tag) const; const item_validator *get_validator_for_item(std::string_view tag) const;
/// @brief Return the item_validator for an item that has as alias name @a tag, may return nullptr
const item_validator *get_validator_for_aliased_item(std::string_view tag) const;
}; };
/** /**
......
...@@ -614,6 +614,20 @@ void category::remove_column(std::string_view column_name) ...@@ -614,6 +614,20 @@ void category::remove_column(std::string_view column_name)
} }
} }
void category::rename_column(std::string_view from_name, std::string_view to_name)
{
for (size_t ix = 0; ix < m_columns.size(); ++ix)
{
if (not iequals(from_name, m_columns[ix].m_name))
continue;
m_columns[ix].m_name = to_name;
m_columns[ix].m_validator = m_cat_validator ? m_cat_validator->get_validator_for_item(to_name) : nullptr;
break;
}
}
iset category::get_columns() const iset category::get_columns() const
{ {
iset result; iset result;
......
...@@ -224,6 +224,13 @@ class dictionary_parser : public parser ...@@ -224,6 +224,13 @@ class dictionary_parser : public parser
// } // }
// } // }
std::vector<item_alias> aliases;
for (const auto &[alias_name, dictionary, version] :
dict["item_aliases"].rows<std::string,std::string,std::string>("alias_name", "dictionary", "version"))
{
aliases.emplace_back(alias_name, dictionary, version);
}
// collect the dict from our dataBlock and construct validators // collect the dict from our dataBlock and construct validators
for (auto i : dict["item"]) for (auto i : dict["item"])
{ {
...@@ -245,7 +252,7 @@ class dictionary_parser : public parser ...@@ -245,7 +252,7 @@ class dictionary_parser : public parser
auto vi = find(ivs.begin(), ivs.end(), item_validator{ item_name }); auto vi = find(ivs.begin(), ivs.end(), item_validator{ item_name });
if (vi == ivs.end()) if (vi == ivs.end())
ivs.push_back(item_validator{ item_name, iequals(mandatory, "yes"), tv, ess, defaultValue /*, defaultIsNull*/ }); ivs.push_back(item_validator{ item_name, iequals(mandatory, "yes"), tv, ess, defaultValue, nullptr, std::move(aliases) });
else else
{ {
// need to update the itemValidator? // need to update the itemValidator?
......
...@@ -683,9 +683,6 @@ void comparePolySeqSchemes(datablock &db) ...@@ -683,9 +683,6 @@ void comparePolySeqSchemes(datablock &db)
} }
} }
} }
if (ndb_poly_seq_scheme.empty())
db.erase(std::remove(db.begin(), db.end(), ndb_poly_seq_scheme), db.end());
} }
void reconstruct_pdbx(file &file, std::string_view dictionary) void reconstruct_pdbx(file &file, std::string_view dictionary)
...@@ -733,6 +730,23 @@ void reconstruct_pdbx(file &file, std::string_view dictionary) ...@@ -733,6 +730,23 @@ void reconstruct_pdbx(file &file, std::string_view dictionary)
if (not cv) if (not cv)
continue; continue;
// Start by renaming columns that may have old names based on alias info
for (auto tag : cat.get_columns())
{
auto iv = cv->get_validator_for_item(tag);
if (iv) // know, must be OK then
continue;
iv = cv->get_validator_for_aliased_item(tag);
if (not iv)
continue;
if (cif::VERBOSE > 0)
std::clog << "Renaming " << tag << " to " << iv->m_tag << " in category " << cat.name() << '\n';
cat.rename_column(tag, iv->m_tag);
}
for (auto link : validator.get_links_for_child(cat.name())) for (auto link : validator.get_links_for_child(cat.name()))
{ {
if (link->m_parent_category != "entry") if (link->m_parent_category != "entry")
......
...@@ -256,6 +256,28 @@ const item_validator *category_validator::get_validator_for_item(std::string_vie ...@@ -256,6 +256,28 @@ const item_validator *category_validator::get_validator_for_item(std::string_vie
return result; return result;
} }
const item_validator *category_validator::get_validator_for_aliased_item(std::string_view tag) const
{
const item_validator *result = nullptr;
for (auto &iv : m_item_validators)
{
for (auto &ai : iv.m_aliases)
{
const auto &[cat, name] = split_tag_name(ai.m_name);
if (name == tag and cat == m_name)
{
result = &iv;
break;
}
}
if (result)
break;
}
return result;
}
// -------------------------------------------------------------------- // --------------------------------------------------------------------
void validator::add_type_validator(type_validator &&v) void validator::add_type_validator(type_validator &&v)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment