aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/configgen/value_converter.cpp
blob: 36843456b25b636df6cf999e5f859f62202993f5 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "value_converter.h"
#include <vespa/config/common/exceptions.h>
#include <vespa/vespalib/locale/c.h>
#include <vespa/vespalib/util/stringfmt.h>

using namespace vespalib;
using namespace vespalib::slime;

namespace config::internal {

template<>
int32_t convertValue(const ::vespalib::slime::Inspector & __inspector) {
    switch (__inspector.type().getId()) {
        case LONG::ID:   return static_cast<int32_t>(__inspector.asLong());
        case DOUBLE::ID: return static_cast<int32_t>(__inspector.asDouble());
        case STRING::ID: return static_cast<int32_t>(strtoll(__inspector.asString().make_string().c_str(), 0, 0));
    }
    throw InvalidConfigException(make_string("Expected int32_t, but got incompatible config type %u", __inspector.type().getId()));
}

template<>
int64_t convertValue(const ::vespalib::slime::Inspector & __inspector) {
    switch (__inspector.type().getId()) {
        case LONG::ID:   return static_cast<int64_t>(__inspector.asLong());
        case DOUBLE::ID: return static_cast<int64_t>(__inspector.asDouble());
        case STRING::ID: return static_cast<int64_t>(strtoll(__inspector.asString().make_string().c_str(), 0, 0));
    }
    throw InvalidConfigException(make_string("Expected int64_t, but got incompatible config type %u", __inspector.type().getId()));
}

template<>
double convertValue(const ::vespalib::slime::Inspector & __inspector) {
    switch (__inspector.type().getId()) {
        case LONG::ID:   return static_cast<double>(__inspector.asLong());
        case DOUBLE::ID: return static_cast<double>(__inspector.asDouble());
        case STRING::ID: return static_cast<double>(vespalib::locale::c::strtod(__inspector.asString().make_string().c_str(), 0));
    }
    throw InvalidConfigException(make_string("Expected double, but got incompatible config type %u", __inspector.type().getId()));
}

template<>
bool convertValue(const ::vespalib::slime::Inspector & __inspector) {
    switch (__inspector.type().getId()) {
        case BOOL::ID:   return __inspector.asBool();
        case STRING::ID:
            vespalib::string s(__inspector.asString().make_string());
            return s.compare("true") == 0 ? true : false;
    }
    throw InvalidConfigException(make_string("Expected bool, but got incompatible config type %u", __inspector.type().getId()));
}

template<>
vespalib::string convertValue(const ::vespalib::slime::Inspector & __inspector) { return __inspector.asString().make_string(); }

void
requireValid(vespalib::stringref __fieldName, const ::vespalib::slime::Inspector & __inspector) {
    if (!__inspector.valid()) {
        throw ::config::InvalidConfigException("Value for '" + __fieldName + "' required but not found");
    }
}

}