C++ Server-Side SDK
LaunchDarkly SDK
Loading...
Searching...
No Matches
attributes.hpp
1#pragma once
2
3#include <string>
4#include <unordered_set>
5
6#include <launchdarkly/attribute_reference.hpp>
7#include <launchdarkly/value.hpp>
8
9namespace launchdarkly {
10
15class Attributes final {
16 public:
21 std::string const& Key() const;
22
29 std::string const& Name() const;
30
35 bool Anonymous() const;
36
41 Value const& CustomAttributes() const;
42
47 AttributeReference::SetType const& PrivateAttributes() const;
48
57 launchdarkly::AttributeReference const& ref) const {
58 if (!ref.Valid()) {
59 // Cannot index by invalid references.
61 }
62 if (ref.IsKind()) {
63 // Cannot access kind.
65 }
66
67 if (ref.Depth() == 1) {
68 // Handle built-in attributes.
69 if (ref.Component(0) == "key") {
70 return key_;
71 }
72 if (ref.Component(0) == "name") {
73 return name_;
74 }
75 if (ref.Component(0) == "anonymous") {
76 return anonymous_;
77 }
78 }
79
80 launchdarkly::Value const* node = &custom_attributes_;
81 bool found = true;
82 for (size_t index = 0; index < ref.Depth(); index++) {
83 auto const& component = ref.Component(index);
84 if (node->IsObject()) {
85 auto const& map = node->AsObject();
86 if (auto search = map.Find(component); search != map.end()) {
87 node = &search->second;
88 } else {
89 found = false;
90 break;
91 }
92 } else {
93 found = false;
94 }
95 }
96 if (!found) {
98 }
99 return *node;
100 }
101
112 Attributes(std::string key,
113 std::optional<std::string> name,
114 bool anonymous,
115 launchdarkly::Value attributes,
116 AttributeReference::SetType private_attributes =
117 AttributeReference::SetType())
118 : key_(std::move(key)),
119 name_(std::move(name)),
120 anonymous_(anonymous),
121 private_attributes_(std::move(private_attributes)),
122 custom_attributes_(std::move(attributes)) {}
123
124 friend std::ostream& operator<<(std::ostream& out,
125 Attributes const& attrs) {
126 out << "{key: " << attrs.key_ << ", "
127 << " name: " << attrs.name_ << " anonymous: " << attrs.anonymous_
128 << " private: [";
129 bool first = true;
130 for (auto const& private_attribute : attrs.private_attributes_) {
131 if (first) {
132 first = false;
133 } else {
134 out << ", ";
135 }
136 out << private_attribute;
137 }
138 out << "] "
139 << " custom: " << attrs.custom_attributes_ << "}";
140
141 return out;
142 }
143
144 Attributes(Attributes const& context) = default;
145
146 Attributes(Attributes&& context) = default;
147
148 ~Attributes() = default;
149
150 Attributes& operator=(Attributes const&) = default;
151
152 Attributes& operator=(Attributes&&) = default;
153
154 private:
155 // Built-in attributes.
158 launchdarkly::Value anonymous_;
159 AttributeReference::SetType private_attributes_;
160
161 launchdarkly::Value custom_attributes_;
162
163 // Kinds are contained at the context level, not inside attributes.
164};
165
166} // namespace launchdarkly
Definition attribute_reference.hpp:34
bool IsKind() const
Definition attribute_reference.cpp:215
bool Valid() const
Definition attribute_reference.cpp:219
std::size_t Depth() const
Definition attribute_reference.cpp:211
std::string const & Component(std::size_t depth) const
Definition attribute_reference.cpp:204
Definition attributes.hpp:15
Value const & CustomAttributes() const
Definition attributes.cpp:17
bool Anonymous() const
Definition attributes.cpp:13
AttributeReference::SetType const & PrivateAttributes() const
Definition attributes.cpp:21
Attributes(std::string key, std::optional< std::string > name, bool anonymous, launchdarkly::Value attributes, AttributeReference::SetType private_attributes=AttributeReference::SetType())
Definition attributes.hpp:112
launchdarkly::Value const & Get(launchdarkly::AttributeReference const &ref) const
Definition attributes.hpp:56
std::string const & Key() const
Definition attributes.cpp:5
std::string const & Name() const
Definition attributes.cpp:9
Definition value.hpp:42
Object const & AsObject() const
Definition value.cpp:121
bool IsObject() const
Definition value.cpp:76
static Value const & Null()
Definition value.cpp:80