aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/config/sax.cpp
blob: 7837fe97b55fe0b9a263c305eb0afd53e8f9c48b (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
#include "sax.h"

using namespace NConfig;

namespace {
    class TSax: public IConfig, public IConfig::IValue {
    public:
        inline TSax(const TConfig& cfg)
            : C_(cfg)
        {
        }

        void DoForEach(IFunc* func) override {
            if (C_.IsA<TArray>()) {
                const TArray& a = C_.Get<TArray>();

                for (size_t i = 0; i < a.size(); ++i) {
                    TSax slave(a[i]);

                    func->Consume(ToString(i), &slave);
                }
            } else if (C_.IsA<TDict>()) {
                const TDict& d = C_.Get<TDict>();

                for (const auto& it : d) {
                    TSax slave(it.second);

                    func->Consume(it.first, &slave);
                }
            }
        }

        TString AsString() override {
            if (C_.IsA<TArray>()) {
                TSax slave(C_.Get<TArray>()[0]);
                return slave.AsString();
            }
            return C_.As<TString>();
        }

        bool AsBool() override {
            return C_.As<bool>();
        }

        IConfig* AsSubConfig() override {
            return this;
        }
        bool IsContainer() const override {
            return C_.IsA<TArray>() || C_.IsA<TDict>();
        }
        void DumpJson(IOutputStream& stream) const override {
            C_.DumpJson(stream);
        }
        void DumpLua(IOutputStream& stream) const override {
            C_.DumpLua(stream);
        }

    private:
        TConfig C_;
    };
}

namespace NConfig {
    THolder<IConfig> ConfigParser(IInputStream& in, const TGlobals& globals) {
        return MakeHolder<TSax>(TConfig::FromStream(in, globals));
    }
}