Skip to content

Commit

Permalink
Revert "Refactor: Use fmt builtin functionality to format variants"
Browse files Browse the repository at this point in the history
This reverts commit 78ae9ee.

Grr. The builtin functionality doesn't work for us. It writes any value
out as "variant(VALUE)". I thought I had tested this, but apparently I
tested the wrong thing.
  • Loading branch information
joto committed Dec 13, 2024
1 parent 3a8f53b commit 6a0c731
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#define FMT_HEADER_ONLY
#include <fmt/format.h>
#include <fmt/std.h>

#include <stdexcept>

Expand Down
19 changes: 14 additions & 5 deletions src/gen/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
#include "overloaded.hpp"
#include "pgsql.hpp"

std::string to_string(param_value_t const &value)
{
return std::visit(
overloaded{[](null_param_t) { return std::string{}; },
[](std::string val) { return val; },
[](auto const &val) { return fmt::to_string(val); }},
value);
}

param_value_t params_t::get(std::string const &key) const
{
return m_map.at(key);
Expand Down Expand Up @@ -49,7 +58,7 @@ double params_t::get_double(std::string const &key, double default_value) const
return static_cast<double>(std::get<int64_t>(it->second));
}

throw fmt_error("Invalid value '{}' for {}.", it->second, key);
throw fmt_error("Invalid value '{}' for {}.", to_string(it->second), key);
}

std::string params_t::get_string(std::string const &key) const
Expand All @@ -58,7 +67,7 @@ std::string params_t::get_string(std::string const &key) const
if (it == m_map.end()) {
throw fmt_error("Missing parameter '{}' on generalizer.", key);
}
return fmt::format("{}", it->second);
return to_string(it->second);
}

std::string params_t::get_string(std::string const &key,
Expand All @@ -73,7 +82,7 @@ std::string params_t::get_identifier(std::string const &key) const
if (it == m_map.end()) {
return {};
}
std::string result = fmt::format("{}", it->second);
std::string result = to_string(it->second);
check_identifier(result, key.c_str());
return result;
}
Expand All @@ -85,7 +94,7 @@ void params_t::check_identifier_with_default(std::string const &key,
if (it == m_map.end()) {
m_map.emplace(key, std::move(default_value));
} else {
check_identifier(fmt::format("{}", it->second), key.c_str());
check_identifier(to_string(it->second), key.c_str());
}
}

Expand All @@ -111,6 +120,6 @@ void write_to_debug_log(params_t const &params, char const *message)
}
log_debug("{}", message);
for (auto const &[key, value] : params) {
log_debug(" {}={}", key, value);
log_debug(" {}={}", key, to_string(value));
}
}
6 changes: 5 additions & 1 deletion src/gen/params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ using null_param_t = std::monostate;
using param_value_t =
std::variant<null_param_t, std::string, int64_t, double, bool>;

/// Convert a parameter value into a string.
std::string to_string(param_value_t const &value);

/**
* A collection of parameters.
*/
Expand Down Expand Up @@ -76,7 +79,8 @@ class params_t
}

if (!std::holds_alternative<T>(it->second)) {
throw fmt_error("Invalid value '{}' for {}.", it->second, key);
throw fmt_error("Invalid value '{}' for {}.", to_string(it->second),
key);
}
return std::get<T>(it->second);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gen/template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

void template_t::set_params(params_t const &params) {
for (auto const &[key, value] : params) {
m_format_store.push_back(fmt::arg(key.c_str(), value));
m_format_store.push_back(fmt::arg(key.c_str(), to_string(value)));
}
}

Expand Down

0 comments on commit 6a0c731

Please sign in to comment.