C++ Client-Side SDK
LaunchDarkly SDK
context.hpp
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 #include <launchdarkly/attributes.hpp>
7 #include <launchdarkly/value.hpp>
8 
9 namespace launchdarkly {
10 
11 struct ContextErrors {
12  inline static const std::string kInvalidKind =
13  "\"Kind contained invalid characters. A kind may contain ASCII letters "
14  "or "
15  "numbers, as well as '.', '-', and '_'.\"";
16  // For now disallow an empty key. This may need changed if anonymous key
17  // generation and persistence is added.
18  inline static const std::string kInvalidKey =
19  "\"The key for a context may not be empty.\"";
20  inline static const std::string kMissingKinds =
21  "\"The context must contain at least 1 kind.\"";
22 };
23 
24 class ContextBuilder;
25 
29 class Context final {
30  friend class ContextBuilder;
31 
32  public:
38  [[nodiscard]] std::vector<std::string> const& Kinds() const;
39 
48  [[nodiscard]] launchdarkly::Attributes const& Attributes(
49  std::string const& kind) const;
50 
60  [[nodiscard]] Value const& Get(
61  std::string const& kind,
62  launchdarkly::AttributeReference const& ref) const;
63 
69  [[nodiscard]] bool Valid() const { return valid_; }
70 
74  [[nodiscard]] std::string const& CanonicalKey() const;
75 
81  [[nodiscard]] std::map<std::string, std::string> const& KindsToKeys() const;
82 
90  std::string const& errors() { return errors_; }
91 
92  friend std::ostream& operator<<(std::ostream& out, Context const& context) {
93  if (context.valid_) {
94  out << "{contexts: [";
95  bool first = true;
96  for (auto const& kind : context.attributes_) {
97  if (first) {
98  first = false;
99  } else {
100  out << ", ";
101  }
102  out << "kind: " << kind.first << " attributes: " << kind.second;
103  }
104  out << "]";
105  } else {
106  out << "{invalid: errors: [" << context.errors_ << "]";
107  }
108 
109  return out;
110  }
111 
112  ~Context() = default;
113  Context(Context const& context) = default;
114  Context(Context&& context) = default;
115  Context& operator=(Context const&) = default;
116  Context& operator=(Context&&) = default;
117 
118  private:
122  Context(std::string error_message);
123 
128  Context(std::map<std::string, launchdarkly::Attributes> attributes);
129 
130  std::map<std::string, launchdarkly::Attributes> attributes_;
131  std::vector<std::string> kinds_;
132  std::map<std::string, std::string> kinds_to_keys_;
133  bool valid_ = false;
134  std::string errors_;
135  std::string canonical_key_;
136 
137  std::string make_canonical_key();
138 };
139 
140 } // namespace launchdarkly
Definition: attribute_reference.hpp:34
Definition: attributes.hpp:15
Definition: context_builder.hpp:62
Definition: context.hpp:29
Value const & Get(std::string const &kind, launchdarkly::AttributeReference const &ref) const
Definition: context.cpp:42
bool Valid() const
Definition: context.hpp:69
launchdarkly::Attributes const & Attributes(std::string const &kind) const
Definition: context.cpp:51
std::map< std::string, std::string > const & KindsToKeys() const
Definition: context.cpp:59
std::vector< std::string > const & Kinds() const
Definition: context.cpp:25
std::string const & CanonicalKey() const
Definition: context.cpp:55
std::string const & errors()
Definition: context.hpp:90
Definition: value.hpp:42
Definition: context.hpp:11