2
* Copyright (C) 2012 10gen Inc.
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.
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.
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/>.
21
#include "mongo/bson/bson_field.h"
22
#include "mongo/bson/util/misc.h" // for Date_t
23
#include "mongo/db/jsobj.h"
30
* Returns true and fills in 'out' with the contents of the field described by 'field'
31
* or with the value in 'def', depending on whether the field is present and has the
32
* correct type in 'doc' or not, respectively. Otherwise, if the field exists but has
33
* the wrong type, returns false.
35
* NOTE ON BSON OWNERSHIP:
37
* The caller must assume that this class will point to data inside 'doc' without
38
* copying it. In practice this means that 'doc' MUST EXIST for as long as 'out'
43
// The field is present but has the wrong type
46
// The field is present and has the correct type
49
// The field is absent in the BSON object but set from default
52
// The field is absent and no default was specified
56
static FieldState extract(BSONObj doc,
57
const BSONField<bool>& field,
59
string* errMsg = NULL);
61
static FieldState extract(BSONObj doc,
62
const BSONField<BSONArray>& field,
64
string* errMsg = NULL);
66
static FieldState extract(BSONObj doc,
67
const BSONField<BSONObj>& field,
69
string* errMsg = NULL);
71
static FieldState extract(BSONObj doc,
72
const BSONField<Date_t>& field,
74
string* errMsg = NULL);
76
static FieldState extract(BSONObj doc,
77
const BSONField<string>& field,
79
string* errMsg = NULL);
81
static FieldState extract(BSONObj doc,
82
const BSONField<OID>& field,
84
string* errMsg = NULL);
86
static FieldState extract(BSONObj doc,
87
const BSONField<int>& field,
89
string* errMsg = NULL);
91
static FieldState extract(BSONObj doc,
92
const BSONField<long long>& field,
94
string* errMsg = NULL);
97
* The following extractNumber methods do implicit conversion between any numeric type and
98
* the BSONField type. This can be useful when an exact numeric type is not needed, for
99
* example if the field is sometimes modified from the shell which can change the type.
101
static FieldState extractNumber(BSONObj doc,
102
const BSONField<int>& field,
104
string* errMsg = NULL);
106
static FieldState extractNumber(BSONObj doc,
107
const BSONField<long long>& field,
109
string* errMsg = NULL);
112
* The following extract methods are templatized to handle extraction of vectors and
113
* maps of sub-objects. Keys in the map should be StringData compatible.
115
* It's possible to nest extraction of vectors and maps to any depth, i.e:
117
* vector<map<string,vector<string> > > val;
118
* FieldParser::extract(doc, field, val, &val);
121
static FieldState extract(BSONObj doc,
122
const BSONField<vector<T> >& field,
124
string* errMsg = NULL);
126
template<typename K, typename T>
127
static FieldState extract(BSONObj doc,
128
const BSONField<map<K, T> >& field,
130
string* errMsg = NULL);
135
// Inline functions for templating
136
#include "field_parser-inl.h"