aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yconf/patcher/unstrict_config.h
blob: 4990fbe86d7972b7aedd509dab82cf7382a5c060 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once

#include <library/cpp/charset/ci_string.h>
#include <library/cpp/json/json_value.h>
#include <library/cpp/yconf/conf.h>

#include <util/string/vector.h>

class TUnstrictConfig
   : public TYandexConfig {
public:
    const Section* GetSection(const TString& path);
    bool AddSection(const TString& path);
    TString GetValue(const TString& path) const;
    //return true if value was changed;
    bool SetValue(const TString& path, const TString& value);
    bool Remove(const TString& path);
    bool RemoveAll(const TString& path);
    bool PatchEntry(const TString& path, const TString& value, const TString& prefix = "");

    [[nodiscard]] bool ParseJson(const NJson::TJsonValue& json);

    TString ToString() const;
    NJson::TJsonValue ToJson() const;

public:
    static void ToJsonPatch(const Section& section, NJson::TJsonValue& result, const TString& preffix);
    static void ToJson(const Section& section, NJson::TJsonValue& result);
    static void ToJson(const TYandexConfig& config, NJson::TJsonValue& result);
    static void ToJson(const TString& section, NJson::TJsonValue& result);

    template <class T>
    static NJson::TJsonValue ToJson(const T& entity) {
        NJson::TJsonValue result;
        ToJson(entity, result);
        return result;
    }

protected:
    bool OnBeginSection(Section& sec) override;

private:
    struct TPathUnit {
        TCiString Name;
        size_t BeginIndex;
        size_t EndIndex;

        inline TPathUnit(const TCiString& name, size_t beginIndex, size_t endIndex)
            : Name(name)
            , BeginIndex(beginIndex)
            , EndIndex(endIndex)
        {
            Y_VERIFY(EndIndex >= BeginIndex);
        }
        inline TPathUnit(const TCiString& name, size_t index)
            : TPathUnit(name, index, index)
        {
        }
        inline TPathUnit(const TCiString& name)
            : TPathUnit(name, 0)
        {
        }
    };

    using TPathIterator = TVector<TString>::const_iterator;

private:
    bool ParseJson(const NJson::TJsonValue& json, const TString& path);
    TPathUnit ProcessPathUnit(const TString& element) const;
    const Section* GetSection(const Section* section, const TVector<TString>::const_iterator& begin, const TVector<TString>::const_iterator& end) const;
    Section* GetSection(Section* section, const TVector<TString>::const_iterator& begin, const TVector<TString>::const_iterator& end, bool add);
    bool RemoveAllSections(Section& parent, const TString& name);
    bool RemoveSection(Section& parent, const TString& name);
    bool RemoveDirective(Section& parent, const TString& name);

    TVector<const Section*> GetSections(const Section* section, TPathIterator begin, TPathIterator end) const;
    TVector<Section*> GetSections(Section* section, TPathIterator begin, TPathIterator end, bool add);

private:
    TVector<TString> Strings;
};

void SectionToStream(const TYandexConfig::Section* section, IOutputStream& stream, ui16 level = 0);