feat/update_config #10
@ -209,6 +209,7 @@ set(TILE_SRCS
|
||||
# "tile/rpc/server.cc"
|
||||
"tile/base/config/configuration.cc"
|
||||
"tile/base/config/ini_file_configuration.cc"
|
||||
"tile/base/config/json_configuration.cc"
|
||||
"tile/base/config/layered_configuration.cc"
|
||||
)
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
#include "tile/base/string.h"
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
class Configurable {
|
||||
Configurable();
|
||||
virtual ~Configurable() = default;
|
||||
@ -13,7 +12,6 @@ class Configurable {
|
||||
virtual std::string GetProperty(const Slice &name) const = 0;
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
||||
#endif // TILE_BASE_CONFIG_CONFIGURABLE_H
|
||||
|
@ -2,7 +2,6 @@
|
||||
#include "tile/base/thread/unique_lock.h"
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
bool Configuration::Has(const Slice &key) const {
|
||||
UniqueLock<tile::Mutex> lock(mutex_);
|
||||
std::string value;
|
||||
@ -109,6 +108,4 @@ void Configuration::SetRawWithEvent(const Slice &key,
|
||||
|
||||
Configuration::~Configuration() {}
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace tile
|
||||
|
@ -11,7 +11,6 @@
|
||||
#include <cstdint>
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
class Configuration : public RefCounted<Configuration> {
|
||||
public:
|
||||
using Keys = std::vector<std::string>;
|
||||
@ -121,7 +120,6 @@ private:
|
||||
mutable Mutex mutex_;
|
||||
bool events_enabled_{false};
|
||||
};
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
||||
#endif // TILE_BASE_CONFIG_CONFIGURATION_H
|
||||
|
@ -2,7 +2,6 @@
|
||||
#include "tile/base/thread/scoped_lock.h"
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
|
||||
IniFileConfiguration::IniFileConfiguration() {}
|
||||
// IniFileConfig::IniFileConfig(std::istream &istr) { load(istr); }
|
||||
@ -10,6 +9,10 @@ IniFileConfiguration::IniFileConfiguration() {}
|
||||
IniFileConfiguration::~IniFileConfiguration() {}
|
||||
|
||||
bool IniFileConfiguration::load(std::istream &istr) {
|
||||
if (!istr.good()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Configuration::ScopedLock lock(*this);
|
||||
map_.clear();
|
||||
section_key_.clear();
|
||||
@ -20,11 +23,7 @@ bool IniFileConfiguration::load(std::istream &istr) {
|
||||
}
|
||||
bool IniFileConfiguration::load(const std::string &path) {
|
||||
std::ifstream istr(path);
|
||||
if (istr.good()) {
|
||||
return load(istr);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return load(istr);
|
||||
}
|
||||
bool IniFileConfiguration::GetRaw(const Slice &key, std::string *value) const {
|
||||
auto iter = map_.find(key.ToString());
|
||||
@ -141,5 +140,4 @@ bool IniFileConfiguration::ICompare::operator()(const std::string &s1,
|
||||
return strncmp(s1.c_str(), s2.c_str(), len) < 0;
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
@ -7,7 +7,6 @@
|
||||
#include <map>
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
class IniFileConfiguration : public Configuration {
|
||||
public:
|
||||
using Ptr = RefPtr<IniFileConfiguration>;
|
||||
@ -34,7 +33,6 @@ private:
|
||||
IStringMap map_;
|
||||
std::string section_key_;
|
||||
};
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
||||
#endif // TILE_BASE_CONFIG_INI_FILE_CONFIG_H
|
||||
|
@ -16,7 +16,6 @@ a=4
|
||||
)";
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
|
||||
static_assert(
|
||||
!detail::HasClassofImpl<IniFileConfiguration, Configuration>::value, "");
|
||||
@ -43,5 +42,4 @@ TEST(IniFileConfig, LoadFromIStream) {
|
||||
ASSERT_EQ(3, *config->GetInt("sec3.a"));
|
||||
ASSERT_EQ(4, *config->GetInt("sec2.kk.a"));
|
||||
}
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
128
tile/base/config/json_configuration.cc
Normal file
128
tile/base/config/json_configuration.cc
Normal file
@ -0,0 +1,128 @@
|
||||
#include "tile/base/config/json_configuration.h"
|
||||
#include "tile/base/logging.h"
|
||||
|
||||
namespace tile {
|
||||
JSONConfiguration::JSONConfiguration() : object_() {}
|
||||
JSONConfiguration::~JSONConfiguration() {}
|
||||
|
||||
bool JSONConfiguration::load(const std::string &path) {
|
||||
std::ifstream istr(path);
|
||||
return load(istr);
|
||||
}
|
||||
|
||||
bool JSONConfiguration::load(std::istream &istr) {
|
||||
if (!istr.good()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Configuration::ScopedLock _(*this);
|
||||
object_.clear();
|
||||
Json::Reader reader;
|
||||
return reader.parse(istr, object_, true);
|
||||
}
|
||||
|
||||
void JSONConfiguration::SetInt(const Slice &key, int value) {
|
||||
SetValue(key, value);
|
||||
}
|
||||
|
||||
void JSONConfiguration::SetBool(const Slice &key, bool value) {
|
||||
SetValue(key, value);
|
||||
}
|
||||
|
||||
void JSONConfiguration::SetDouble(const Slice &key, double value) {
|
||||
SetValue(key, value);
|
||||
}
|
||||
|
||||
void JSONConfiguration::SetString(const Slice &key, const std::string &value) {
|
||||
SetValue(key, value);
|
||||
}
|
||||
|
||||
void JSONConfiguration::RemoveRaw(const Slice &key) {
|
||||
Configuration::ScopedLock _(*this);
|
||||
Json::Value root;
|
||||
Slice last_part;
|
||||
if (!FindStart(key, &last_part, &root)) {
|
||||
return;
|
||||
}
|
||||
root.removeMember(last_part.ToString());
|
||||
}
|
||||
|
||||
bool JSONConfiguration::GetRaw(const Slice &key, std::string *value) const {
|
||||
Configuration::ScopedLock _(*this);
|
||||
auto keys = Split(key, '.');
|
||||
auto root = object_;
|
||||
for (std::size_t i = 0; i < keys.size(); ++i) {
|
||||
const auto &cur_key = keys[i];
|
||||
if (cur_key.empty()) {
|
||||
TILE_LOG_ERROR("Invalid key: {}", key);
|
||||
return false;
|
||||
}
|
||||
if (!object_.isMember(cur_key)) {
|
||||
return false;
|
||||
}
|
||||
root = root[cur_key];
|
||||
}
|
||||
|
||||
*value = root.asString();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool JSONConfiguration::SetRaw(const Slice &key, const Slice &value) {
|
||||
return SetValue(key, value.ToString());
|
||||
}
|
||||
|
||||
Configuration::Keys JSONConfiguration::Enumerate(const Slice &range) const {
|
||||
Configuration::ScopedLock _(*this);
|
||||
Configuration::Keys key_set;
|
||||
|
||||
auto keys = Split(range, '.');
|
||||
auto root = object_;
|
||||
for (std::size_t i = 0; i < keys.size(); ++i) {
|
||||
const auto &cur_key = keys[i];
|
||||
if (cur_key.empty()) {
|
||||
TILE_LOG_ERROR("Invalid range: {}", range);
|
||||
return key_set;
|
||||
}
|
||||
if (!object_.isMember(cur_key)) {
|
||||
return key_set;
|
||||
}
|
||||
root = root[cur_key];
|
||||
}
|
||||
|
||||
for (const auto &key : root.getMemberNames()) {
|
||||
key_set.push_back(key);
|
||||
}
|
||||
return key_set;
|
||||
}
|
||||
|
||||
bool JSONConfiguration::FindStart(const Slice &key, Slice *last_part,
|
||||
Json::Value *parent_obj) {
|
||||
auto keys = Split(key, '.');
|
||||
if (keys.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto root = object_;
|
||||
for (std::size_t i = 0; i < keys.size() - 1; ++i) {
|
||||
const auto &cur_key = keys[i];
|
||||
if (cur_key.empty()) {
|
||||
TILE_LOG_ERROR("Invalid key: {}", key);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!root.isMember(cur_key)) {
|
||||
root[cur_key] = Json::Value(Json::objectValue);
|
||||
} else if (!root[cur_key].isObject()) {
|
||||
TILE_LOG_ERROR("only leaf nodes can be set: key: {}, cur_key(idx={}): {}",
|
||||
key, i, cur_key);
|
||||
return false;
|
||||
}
|
||||
root = root[cur_key];
|
||||
}
|
||||
|
||||
*last_part = keys.back();
|
||||
*parent_obj = root;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace tile
|
48
tile/base/config/json_configuration.h
Normal file
48
tile/base/config/json_configuration.h
Normal file
@ -0,0 +1,48 @@
|
||||
#ifndef TILE_BASE_CONFIG_JSON_CONFIGURATION_H
|
||||
#define TILE_BASE_CONFIG_JSON_CONFIGURATION_H
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "tile/base/config/configuration.h"
|
||||
#include "json/json.h"
|
||||
|
||||
namespace tile {
|
||||
class JSONConfiguration : public Configuration {
|
||||
public:
|
||||
using Ptr = RefPtr<JSONConfiguration>;
|
||||
JSONConfiguration();
|
||||
~JSONConfiguration() override;
|
||||
bool load(const std::string &path);
|
||||
bool load(std::istream &istr);
|
||||
|
||||
void SetInt(const Slice &key, int value) override;
|
||||
void SetBool(const Slice &key, bool value) override;
|
||||
void SetDouble(const Slice &key, double value) override;
|
||||
void SetString(const Slice &key, const std::string &value) override;
|
||||
void RemoveRaw(const Slice &key) override;
|
||||
|
||||
protected:
|
||||
bool GetRaw(const Slice &key, std::string *value) const override;
|
||||
bool SetRaw(const Slice &key, const Slice &value) override;
|
||||
Keys Enumerate(const Slice &range) const override;
|
||||
|
||||
private:
|
||||
bool FindStart(const Slice &key, Slice *last_prt, Json::Value *parent_obj);
|
||||
template <typename T> bool SetValue(const Slice &key, T value);
|
||||
Json::Value object_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool JSONConfiguration::SetValue(const Slice &key, T value) {
|
||||
Slice last_part;
|
||||
Json::Value root;
|
||||
Configuration::ScopedLock _(*this);
|
||||
if (!FindStart(key, &last_part, &root)) {
|
||||
return false;
|
||||
}
|
||||
root[last_part] = Json::Value(value);
|
||||
return true;
|
||||
}
|
||||
} // namespace tile
|
||||
|
||||
#endif // TILE_BASE_CONFIG_JSON_CONFIGURATION_H
|
@ -1,7 +1,6 @@
|
||||
#include "tile/base/config/layered_configuration.h"
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
|
||||
LayeredConfiguration::LayeredConfiguration() {}
|
||||
LayeredConfiguration::~LayeredConfiguration() {}
|
||||
@ -129,5 +128,4 @@ int LayeredConfiguration::highest() const {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
@ -7,7 +7,6 @@
|
||||
#include <list>
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
class LayeredConfiguration : public Configuration {
|
||||
public:
|
||||
using Ptr = RefPtr<LayeredConfiguration>;
|
||||
@ -51,7 +50,6 @@ private:
|
||||
using ConfigList = std::list<ConfigItem>;
|
||||
ConfigList configs_;
|
||||
};
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
||||
#endif // TILE_BASE_CONFIG_LAYERED_CONFIGURATION_H
|
||||
|
@ -47,7 +47,6 @@
|
||||
#include "sigslot/sigslot.h"
|
||||
|
||||
// Tile Init
|
||||
#include "mustache.hpp"
|
||||
#include "tile/init.h"
|
||||
|
||||
#endif // TILE_TILE_H
|
||||
|
Loading…
Reference in New Issue
Block a user