blob: 81883319e9d81768e749c0de50f7c4a3a9581ac4 (
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/config/common/exceptions.h>
#include "value_converter.h"
using namespace vespalib;
using namespace vespalib::slime;
namespace config {
namespace 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("Expected int32_t, but got incompatible config type " + __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("Expected int64_t, but got incompatible config type " + __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>(strtod(__inspector.asString().make_string().c_str(), 0));
}
throw InvalidConfigException("Expected double, but got incompatible config type " + __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("Expected bool, but got incompatible config type " + __inspector.type().getId());
}
template<>
vespalib::string convertValue(const ::vespalib::slime::Inspector & __inspector) { return __inspector.asString().make_string(); }
void
requireValid(const vespalib::string & __fieldName, const ::vespalib::slime::Inspector & __inspector) {
if (!__inspector.valid()) {
throw ::config::InvalidConfigException("Value for '" + __fieldName + "' required but not found");
}
}
}
}
|