~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/s/field_parser.cpp

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *    Copyright (C) 2012 10gen Inc.
 
3
 *
 
4
 *    This program is free software: you can redistribute it and/or  modify
 
5
 *    it under the terms of the GNU Affero General Public License, version 3,
 
6
 *    as published by the Free Software Foundation.
 
7
 *
 
8
 *    This program is distributed in the hope that it will be useful,
 
9
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *    GNU Affero General Public License for more details.
 
12
 *
 
13
 *    You should have received a copy of the GNU Affero General Public License
 
14
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include "mongo/s/field_parser.h"
 
18
#include "mongo/util/mongoutils/str.h"
 
19
 
 
20
namespace mongo {
 
21
 
 
22
    using mongoutils::str::stream;
 
23
 
 
24
    template<class T>
 
25
    void _genFieldErrMsg(const BSONObj& doc,
 
26
                         const BSONField<T>& field,
 
27
                         const string expected,
 
28
                         string* errMsg)
 
29
    {
 
30
        if (!errMsg) return;
 
31
        *errMsg = stream() << "wrong type for '" << field() << "' field, expected " << expected
 
32
                           << ", found " << doc[field.name()].toString();
 
33
    }
 
34
 
 
35
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
36
                              const BSONField<bool>& field,
 
37
                              bool* out,
 
38
                              string* errMsg)
 
39
    {
 
40
        BSONElement elem = doc[field.name()];
 
41
        if (elem.eoo()) {
 
42
            if (field.hasDefault()) {
 
43
                *out = field.getDefault();
 
44
                return FIELD_DEFAULT;
 
45
            }
 
46
            else {
 
47
                return FIELD_NONE;
 
48
            }
 
49
        }
 
50
 
 
51
        if (elem.type() == Bool) {
 
52
            *out = elem.boolean();
 
53
            return FIELD_SET;
 
54
        }
 
55
 
 
56
        _genFieldErrMsg(doc, field, "boolean", errMsg);
 
57
        return FIELD_INVALID;
 
58
    }
 
59
 
 
60
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
61
                              const BSONField<BSONArray>& field,
 
62
                              BSONArray* out,
 
63
                              string* errMsg)
 
64
    {
 
65
        BSONElement elem = doc[field.name()];
 
66
        if (elem.eoo()) {
 
67
            if (field.hasDefault()) {
 
68
                *out = field.getDefault();
 
69
                return FIELD_DEFAULT;
 
70
            }
 
71
            else {
 
72
                return FIELD_NONE;
 
73
            }
 
74
        }
 
75
 
 
76
        if (elem.type() == Array) {
 
77
            *out = BSONArray(elem.embeddedObject().getOwned());
 
78
            return FIELD_SET;
 
79
        }
 
80
 
 
81
        _genFieldErrMsg(doc, field, "array", errMsg);
 
82
        return FIELD_INVALID;
 
83
    }
 
84
 
 
85
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
86
                              const BSONField<BSONObj>& field,
 
87
                              BSONObj* out,
 
88
                              string* errMsg)
 
89
    {
 
90
        BSONElement elem = doc[field.name()];
 
91
        if (elem.eoo()) {
 
92
            if (field.hasDefault()) {
 
93
                *out = field.getDefault();
 
94
                return FIELD_DEFAULT;
 
95
            }
 
96
            else {
 
97
                return FIELD_NONE;
 
98
            }
 
99
        }
 
100
 
 
101
        if (elem.type() == Object) {
 
102
            *out = elem.embeddedObject().getOwned();
 
103
            return FIELD_SET;
 
104
        }
 
105
 
 
106
        _genFieldErrMsg(doc, field, "object", errMsg);
 
107
        return FIELD_INVALID;
 
108
    }
 
109
 
 
110
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
111
                              const BSONField<Date_t>& field,
 
112
                              Date_t* out,
 
113
                              string* errMsg)
 
114
    {
 
115
        BSONElement elem = doc[field.name()];
 
116
        if (elem.eoo()) {
 
117
            if (field.hasDefault()) {
 
118
                *out = field.getDefault();
 
119
                return FIELD_DEFAULT;
 
120
            }
 
121
            else {
 
122
                return FIELD_NONE;
 
123
            }
 
124
        }
 
125
 
 
126
        if (elem.type() == Date) {
 
127
            *out = elem.date();
 
128
            return FIELD_SET;
 
129
        }
 
130
 
 
131
        _genFieldErrMsg(doc, field, "date or timestamp", errMsg);
 
132
        return FIELD_INVALID;
 
133
    }
 
134
 
 
135
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
136
                              const BSONField<string>& field,
 
137
                              string* out,
 
138
                              string* errMsg)
 
139
    {
 
140
        BSONElement elem = doc[field.name()];
 
141
        if (elem.eoo()) {
 
142
            if (field.hasDefault()) {
 
143
                *out = field.getDefault();
 
144
                return FIELD_DEFAULT;
 
145
            }
 
146
            else {
 
147
                return FIELD_NONE;
 
148
            }
 
149
        }
 
150
 
 
151
        if (elem.type() == String) {
 
152
            *out = elem.valuestr();
 
153
            return FIELD_SET;
 
154
        }
 
155
 
 
156
        _genFieldErrMsg(doc, field, "string", errMsg);
 
157
        return FIELD_INVALID;
 
158
    }
 
159
 
 
160
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
161
                              const BSONField<OID>& field,
 
162
                              OID* out,
 
163
                              string* errMsg)
 
164
    {
 
165
        BSONElement elem = doc[field.name()];
 
166
        if (elem.eoo()) {
 
167
            if (field.hasDefault()) {
 
168
                *out = field.getDefault();
 
169
                return FIELD_DEFAULT;
 
170
            }
 
171
            else {
 
172
                return FIELD_NONE;
 
173
            }
 
174
        }
 
175
 
 
176
        if (elem.type() == jstOID) {
 
177
            *out = elem.__oid();
 
178
            return FIELD_SET;
 
179
        }
 
180
 
 
181
        _genFieldErrMsg(doc, field, "OID", errMsg);
 
182
        return FIELD_INVALID;
 
183
    }
 
184
 
 
185
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
186
                              const BSONField<int>& field,
 
187
                              int* out,
 
188
                              string* errMsg)
 
189
    {
 
190
        BSONElement elem = doc[field.name()];
 
191
        if (elem.eoo()) {
 
192
            if (field.hasDefault()) {
 
193
                *out = field.getDefault();
 
194
                return FIELD_DEFAULT;
 
195
            }
 
196
            else {
 
197
                return FIELD_NONE;
 
198
            }
 
199
        }
 
200
 
 
201
        if (elem.type() == NumberInt) {
 
202
            *out = elem.numberInt();
 
203
            return FIELD_SET;
 
204
        }
 
205
 
 
206
        _genFieldErrMsg(doc, field, "integer", errMsg);
 
207
        return FIELD_INVALID;
 
208
    }
 
209
 
 
210
    FieldParser::FieldState FieldParser::extractNumber(BSONObj doc,
 
211
                                    const BSONField<int>& field,
 
212
                                    int* out,
 
213
                                    string* errMsg)
 
214
    {
 
215
        BSONElement elem = doc[field.name()];
 
216
        if (elem.eoo()) {
 
217
            if (field.hasDefault()) {
 
218
                *out = field.getDefault();
 
219
                return FIELD_DEFAULT;
 
220
            }
 
221
            else {
 
222
                return FIELD_NONE;
 
223
            }
 
224
        }
 
225
 
 
226
        if (elem.isNumber()) {
 
227
            *out = elem.numberInt();
 
228
            return FIELD_SET;
 
229
        }
 
230
 
 
231
        _genFieldErrMsg(doc, field, "number", errMsg);
 
232
        return FIELD_INVALID;
 
233
    }
 
234
 
 
235
    FieldParser::FieldState FieldParser::extract(BSONObj doc,
 
236
                              const BSONField<long long>& field,
 
237
                              long long* out,
 
238
                              string* errMsg)
 
239
    {
 
240
        BSONElement elem = doc[field.name()];
 
241
        if (elem.eoo()) {
 
242
            if (field.hasDefault()) {
 
243
                *out = field.getDefault();
 
244
                return FIELD_DEFAULT;
 
245
            }
 
246
            else {
 
247
                return FIELD_NONE;
 
248
            }
 
249
        }
 
250
 
 
251
        if (elem.type() == NumberLong) {
 
252
            *out = elem.numberLong();
 
253
            return FIELD_SET;
 
254
        }
 
255
 
 
256
        _genFieldErrMsg(doc, field, "long", errMsg);
 
257
        return FIELD_INVALID;
 
258
    }
 
259
 
 
260
    FieldParser::FieldState FieldParser::extractNumber(BSONObj doc,
 
261
                                    const BSONField<long long>& field,
 
262
                                    long long* out,
 
263
                                    string* errMsg)
 
264
    {
 
265
        BSONElement elem = doc[field.name()];
 
266
        if (elem.eoo()) {
 
267
            if (field.hasDefault()) {
 
268
                *out = field.getDefault();
 
269
                return FIELD_DEFAULT;
 
270
            }
 
271
            else {
 
272
                return FIELD_NONE;
 
273
            }
 
274
        }
 
275
 
 
276
        if (elem.isNumber()) {
 
277
            *out = elem.numberLong();
 
278
            return FIELD_SET;
 
279
        }
 
280
 
 
281
        _genFieldErrMsg(doc, field, "number", errMsg);
 
282
        return FIELD_INVALID;
 
283
    }
 
284
 
 
285
} // namespace mongo