Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/extensions/filters/network/mongo_proxy/bson.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <array>
4
#include <list>
5
#include <memory>
6
#include <string>
7
8
#include "envoy/buffer/buffer.h"
9
#include "envoy/common/pure.h"
10
11
namespace Envoy {
12
namespace Extensions {
13
namespace NetworkFilters {
14
namespace MongoProxy {
15
namespace Bson {
16
17
/**
18
 * Implementation of http://bsonspec.org/spec.html
19
 */
20
class Document;
21
using DocumentSharedPtr = std::shared_ptr<Document>;
22
23
/**
24
 * A BSON document field. This is essentially a variably typed parameter that can be "cast" to
25
 * the correct type via the as*() functions.
26
 */
27
class Field {
28
public:
29
  /**
30
   * Raw field type.
31
   */
32
  enum class Type : uint8_t {
33
    Double = 0x01,
34
    String = 0x02,
35
    Document = 0x03,
36
    Array = 0x04,
37
    Binary = 0x05,
38
    ObjectId = 0x07,
39
    Boolean = 0x08,
40
    Datetime = 0x09,
41
    NullValue = 0x0A,
42
    Regex = 0x0B,
43
    Symbol = 0x0E,
44
    Int32 = 0x10,
45
    Timestamp = 0x11,
46
    Int64 = 0x12
47
  };
48
49
  /**
50
   * 12 byte ObjectId type.
51
   */
52
  using ObjectId = std::array<uint8_t, 12>;
53
54
  /**
55
   * Regex type.
56
   */
57
  struct Regex {
58
0
    bool operator==(const Regex& rhs) const {
59
0
      return pattern_ == rhs.pattern_ && options_ == rhs.options_;
60
0
    }
61
62
    std::string pattern_;
63
    std::string options_;
64
  };
65
66
10.4k
  virtual ~Field() = default;
67
68
  virtual double asDouble() const PURE;
69
  virtual const std::string& asString() const PURE;
70
  virtual const std::string& asSymbol() const PURE;
71
  virtual const Document& asDocument() const PURE;
72
  virtual const Document& asArray() const PURE;
73
  virtual const std::string& asBinary() const PURE;
74
  virtual const ObjectId& asObjectId() const PURE;
75
  virtual bool asBoolean() const PURE;
76
  virtual int64_t asDatetime() const PURE;
77
  virtual const Regex& asRegex() const PURE;
78
  virtual int32_t asInt32() const PURE;
79
  virtual int64_t asTimestamp() const PURE;
80
  virtual int64_t asInt64() const PURE;
81
82
  virtual int32_t byteSize() const PURE;
83
  virtual void encode(Buffer::Instance& output) const PURE;
84
  virtual const std::string& key() const PURE;
85
  virtual bool operator==(const Field& rhs) const PURE;
86
  virtual std::string toString() const PURE;
87
  virtual Type type() const PURE;
88
};
89
90
using FieldPtr = std::unique_ptr<Field>;
91
92
/**
93
 * A BSON document. add*() is used to add strongly typed fields.
94
 */
95
class Document {
96
public:
97
29.6k
  virtual ~Document() = default;
98
99
  virtual DocumentSharedPtr addDouble(const std::string& key, double value) PURE;
100
  virtual DocumentSharedPtr addString(const std::string& key, std::string&& value) PURE;
101
  virtual DocumentSharedPtr addSymbol(const std::string& key, std::string&& value) PURE;
102
  virtual DocumentSharedPtr addDocument(const std::string& key, DocumentSharedPtr value) PURE;
103
  virtual DocumentSharedPtr addArray(const std::string& key, DocumentSharedPtr value) PURE;
104
  virtual DocumentSharedPtr addBinary(const std::string& key, std::string&& value) PURE;
105
  virtual DocumentSharedPtr addObjectId(const std::string& key, Field::ObjectId&& value) PURE;
106
  virtual DocumentSharedPtr addBoolean(const std::string& key, bool value) PURE;
107
  virtual DocumentSharedPtr addDatetime(const std::string& key, int64_t value) PURE;
108
  virtual DocumentSharedPtr addNull(const std::string& key) PURE;
109
  virtual DocumentSharedPtr addRegex(const std::string& key, Field::Regex&& value) PURE;
110
  virtual DocumentSharedPtr addInt32(const std::string& key, int32_t value) PURE;
111
  virtual DocumentSharedPtr addTimestamp(const std::string& key, int64_t value) PURE;
112
  virtual DocumentSharedPtr addInt64(const std::string& key, int64_t value) PURE;
113
114
  virtual bool operator==(const Document& rhs) const PURE;
115
  virtual int32_t byteSize() const PURE;
116
  virtual void encode(Buffer::Instance& output) const PURE;
117
  virtual const Field* find(const std::string& name) const PURE;
118
  virtual const Field* find(const std::string& name, Field::Type type) const PURE;
119
  virtual std::string toString() const PURE;
120
  virtual const std::list<FieldPtr>& values() const PURE;
121
};
122
123
} // namespace Bson
124
} // namespace MongoProxy
125
} // namespace NetworkFilters
126
} // namespace Extensions
127
} // namespace Envoy