From 14d2d9a7d690bc3e74c93bf30aeb4391446b051d Mon Sep 17 00:00:00 2001 From: Emanuele Danovaro Date: Thu, 22 Feb 2024 12:27:26 +0000 Subject: [PATCH] all keys to lowercase --- VERSION | 2 +- src/fdb5/database/InspectionKey.cc | 47 ------------------------------ src/fdb5/database/Key.cc | 26 ++++++++++++++--- src/fdb5/database/Key.h | 2 ++ tests/fdb/type/test_toKey.cc | 4 +-- 5 files changed, 27 insertions(+), 54 deletions(-) diff --git a/VERSION b/VERSION index 73e63502d..bbc239cb8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -5.11.101 \ No newline at end of file +5.11.102 diff --git a/src/fdb5/database/InspectionKey.cc b/src/fdb5/database/InspectionKey.cc index d23698d40..a16799031 100644 --- a/src/fdb5/database/InspectionKey.cc +++ b/src/fdb5/database/InspectionKey.cc @@ -106,53 +106,6 @@ Key InspectionKey::canonical() const { return key; } -// void InspectionKey::decode(eckit::Stream& s) { - -// ASSERT(rule_ == nullptr); - -// keys_.clear(); -// names_.clear(); - - -// size_t n; - -// s >> n; -// std::string k; -// std::string v; -// for (size_t i = 0; i < n; ++i) { -// s >> k; -// s >> v; -// keys_[k] = v; -// } - -// s >> n; -// for (size_t i = 0; i < n; ++i) { -// s >> k; -// s >> v; // this is the type (ignoring FTM) -// names_.push_back(k); -// } -// s >> canonical_; -// } - -// void InspectionKey::encode(eckit::Stream& s) const { -// const TypesRegistry& registry = this->registry(); - -// s << keys_.size(); -// for (eckit::StringDict::const_iterator i = keys_.begin(); i != keys_.end(); ++i) { -// const Type &t = registry.lookupType(i->first); -// s << i->first << canonicalise(i->first, i->second); -// } - -// s << names_.size(); -// for (eckit::StringList::const_iterator i = names_.begin(); i != names_.end(); ++i) { -// const Type &t = registry.lookupType(*i); -// s << (*i); -// s << t.type(); -// } - -// s << true; -// } - void InspectionKey::rule(const Rule *rule) { rule_ = rule; } diff --git a/src/fdb5/database/Key.cc b/src/fdb5/database/Key.cc index 956ba5ebc..822e32ab8 100644 --- a/src/fdb5/database/Key.cc +++ b/src/fdb5/database/Key.cc @@ -10,6 +10,7 @@ #include +#include "eckit/config/Resource.h" #include "eckit/container/DenseSet.h" #include "eckit/utils/Tokenizer.h" @@ -79,7 +80,7 @@ void Key::decode(eckit::Stream& s) { for (size_t i = 0; i < n; ++i) { s >> k; s >> v; - keys_[k] = v; + keys_[k] = lower(v); } s >> n; @@ -126,9 +127,9 @@ void Key::set(const std::string &k, const std::string &v) { eckit::StringDict::iterator it = keys_.find(k); if (it == keys_.end()) { names_.push_back(k); - keys_[k] = v; + keys_[k] = lower(v); } else { - it->second = v; + it->second = lower(v); } } @@ -138,7 +139,7 @@ void Key::unset(const std::string &k) { } void Key::push(const std::string &k, const std::string &v) { - keys_[k] = v; + keys_[k] = lower(v); names_.push_back(k); } @@ -338,7 +339,24 @@ fdb5::Key::operator std::string() const { return toString(); } +std::string Key::lower(const std::string& value) const { + + static bool toLower = eckit::Resource("fdbKeysLowercase;$FDB_KEYS_LOWERCASE", true); + + if (!toLower) { + return value; + } + + std::string lowerCase; + lowerCase.resize(value.size()); + + std::transform(value.begin(), value.end(), lowerCase.begin(), [](unsigned char c){ return std::tolower(c); }); + + return lowerCase; +} + std::string Key::canonicalise(const std::string&, const std::string& value) const { + return value; } diff --git a/src/fdb5/database/Key.h b/src/fdb5/database/Key.h index f17eecf3e..10dcb1d4a 100644 --- a/src/fdb5/database/Key.h +++ b/src/fdb5/database/Key.h @@ -138,6 +138,8 @@ class Key { protected: // methods + std::string lower(const std::string& value) const; + //TODO add unit test for each type virtual std::string canonicalise(const std::string& keyword, const std::string& value) const; diff --git a/tests/fdb/type/test_toKey.cc b/tests/fdb/type/test_toKey.cc index 64496fe95..9407fc317 100644 --- a/tests/fdb/type/test_toKey.cc +++ b/tests/fdb/type/test_toKey.cc @@ -275,7 +275,7 @@ CASE( "Date - string ctor - expansion" ) { eckit::Translator t; EXPECT(key.canonicalValue("date") == t(now.yyyymmdd())); - EXPECT(key.valuesToString() == "od:0001:oper:ofb:"+t(now.yyyymmdd())+":0000:MHS:3001"); + EXPECT(key.valuesToString() == "od:0001:oper:ofb:"+t(now.yyyymmdd())+":0000:mhs:3001"); fdb5::Archiver archiver; fdb5::ArchiveVisitor visitor(archiver, key, data, 4); @@ -284,7 +284,7 @@ CASE( "Date - string ctor - expansion" ) { // std::cout << key.valuesToString() << std::endl; EXPECT(key.canonicalValue("date") == t(now.yyyymmdd())); - EXPECT(key.valuesToString() == "od:0001:oper:ofb:"+t(now.yyyymmdd())+":0000:MHS:3001"); + EXPECT(key.valuesToString() == "od:0001:oper:ofb:"+t(now.yyyymmdd())+":0000:mhs:3001"); }