value.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "value.h"
  2. #include "config.h"
  3. #include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h>
  4. #include <util/generic/algorithm.h>
  5. #include <util/system/type_name.h>
  6. #include <util/generic/singleton.h>
  7. #include <util/string/cast.h>
  8. #include <util/string/strip.h>
  9. #include <util/string/type.h>
  10. using namespace NConfig;
  11. namespace {
  12. template <class T>
  13. class TValue: public IValue {
  14. public:
  15. inline TValue(const T& t)
  16. : T_(t)
  17. {
  18. }
  19. bool IsA(const std::type_info& info) const override {
  20. return info == typeid(T);
  21. }
  22. TString TypeName() const override {
  23. return ::TypeName<T>();
  24. }
  25. void* Ptr() const override {
  26. return (void*)&T_;
  27. }
  28. void ToJson(IOutputStream& out) const override {
  29. out << AsString();
  30. }
  31. bool AsBool() const override {
  32. return (bool)AsDouble();
  33. }
  34. protected:
  35. T T_;
  36. };
  37. class TNullValue: public TValue<TNull> {
  38. public:
  39. inline TNullValue()
  40. : TValue<TNull>(TNull())
  41. {
  42. Ref();
  43. }
  44. double AsDouble() const override {
  45. return 0;
  46. }
  47. ui64 AsUInt() const override {
  48. return 0;
  49. }
  50. i64 AsInt() const override {
  51. return 0;
  52. }
  53. TString AsString() const override {
  54. return TString();
  55. }
  56. void ToJson(IOutputStream& out) const override {
  57. out << "null";
  58. }
  59. TString TypeName() const override {
  60. return "null";
  61. }
  62. };
  63. template <class T>
  64. class TNumericValue: public TValue<T> {
  65. public:
  66. inline TNumericValue(const T& t)
  67. : TValue<T>(t)
  68. {
  69. }
  70. double AsDouble() const override {
  71. return this->T_;
  72. }
  73. ui64 AsUInt() const override {
  74. return this->T_;
  75. }
  76. i64 AsInt() const override {
  77. return this->T_;
  78. }
  79. };
  80. class TBoolValue: public TNumericValue<bool> {
  81. public:
  82. inline TBoolValue(bool v)
  83. : TNumericValue<bool>(v)
  84. {
  85. }
  86. TString AsString() const override {
  87. return T_ ? "true" : "false";
  88. }
  89. };
  90. template <class T>
  91. class TArithmeticValue: public TNumericValue<T> {
  92. public:
  93. inline TArithmeticValue(T v)
  94. : TNumericValue<T>(v)
  95. {
  96. }
  97. TString AsString() const override {
  98. return ToString(this->T_);
  99. }
  100. };
  101. class TStringValue: public TValue<TString> {
  102. public:
  103. inline TStringValue(const TString& v)
  104. : TValue<TString>(v)
  105. {
  106. }
  107. template <class T>
  108. inline T AsT() const {
  109. const TStringBuf s = StripString(TStringBuf(T_));
  110. if (IsTrue(s)) {
  111. return true;
  112. }
  113. if (IsFalse(s)) {
  114. return false;
  115. }
  116. return FromString<T>(s);
  117. }
  118. double AsDouble() const override {
  119. return AsT<double>();
  120. }
  121. ui64 AsUInt() const override {
  122. return AsT<ui64>();
  123. }
  124. i64 AsInt() const override {
  125. return AsT<i64>();
  126. }
  127. TString AsString() const override {
  128. return T_;
  129. }
  130. void ToJson(IOutputStream& out) const override {
  131. NEscJ::EscapeJ<true, true>(T_, out);
  132. }
  133. TString TypeName() const override {
  134. return "string";
  135. }
  136. };
  137. template <class T>
  138. class TContainer: public TValue<T> {
  139. public:
  140. inline TContainer(const T& t)
  141. : TValue<T>(t)
  142. {
  143. }
  144. double AsDouble() const override {
  145. NCfgPrivate::ReportTypeMismatch(this->TypeName(), "double");
  146. }
  147. ui64 AsUInt() const override {
  148. NCfgPrivate::ReportTypeMismatch(this->TypeName(), "uint");
  149. }
  150. i64 AsInt() const override {
  151. NCfgPrivate::ReportTypeMismatch(this->TypeName(), "int");
  152. }
  153. bool AsBool() const override {
  154. NCfgPrivate::ReportTypeMismatch(this->TypeName(), "bool");
  155. }
  156. TString AsString() const override {
  157. NCfgPrivate::ReportTypeMismatch(this->TypeName(), "string");
  158. }
  159. };
  160. class TArrayValue: public TContainer<TArray> {
  161. public:
  162. inline TArrayValue(const TArray& v)
  163. : TContainer<TArray>(v)
  164. {
  165. }
  166. void ToJson(IOutputStream& s) const override {
  167. s << "[";
  168. for (TArray::const_iterator it = T_.begin(); it != T_.end(); ++it) {
  169. if (it != T_.begin()) {
  170. s << ",";
  171. }
  172. it->ToJson(s);
  173. }
  174. s << "]";
  175. }
  176. TString TypeName() const override {
  177. return "array";
  178. }
  179. };
  180. class TDictValue: public TContainer<TDict> {
  181. public:
  182. inline TDictValue(const TDict& v)
  183. : TContainer<TDict>(v)
  184. {
  185. }
  186. void ToJson(IOutputStream& s) const override {
  187. s << "{";
  188. TVector<TStringBuf> buf;
  189. buf.reserve(T_.size());
  190. for (const auto& t : T_) {
  191. buf.push_back(t.first);
  192. }
  193. Sort(buf.begin(), buf.end());
  194. for (TVector<TStringBuf>::const_iterator kit = buf.begin(); kit != buf.end(); ++kit) {
  195. TStringBuf key = *kit;
  196. TDict::const_iterator it = T_.find(key);
  197. if (kit != buf.begin()) {
  198. s << ",";
  199. }
  200. NEscJ::EscapeJ<true, true>(key, s);
  201. s << ":";
  202. it->second.ToJson(s);
  203. }
  204. s << "}";
  205. }
  206. TString TypeName() const override {
  207. return "dict";
  208. }
  209. };
  210. }
  211. #define DECLARE(type1, type2) \
  212. IValue* ConstructValueImpl(const type2& t) { \
  213. return new type1(t); \
  214. }
  215. namespace NConfig {
  216. namespace NCfgPrivate {
  217. DECLARE(TBoolValue, bool)
  218. DECLARE(TArithmeticValue<double>, double)
  219. DECLARE(TArithmeticValue<i64>, i64)
  220. DECLARE(TArithmeticValue<ui64>, ui64)
  221. DECLARE(TStringValue, TString)
  222. DECLARE(TArrayValue, TArray)
  223. DECLARE(TDictValue, TDict)
  224. }
  225. IValue* Null() {
  226. return Singleton<TNullValue>();
  227. }
  228. [[noreturn]] void NCfgPrivate::ReportTypeMismatch(TStringBuf realType, TStringBuf expectedType) {
  229. ythrow TTypeMismatch() << "type mismatch (real: " << realType << ", expected: " << expectedType << ')';
  230. }
  231. }