Prepare for the switch from strings to arrays for tags and receipts.
continuous-integration/drone/push Build is passing Details

Old behavior: Send tags and receipts as a comma separated string.
New behavior: Send them as array of strings.
This commit is contained in:
tastytea 2021-01-21 06:01:37 +01:00
parent 9b06a48f64
commit ed1972a011
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 31 additions and 19 deletions

View File

@ -77,10 +77,24 @@ entry_type parse_formdata()
} }
entry.instance = cgi("instance"); entry.instance = cgi("instance");
if (!cgi("tags").empty()) // Old form.
{
entry.tags = string_to_vector(cgi("tags")); entry.tags = string_to_vector(cgi("tags"));
}
else
{
entry.tags = get_array("tags[]");
}
transform(entry.tags.begin(), entry.tags.end(), entry.tags.begin(), transform(entry.tags.begin(), entry.tags.end(), entry.tags.begin(),
[](const auto &tag) { return tolower(tag); }); [](const auto &tag) { return tolower(tag); });
if (!cgi("receipts").empty()) // Old form.
{
entry.receipts = string_to_vector(cgi("receipts")); entry.receipts = string_to_vector(cgi("receipts"));
}
else
{
entry.receipts = get_array("receipts[]");
}
entry.description = cgi("description"); entry.description = cgi("description");
entry.report_time = time::to_string(system_clock::now()); entry.report_time = time::to_string(system_clock::now());
@ -143,26 +157,26 @@ vector<string> string_to_vector(const string_view str)
return vec; return vec;
} }
vector<string> get_tags() vector<string> get_array(const string &name)
{ {
cgicc::Cgicc cgi; cgicc::Cgicc cgi;
vector<cgicc::FormEntry> tags_form; vector<cgicc::FormEntry> form;
vector<string> tags_string; vector<string> values;
cgi.getElement("tags[]", tags_form); cgi.getElement(name, form);
for (const auto &element : tags_form) for (const auto &element : form)
{ {
const string tag{element.getValue()}; const string value{element.getValue()};
if (!tag.empty()) if (!value.empty())
{ {
const auto new_tags{string_to_vector(tolower(tag))}; const auto new_values{string_to_vector(tolower(value))};
tags_string.insert(tags_string.end(), values.insert(values.end(),
std::make_move_iterator(new_tags.begin()), std::make_move_iterator(new_values.begin()),
std::make_move_iterator(new_tags.end())); std::make_move_iterator(new_values.end()));
} }
} }
return tags_string; return values;
} }
string tolower(const string_view str) string tolower(const string_view str)

View File

@ -39,13 +39,11 @@ using std::vector;
[[nodiscard]] vector<string> string_to_vector(string_view str); [[nodiscard]] vector<string> string_to_vector(string_view str);
/*! /*!
* @brief Read tags from QUERY_STRING or stdin and return it as a vector. * @brief Read array from QUERY_STRING or stdin and return it as a vector.
*
* For use in rss.cpp.
* *
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] vector<string> get_tags(); [[nodiscard]] vector<string> get_array(const string &name);
// Return str in lowercase. // Return str in lowercase.
[[nodiscard]] string tolower(string_view str); [[nodiscard]] string tolower(string_view str);

View File

@ -222,7 +222,7 @@ int main(int /*argc*/, char * /*argv*/[])
// Ignore, use old version of repo. // Ignore, use old version of repo.
} }
const auto entries{files::read_json_files(true)}; const auto entries{files::read_json_files(true)};
write_rss(cout, entries, cgi::get_tags()); write_rss(cout, entries, cgi::get_array("tags[]"));
} }
catch (const exception &e) catch (const exception &e)
{ {