Skip to content

Commit

Permalink
fix: handle empty string to number conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
pnck committed May 13, 2024
1 parent 95d95af commit 5c2f57b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/common/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace
using base_t = std::string;
constexpr decay_string() : base_t() {}
constexpr decay_string(base_t &&str) noexcept : base_t(std::move(str)) {}
operator const char *() const noexcept { return this->data(); }
operator std::string_view() const noexcept { return {this->data(), this->size()}; }
constexpr operator const char *() const noexcept { return this->data(); }
constexpr operator std::string_view() const noexcept { return {this->data(), this->size()}; }
};

constexpr decay_string operator""_upper(const char *str, std::size_t len) {
Expand All @@ -40,14 +40,14 @@ namespace
}
return result;
}
/*
#if 0 // string can be implicitly converted to string_view
constexpr decay_string upper(const std::string &s) {
decay_string result;
std::transform(
s.begin(), s.end(), std::back_inserter(result), [](char c) -> char { return (c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c;
}); return result;
}
*/
#endif
constexpr auto upper(std::string_view v) {
return operator""_upper(v.data(), v.size());
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,6 @@ extern const struct _spdlog_init_helper_st {
#define DEBUG_LOG(...) SPDLOG_DEBUG(_forward_strs(__VA_ARGS__))
#define DEBUG_LOG_F(...) SPDLOG_DEBUG(__VA_ARGS__)
#define WARN_LOG(...) SPDLOG_WARN(_forward_strs(__VA_ARGS__))
#define WARN_LOG_F(...) SPDLOG_WARN(__VA_ARGS__)
#define ERROR_LOG(...) SPDLOG_ERROR(_forward_strs(__VA_ARGS__))
#define ERROR_LOG_F(...) SPDLOG_ERROR(__VA_ARGS__)
45 changes: 27 additions & 18 deletions src/meta_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ void meta_processor::update(const file_info &info) { // FB2K
}

void meta_processor::update(const nlohmann::json &json) { // NCM, public
update_by_json(json);
if (json.contains("overwrite")) {
update_by_json(json["overwrite"], true);
try {
update_by_json(json);
if (json.contains("overwrite")) {
update_by_json(json["overwrite"], true);
}
} catch (const std::exception &e) {
ERROR_LOG_F("Error processing meta({}): {}", e.what(), json.dump());
}
}

Expand Down Expand Up @@ -123,21 +127,26 @@ void meta_processor::update_by_json(const nlohmann::json &json, bool overwriting
// NOTE: I found an abnormal case that albumPicId is a number instead of string.
// So I deside to test every possible numeric type and try to convert them.

#define reflect_single(field, TYPE) \
[&](const json_t &j) { \
if (j.is_null()) { \
field.reset(); \
return; \
} \
try { \
update_v(field, j.get<TYPE>()); \
} catch (const json_t::type_error &) { \
if constexpr (std::is_same_v<TYPE, std::string>) { \
update_v(field, std::to_string(j.get<uint64_t>())); \
} else if constexpr (std::is_same_v<TYPE, uint64_t>) { \
update_v(field, std::stoull(j.get<std::string>())); \
} \
} \
#define reflect_single(field, TYPE) \
[&](const json_t &j) { \
if (j.is_null()) { \
field.reset(); \
return; \
} \
try { \
update_v(field, j.get<TYPE>()); \
} catch (const json_t::type_error &) { \
try { \
if constexpr (std::is_same_v<TYPE, std::string>) { \
update_v(field, std::to_string(j.get<uint64_t>())); \
} else if constexpr (std::is_same_v<TYPE, uint64_t>) { \
\
update_v(field, std::stoull(j.get<std::string>())); \
} \
} catch (const json_t::type_error &) { \
} catch (const std::invalid_argument &) { \
} \
} \
}
#define reflect_multi_string(field) \
[&](const json_t &j) { \
Expand Down

0 comments on commit 5c2f57b

Please sign in to comment.