domscheme.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. #include "config.h"
  3. #include <util/generic/algorithm.h>
  4. #include <util/generic/typetraits.h>
  5. #include <util/stream/str.h>
  6. struct TConfigTraits {
  7. using TValue = NConfig::TConfig;
  8. using TValueRef = const TValue*;
  9. using TConstValueRef = TValueRef;
  10. using TStringType = TString;
  11. // anyvalue defaults
  12. template <class T>
  13. static inline TValue Value(const T& t) {
  14. return TValue(NConfig::ConstructValue(t));
  15. }
  16. template <class T>
  17. static inline TValue Value(std::initializer_list<T> list) {
  18. NConfig::TArray result;
  19. for (const auto& t : list) {
  20. result.push_back(TValue(NConfig::ConstructValue(t)));
  21. }
  22. return TValue(NConfig::ConstructValue(std::move(result)));
  23. }
  24. static inline TConstValueRef Ref(const TValue& v) {
  25. return &v;
  26. }
  27. // common ops
  28. static inline bool IsNull(TConstValueRef v) {
  29. return v->IsNull();
  30. }
  31. static inline TString ToJson(TConstValueRef v) {
  32. TStringStream str;
  33. v->ToJson(str);
  34. return str.Str();
  35. }
  36. // struct ops
  37. static inline TConstValueRef GetField(TConstValueRef v, const TStringBuf& name) {
  38. return &(*v)[name];
  39. }
  40. // array ops
  41. static bool IsArray(TConstValueRef v) {
  42. return v->IsA<NConfig::TArray>();
  43. }
  44. using TArrayIterator = size_t;
  45. static inline TConstValueRef ArrayElement(TConstValueRef v, TArrayIterator n) {
  46. return &(*v)[n];
  47. }
  48. static inline size_t ArraySize(TConstValueRef v) {
  49. return v->GetArraySize();
  50. }
  51. static inline TArrayIterator ArrayBegin(TConstValueRef) {
  52. return 0;
  53. }
  54. static inline TArrayIterator ArrayEnd(TConstValueRef v) {
  55. return ArraySize(v);
  56. }
  57. // dict ops
  58. static bool IsDict(TConstValueRef v) {
  59. return v->IsA<NConfig::TDict>();
  60. }
  61. static inline TConstValueRef DictElement(TConstValueRef v, TStringBuf key) {
  62. return &(*v)[key];
  63. }
  64. static inline size_t DictSize(TConstValueRef v) {
  65. return v->Get<NConfig::TDict>().size();
  66. }
  67. using TDictIterator = NConfig::TDict::const_iterator;
  68. static inline TDictIterator DictBegin(TConstValueRef v) {
  69. return v->Get<NConfig::TDict>().begin();
  70. }
  71. static inline TDictIterator DictEnd(TConstValueRef v) {
  72. return v->Get<NConfig::TDict>().end();
  73. }
  74. static inline TStringBuf DictIteratorKey(TConstValueRef /*dict*/, const TDictIterator& it) {
  75. return it->first;
  76. }
  77. static inline TConstValueRef DictIteratorValue(TConstValueRef /*dict*/, const TDictIterator& it) {
  78. return &it->second;
  79. }
  80. // generic get
  81. template <typename T>
  82. static inline void Get(TConstValueRef v, T def, T& t) {
  83. t = v->As<T>(def);
  84. }
  85. static inline bool Get(TConstValueRef v, double def, double& t) {
  86. if (v->IsNumeric()) {
  87. t = v->As<double>(def);
  88. return true;
  89. }
  90. t = def;
  91. return false;
  92. }
  93. template <typename T>
  94. static inline void Get(TConstValueRef v, T& t) {
  95. t = v->As<T>();
  96. }
  97. template <typename T>
  98. static inline bool IsValidPrimitive(const T&, TConstValueRef v) {
  99. if (v->IsNull()) {
  100. return true;
  101. }
  102. try {
  103. v->As<T>();
  104. return true;
  105. } catch (const NConfig::TTypeMismatch&) {
  106. } catch (const TBadCastException&) {
  107. }
  108. return false;
  109. }
  110. template <class T>
  111. static inline void Set(TValueRef v, T&& t) {
  112. v->GetNonConstant<std::remove_const_t<std::remove_reference_t<T>>>() = t;
  113. }
  114. // validation ops
  115. static inline TVector<TString> GetKeys(TConstValueRef v) {
  116. TVector<TString> res;
  117. for (const auto& it : v->Get<NConfig::TDict>()) {
  118. res.push_back(it.first);
  119. }
  120. Sort(res.begin(), res.end());
  121. return res;
  122. }
  123. };