summaryrefslogtreecommitdiff
path: root/src/mongo/s/field_parser.h
blob: bddae6605620efc2b04cf4dbe3c3b6400365d67e (plain)
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
/**
 *    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/>.
 */

#pragma once

#include <string>

#include "mongo/bson/bson_field.h"
#include "mongo/bson/util/misc.h" // for Date_t
#include "mongo/db/jsobj.h"

namespace mongo {

    class FieldParser {
    public:
        /**
         * Returns true and fills in 'out' with the contents of the field described by 'field'
         * or with the value in 'def', depending on whether the field is present and has the
         * correct type in 'doc' or not, respectively. Otherwise, if the field exists but has
         * the wrong type, returns false.
         *
         * NOTE ON BSON OWNERSHIP:
         *
         *   The caller must assume that this class will point to data inside 'doc' without
         *   copying it. In practice this means that 'doc' MUST EXIST for as long as 'out'
         *   stays in scope.
         */
        static bool extract(BSONObj doc,
                            const BSONField<bool>& field,
                            bool def,
                            bool* out);

        static bool extract(BSONObj doc,
                            const BSONField<BSONArray>& field,
                            const BSONArray& def,
                            BSONArray* out);

        static bool extract(BSONObj doc,
                            const BSONField<BSONObj>& field,
                            const BSONObj& def,
                            BSONObj* out);

        static bool extract(BSONObj doc,
                            const BSONField<Date_t>& field,
                            const Date_t def,
                            Date_t* out);

        static bool extract(BSONObj doc,
                            const BSONField<string>& field,
                            const string& def,
                            string* out);

        static bool extract(BSONObj doc,
                            const BSONField<OID>& field,
                            const OID& def,
                            OID* out);

        static bool extract(BSONObj doc,
                            const BSONField<long long>& field,
                            const long long& def,
                            long long* out);
    };

} // namespace mongo