xref: /6.6.0/subjson/subdoc/util.cc (revision 9f2dfe6a)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3*     Copyright 2015 Couchbase, Inc
4*
5*   Licensed under the Apache License, Version 2.0 (the "License");
6*   you may not use this file except in compliance with the License.
7*   You may obtain a copy of the License at
8*
9*       http://www.apache.org/licenses/LICENSE-2.0
10*
11*   Unless required by applicable law or agreed to in writing, software
12*   distributed under the License is distributed on an "AS IS" BASIS,
13*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*   See the License for the specific language governing permissions and
15*   limitations under the License.
16*/
17
18#include "util.h"
19#include <stdexcept>
20
21using namespace Subdoc;
22using std::string;
23
24string
25Util::match_match(const Match& m)
26{
27    return m.loc_deepest.to_string();
28}
29
30string
31Util::match_key(const Match& m)
32{
33    if (m.has_key()) {
34        return m.loc_key.to_string();
35    } else {
36        return string();
37    }
38}
39
40string
41Util::match_parent(const Match& m)
42{
43    return m.loc_deepest.to_string();
44}
45
46void
47Util::dump_newdoc(const Result& op, std::ostream& os)
48{
49    auto newdoc = op.newdoc();
50    os << "Dumping doc with "
51       << std::dec << newdoc.size() << " segments" << std::endl;
52    for (size_t ii = 0; ii < newdoc.size(); ++ii) {
53        os << "[" << std::dec << ii << "]: ";
54        os.write(newdoc[ii].at, newdoc[ii].length);
55        os << std::endl;
56    }
57}
58
59const char *
60Util::jsonerr(jsonsl_error_t err)
61{
62#define X(n) if (err == JSONSL_ERROR_##n) { return #n; }
63    JSONSL_XERR;
64#undef X
65    return "UNKNOWN";
66}
67
68namespace std {
69ostream&
70operator<<(ostream& os, const Subdoc::Error::Code& err) {
71    os << "0x" << std::hex << static_cast<int>(err)
72       << " (" << Subdoc::Error(err).description() << ")";
73    return os;
74}
75}
76
77void
78Util::do_assert(const char *e, const char *func, const char *file,
79        int line)
80{
81    std::string ss;
82    ss.append("Assertion failed (").append(e).append(") ");
83    ss.append("function ").append(func).append(", ");
84    ss.append("file ").append(file).append(", line").append(std::to_string(line));
85    throw std::runtime_error(ss);
86}
87