summaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/common/configparser.h
blob: 77b5bf7ddaa098ec0af57a371834ab4a34ba38b8 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/util/stringfmt.h>
#include <map>
#include <set>
#include <vector>
#include <cerrno>
#include <cstdint>

namespace config {

/**
 * To reduce the need for code in autogenerated config classes, these
 * helper functions exist to help parsing.
 */
class ConfigParser {
public:
    typedef std::vector<vespalib::string> vsvector;
private:
    static vsvector getLinesForKey( const vespalib::stringref & key, const vsvector & config);

    static std::vector<vsvector> splitArray( const vsvector & config);
    static std::map<vespalib::string, vsvector> splitMap( const vsvector & config);

    static vespalib::string deQuote(const vespalib::string & source);
    static void throwNoDefaultValue(const vespalib::stringref & key);

    template<typename T>
    static T convert(const vsvector &);

    static vespalib::string arrayToString(const vsvector &);

    template<typename T, typename V>
    static T parseInternal(const vespalib::stringref & key, const V & config);
    template<typename T, typename V>
    static T parseInternal(const vespalib::stringref & key, const V & config, T defaultValue);

    template<typename T, typename V>
    static std::vector<T> parseArrayInternal(const vespalib::stringref & key, const V & config);
    template<typename T, typename V>
    static std::map<vespalib::string, T> parseMapInternal(const vespalib::stringref & key, const V & config);
    template<typename T, typename V>
    static T parseStructInternal(const vespalib::stringref & key, const V & config);

public:
    static void stripLinesForKey(const vespalib::stringref & key,
                                 std::set<vespalib::string>& config);
    static vespalib::string stripWhitespace(const vespalib::stringref & source);

    template<typename T>
    static T parse(const vespalib::stringref & key, const vsvector & config) {
        return parseInternal<T, vsvector>(key, config);
    }
    template<typename T>
    static T parse(const vespalib::stringref & key, const vsvector & config, T defaultValue) {
        return parseInternal(key, config, defaultValue);
    }

    template<typename T>
    static std::vector<T> parseArray(const vespalib::stringref & key, const vsvector & config) {
        return parseArrayInternal<T, vsvector>(key, config);
    }

    template<typename T>
    static std::map<vespalib::string, T> parseMap(const vespalib::stringref & key, const vsvector & config) {
        return parseMapInternal<T, vsvector>(key, config);
    }

    template<typename T>
    static T parseStruct(const vespalib::stringref & key, const vsvector & config) {
        return parseStructInternal<T, vsvector>(key, config);
    }

};

template<typename T, typename V>
T
ConfigParser::parseInternal(const vespalib::stringref & key, const V & config)
{
    V lines = getLinesForKey(key, config);

    if (lines.size() == 0) {
        throwNoDefaultValue(key);
    }
    return convert<T>(lines);
}

template<typename T, typename V>
T
ConfigParser::parseInternal(const vespalib::stringref & key, const V & config, T defaultValue)
{
    V lines = getLinesForKey(key, config);

    if (lines.size() == 0) {
        return defaultValue;
    }

    return convert<T>(lines);
}

template<typename T>
T
ConfigParser::convert(const vsvector & lines) {
    return T(lines);
}

template<typename T, typename V>
std::map<vespalib::string, T>
ConfigParser::parseMapInternal(const vespalib::stringref & key, const V & config)
{
    V lines = getLinesForKey(key, config);
    typedef std::map<vespalib::string, V> SplittedMap;
    SplittedMap s = splitMap(lines);
    std::map<vespalib::string, T> retval;
    for (typename SplittedMap::iterator it(s.begin()), mt(s.end()); it != mt; it++) {
        retval[it->first] = convert<T>(it->second);
    }
    return retval;
}

template<typename T, typename V>
std::vector<T>
ConfigParser::parseArrayInternal(const vespalib::stringref & key, const V & config)
{
    V lines = getLinesForKey(key, config);
    std::vector<V> split = splitArray(lines);

    std::vector<T> retval;
    for (uint32_t i = 0; i < split.size(); i++) {
        retval.push_back(convert<T>(split[i]));
    }

    return retval;
}

template<typename T, typename V>
T
ConfigParser::parseStructInternal(const vespalib::stringref & key, const V & config)
{
    V lines = getLinesForKey(key, config);

    return convert<T>(lines);
}

template<>
bool
ConfigParser::convert<bool>(const vsvector & config);

template<>
int32_t
ConfigParser::convert<int32_t>(const vsvector & config);

template<>
int64_t
ConfigParser::convert<int64_t>(const vsvector & config);

template<>
double
ConfigParser::convert<double>(const vsvector & config);

template<>
vespalib::string
ConfigParser::convert<vespalib::string>(const vsvector & config);

} // config