unstrict_config.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include <library/cpp/charset/ci_string.h>
  3. #include <library/cpp/json/json_value.h>
  4. #include <library/cpp/yconf/conf.h>
  5. #include <util/string/vector.h>
  6. class TUnstrictConfig
  7. : public TYandexConfig {
  8. public:
  9. const Section* GetSection(const TString& path);
  10. bool AddSection(const TString& path);
  11. TString GetValue(const TString& path) const;
  12. //return true if value was changed;
  13. bool SetValue(const TString& path, const TString& value);
  14. bool Remove(const TString& path);
  15. bool RemoveAll(const TString& path);
  16. bool PatchEntry(const TString& path, const TString& value, const TString& prefix = "");
  17. [[nodiscard]] bool ParseJson(const NJson::TJsonValue& json);
  18. TString ToString() const;
  19. NJson::TJsonValue ToJson() const;
  20. public:
  21. static void ToJsonPatch(const Section& section, NJson::TJsonValue& result, const TString& preffix);
  22. static void ToJson(const Section& section, NJson::TJsonValue& result);
  23. static void ToJson(const TYandexConfig& config, NJson::TJsonValue& result);
  24. static void ToJson(const TString& section, NJson::TJsonValue& result);
  25. template <class T>
  26. static NJson::TJsonValue ToJson(const T& entity) {
  27. NJson::TJsonValue result;
  28. ToJson(entity, result);
  29. return result;
  30. }
  31. protected:
  32. bool OnBeginSection(Section& sec) override;
  33. private:
  34. struct TPathUnit {
  35. TCiString Name;
  36. size_t BeginIndex;
  37. size_t EndIndex;
  38. inline TPathUnit(const TCiString& name, size_t beginIndex, size_t endIndex)
  39. : Name(name)
  40. , BeginIndex(beginIndex)
  41. , EndIndex(endIndex)
  42. {
  43. Y_ABORT_UNLESS(EndIndex >= BeginIndex);
  44. }
  45. inline TPathUnit(const TCiString& name, size_t index)
  46. : TPathUnit(name, index, index)
  47. {
  48. }
  49. inline TPathUnit(const TCiString& name)
  50. : TPathUnit(name, 0)
  51. {
  52. }
  53. };
  54. using TPathIterator = TVector<TString>::const_iterator;
  55. private:
  56. bool ParseJson(const NJson::TJsonValue& json, const TString& path);
  57. TPathUnit ProcessPathUnit(const TString& element) const;
  58. const Section* GetSection(const Section* section, const TVector<TString>::const_iterator& begin, const TVector<TString>::const_iterator& end) const;
  59. Section* GetSection(Section* section, const TVector<TString>::const_iterator& begin, const TVector<TString>::const_iterator& end, bool add);
  60. bool RemoveAllSections(Section& parent, const TString& name);
  61. bool RemoveSection(Section& parent, const TString& name);
  62. bool RemoveDirective(Section& parent, const TString& name);
  63. TVector<const Section*> GetSections(const Section* section, TPathIterator begin, TPathIterator end) const;
  64. TVector<Section*> GetSections(Section* section, TPathIterator begin, TPathIterator end, bool add);
  65. private:
  66. TVector<TString> Strings;
  67. };
  68. void SectionToStream(const TYandexConfig::Section* section, IOutputStream& stream, ui16 level = 0);