// Copyright (c) 2014, IOnU Security, Inc. // Copyright (c) 2016, Sequence Logic, Inc. All rights reserved. #include "storagelocation.h" #include #include using namespace sequencelogic; namespace { // Used in StorageLocation::getAsJson() static const char* BUCKET_ID_KEY = "bucket_id"; static const char* KIND_KEY = "kind"; static const char* STATUS_KEY = "status"; static const char* TYPE_KEY = "type"; static const int NUM_KIND = 8; static const int NUM_STATUS = 5; static const int NUM_TYPE = 4; typedef std::pair CHAR_STR_PAIR; auto m1 = [](const StorageLocation::LOCATE_KIND& e, const std::string& s){return CHAR_STR_PAIR(static_cast(e),s);}; static std::array const kind_to_string = { m1(StorageLocation::LOCATE_KIND::AZURE,"azure"), m1(StorageLocation::LOCATE_KIND::BOX,"box"), m1(StorageLocation::LOCATE_KIND::DROPBOX,"dropbox"), m1(StorageLocation::LOCATE_KIND::FS,"fs"), m1(StorageLocation::LOCATE_KIND::GOOGLE_DRIVE,"google_drive"), m1(StorageLocation::LOCATE_KIND::SGWS,"sgws"), m1(StorageLocation::LOCATE_KIND::S3,"s3"), m1(StorageLocation::LOCATE_KIND::UNKNOWN,"unknown") }; auto m2 = [](const StorageLocation::LOCATE_STATUS& e, const std::string& s){return CHAR_STR_PAIR(static_cast(e),s);}; static std::array const status_to_string = { m2(StorageLocation::LOCATE_STATUS::OK,"ok"), m2(StorageLocation::LOCATE_STATUS::PENDING,"pending"), m2(StorageLocation::LOCATE_STATUS::FLUSHED,"flushed"), m2(StorageLocation::LOCATE_STATUS::LOCERROR,"locerror"), m2(StorageLocation::LOCATE_STATUS::UNKNOWN,"unknown") }; typedef std::pair INT_STR_PAIR; auto m3 = [](const StorageLocation::LOCATE_TYPE& e, const std::string& s){return INT_STR_PAIR(static_cast(e),s);}; static std::array const type_to_string = { m3(StorageLocation::LOCATE_TYPE::L1,"l1"), m3(StorageLocation::LOCATE_TYPE::L2,"l2"), m3(StorageLocation::LOCATE_TYPE::CS,"cs"), m3(StorageLocation::LOCATE_TYPE::UNKNOWN,"unknown"), }; } // end anonymous namespace StorageLocation::StorageLocation(const JSONObject& loc_data) : m_bucket_id(loc_data.getJSONString(BUCKET_ID_KEY)), m_kind(toLocKind(loc_data.getJSONString(KIND_KEY))), m_status(toLocStatus(loc_data.getJSONString(STATUS_KEY))), m_type(toLocType(loc_data.getJSONString(TYPE_KEY))) { } StorageLocation& StorageLocation::operator=(const StorageLocation& other) { if (&other != this) { m_bucket_id = other.m_bucket_id; m_kind = other.m_kind; m_status = other.m_status; m_type = other.m_type; } return *this; } std::shared_ptr StorageLocation::getAsJson() const { std::shared_ptr p(new JSONObject()); p->setJSONValue(BUCKET_ID_KEY, m_bucket_id.c_str()); p->setJSONValue(KIND_KEY, toString(m_kind).c_str()); p->setJSONValue(STATUS_KEY, toString(m_status).c_str()); p->setJSONValue(TYPE_KEY, toString(m_type).c_str()); return p; } std::string StorageLocation::toString(const StorageLocation::LOCATE_KIND k) const { for(auto i : kind_to_string) { if(i.first==static_cast(k)) { return i.second; } } return ""; } std::string StorageLocation::toString(const StorageLocation::LOCATE_STATUS s) const { for(auto i : status_to_string) { if(i.first==static_cast(s)) { return i.second; } } return ""; } std::string StorageLocation::toString(const StorageLocation::LOCATE_TYPE t) const { for(auto i : type_to_string) { if(i.first==static_cast(t)) { return i.second; } } return ""; } StorageLocation::LOCATE_KIND StorageLocation::toLocKind(const std::string& s) const { for(auto i : kind_to_string) { if (i.second == s) { return static_cast(i.first); } } return LOCATE_KIND::UNKNOWN; } StorageLocation::LOCATE_STATUS StorageLocation::toLocStatus(const std::string& s) const { for(auto i : status_to_string) { if (i.second == s) { return static_cast(i.first); } } return LOCATE_STATUS::UNKNOWN; } StorageLocation::LOCATE_TYPE StorageLocation::toLocType(const std::string& s) const { for(auto i : type_to_string) { if (i.second == s) { return static_cast(i.first); } } return LOCATE_TYPE::UNKNOWN; }