/usr/include/mongo/s/field_parser-inl.h is in mongodb-dev 1:2.4.9-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mongo/s/field_parser.h"
#include "mongo/util/mongoutils/str.h"
namespace mongo {
using mongoutils::str::stream;
// Extracts an array into a vector
template<typename T>
FieldParser::FieldState FieldParser::extract(BSONObj doc,
const BSONField<vector<T> >& field,
vector<T>* out,
string* errMsg)
{
BSONElement elem = doc[field.name()];
if (elem.eoo()) {
if (field.hasDefault()) {
*out = field.getDefault();
return FIELD_DEFAULT;
}
else {
return FIELD_NONE;
}
}
if (elem.type() == Array) {
BSONArray arr = BSONArray(elem.embeddedObject());
string elErrMsg;
// Append all the new elements to the end of the vector
size_t initialSize = out->size();
out->resize(initialSize + arr.nFields());
int i = 0;
BSONObjIterator objIt(arr);
while (objIt.more()) {
BSONElement next = objIt.next();
BSONField<T> fieldFor(next.fieldName(), out->at(initialSize + i));
if (!FieldParser::extract(arr,
fieldFor,
&out->at(initialSize + i),
&elErrMsg))
{
if (errMsg) {
*errMsg = stream() << "error parsing element " << i << " of field "
<< field() << causedBy(elErrMsg);
}
return FIELD_INVALID;
}
i++;
}
return FIELD_SET;
}
if (errMsg) {
*errMsg = stream() << "wrong type for '" << field() << "' field, expected "
<< "vector array" << ", found " << doc[field.name()].toString();
}
return FIELD_INVALID;
}
// Extracts an object into a map
template<typename K, typename T>
FieldParser::FieldState FieldParser::extract(BSONObj doc,
const BSONField<map<K, T> >& field,
map<K, T>* out,
string* errMsg)
{
BSONElement elem = doc[field.name()];
if (elem.eoo()) {
if (field.hasDefault()) {
*out = field.getDefault();
return FIELD_DEFAULT;
}
else {
return FIELD_NONE;
}
}
if (elem.type() == Object) {
BSONObj obj = elem.embeddedObject();
string elErrMsg;
BSONObjIterator objIt(obj);
while (objIt.more()) {
BSONElement next = objIt.next();
T& value = (*out)[next.fieldName()];
BSONField<T> fieldFor(next.fieldName(), value);
if (!FieldParser::extract(obj, fieldFor, &value, &elErrMsg)) {
if (errMsg) {
*errMsg = stream() << "error parsing map element " << next.fieldName()
<< " of field " << field() << causedBy(elErrMsg);
}
return FIELD_INVALID;
}
}
return FIELD_SET;
}
if (errMsg) {
*errMsg = stream() << "wrong type for '" << field() << "' field, expected "
<< "vector array" << ", found " << doc[field.name()].toString();
}
return FIELD_INVALID;
}
} // namespace mongo
|