From 28892f3d4cf6d9fb6486de5197dd6d46da706f13 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 16 Oct 2025 20:11:33 +0100 Subject: Replace that awful magic number heavy mapping function Now a tuple of mapping functors and we pass each value through its corresponding converter. --- src/ingestor.cpp | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/ingestor.cpp b/src/ingestor.cpp index 1bf4c99..4f752d7 100644 --- a/src/ingestor.cpp +++ b/src/ingestor.cpp @@ -30,32 +30,36 @@ namespace WebStat { static_cast(value.length()))); } - Entity - toEntity(const std::string_view value, const EntityType type) - { - return {crc32(value), type, value}; - } + template struct ToEntity { + Entity + operator()(const std::string_view value) const + { + return {crc32(value), Type, value}; + } - std::optional - toEntityo(const std::optional value, const EntityType type) - { - return value.transform([type](auto && value) { - return toEntity(value, type); - }); - } + template + std::optional + operator()(const std::optional & value) const + { + return value.transform([this](auto && value) { + return (*this)(value); + }); + } + }; auto crc32ScanValues(const Ingestor::ScanValues & values) { - return std::apply( - [](auto &&... value) { - return std::make_tuple(toEntity(value...[0], EntityType::VirtualHost), value...[1], value...[2], - value...[3], toEntity(value...[4], EntityType::Path), - toEntityo(value...[5], EntityType::QueryString), value...[6], value...[7], value...[8], - value...[9], toEntityo(value...[10], EntityType::Referrer), - toEntityo(value...[11], EntityType::UserAgent)); - }, - values); + static constexpr std::tuple, std::identity, std::identity, std::identity, + ToEntity, ToEntity, std::identity, std::identity, + std::identity, std::identity, ToEntity, ToEntity> + ENTITY_TYPE_MAP; + static constexpr size_t VALUE_COUNT = std::tuple_size_v; + static_assert(VALUE_COUNT == std::tuple_size_v); + + return [&values](std::index_sequence) { + return std::make_tuple(std::get(ENTITY_TYPE_MAP)(std::get(values))...); + }(std::make_index_sequence()); } } @@ -186,7 +190,7 @@ namespace WebStat { } else { linesDiscarded++; - const auto unparsableLine = toEntity(line, EntityType::UnparsableLine); + const auto unparsableLine = ToEntity {}(line); rememberNewEntityIds(storeEntities(dbconn, {unparsableLine})); } } -- cgit v1.2.3