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

View File

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

View File

@ -222,7 +222,7 @@ int main(int /*argc*/, char * /*argv*/[])
// Ignore, use old version of repo.
}
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)
{