CCF
Loading...
Searching...
No Matches
entity_id.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/json.h"
7
8#include <string>
9
10namespace ccf
11{
12 template <typename FmtExtender = void>
13 struct EntityId
14 {
15 public:
16 // The underlying value type should be blit-serialisable so that it can be
17 // written to and read from the ring buffer
18 static constexpr size_t LENGTH = 64; // hex-encoded SHA-256 hash
19 using Value = std::string; // < hex-encoded hash
20
21 private:
22 Value id;
23
24 public:
25 EntityId() = default;
26 EntityId(const EntityId& id_) = default;
27 EntityId(const Value& id_) : id(id_) {}
28 EntityId(Value&& id_) : id(std::move(id_)) {}
29 EntityId(EntityId&& id_) noexcept : id(std::move(id_)) {}
30 EntityId& operator=(EntityId&& other) = default;
31
32 operator std::string() const
33 {
34 return id;
35 }
36
38 {
39 if (this != &other)
40 {
41 id = other.id;
42 }
43 return *this;
44 }
45
47 {
48 id = id_;
49 return *this;
50 }
51
52 bool operator==(const EntityId& other) const
53 {
54 return id == other.id;
55 }
56
57 bool operator!=(const EntityId& other) const
58 {
59 return !(*this == other);
60 }
61
62 bool operator<(const EntityId& other) const
63 {
64 return id < other.id;
65 }
66
68 {
69 return id;
70 }
71
72 [[nodiscard]] const Value& value() const
73 {
74 return id;
75 }
76
77 [[nodiscard]] char const* data() const
78 {
79 return id.data();
80 }
81
82 [[nodiscard]] size_t size() const
83 {
84 return id.size();
85 }
86 };
87
88 template <typename FmtExtender>
89 inline void to_json(nlohmann::json& j, const EntityId<FmtExtender>& entity_id)
90 {
91 j = entity_id.value();
92 }
93
94 template <typename FmtExtender>
95 inline void from_json(
96 const nlohmann::json& j, EntityId<FmtExtender>& entity_id)
97 {
98 if (j.is_string())
99 {
100 entity_id = j.get<std::string>();
101 }
102 else
103 {
104 throw ccf::JsonParseError(fmt::format(
105 "{} should be hex-encoded string: {}",
106 FmtExtender::ID_LABEL,
107 j.dump()));
108 }
109 }
110
111 template <typename FmtExtender>
112 inline std::string schema_name(
113 [[maybe_unused]] const EntityId<FmtExtender>* entity_id_type)
114 {
115 return FmtExtender::ID_LABEL;
116 }
117
118 template <typename FmtExtender>
119 inline void fill_json_schema(
120 nlohmann::json& schema,
121 [[maybe_unused]] const EntityId<FmtExtender>* entity_id_type)
122 {
123 schema["type"] = "string";
124
125 // According to the spec, "format is an open value, so you can use any
126 // formats, even not those defined by the OpenAPI Specification"
127 // https://swagger.io/docs/specification/data-models/data-types/#format
128 schema["format"] = "hex";
129 schema["pattern"] =
130 fmt::format("^[a-f0-9]{{{}}}$", EntityId<FmtExtender>::LENGTH);
131 }
132
134 {
135 static std::string format(const std::string& core)
136 {
137 return fmt::format("m[{}]", core);
138 }
139
140 static constexpr auto ID_LABEL = "MemberId";
141 };
143
145 {
146 static std::string format(const std::string& core)
147 {
148 return fmt::format("u[{}]", core);
149 }
150
151 static constexpr auto ID_LABEL = "UserId";
152 };
154
156 {
157 static std::string format(const std::string& core)
158 {
159 return fmt::format("n[{}]", core);
160 }
161
162 static constexpr auto ID_LABEL = "NodeId";
163 };
165}
166
167// NOLINTBEGIN(cert-dcl58-cpp)
168namespace std
169{
170 template <typename FmtExtender>
171 static inline std::ostream& operator<<(
172 std::ostream& os, const ccf::EntityId<FmtExtender>& entity_id)
173 {
174 if constexpr (std::is_same_v<FmtExtender, void>)
175 {
176 os << entity_id.value();
177 }
178 else
179 {
180 os << FmtExtender::format(entity_id.value());
181 }
182 return os;
183 }
184
185 template <typename FmtExtender>
186 struct hash<ccf::EntityId<FmtExtender>>
187 {
188 size_t operator()(const ccf::EntityId<FmtExtender>& entity_id) const
189 {
190 return std::hash<std::string>{}(entity_id.value());
191 }
192 };
193}
194// NOLINTEND(cert-dcl58-cpp)
195
196FMT_BEGIN_NAMESPACE
197template <typename FmtExtender>
198struct formatter<ccf::EntityId<FmtExtender>>
199{
200 template <typename ParseContext>
201 constexpr auto parse(ParseContext& ctx)
202 {
203 return ctx.begin();
204 }
205
206 template <typename FormatContext>
207 auto format(const ccf::EntityId<FmtExtender>& v, FormatContext& ctx) const
208 {
209 std::stringstream ss;
210 ss << v;
211 return format_to(ctx.out(), "{}", ss.str());
212 }
213};
214FMT_END_NAMESPACE
215
216namespace ccf::kv::serialisers
217{
218 template <typename FmtExtender>
219 struct BlitSerialiser<ccf::EntityId<FmtExtender>>
220 {
222 const ccf::EntityId<FmtExtender>& entity_id)
223 {
224 const auto& data = entity_id.value();
225 return SerialisedEntry(data.begin(), data.end());
226 }
227
229 const SerialisedEntry& data)
230 {
231 return ccf::EntityId<FmtExtender>(std::string(data.begin(), data.end()));
232 }
233 };
234}
Definition json.h:26
Definition sha256_hash.h:88
ccf::ByteVector SerialisedEntry
Definition serialised_entry.h:8
Definition app_interface.h:14
std::string schema_name(const ClaimsDigest *claims_digest_type)
Definition claims_digest.h:59
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
void to_json(nlohmann::json &j, const ClaimsDigest &hash)
Definition claims_digest.h:49
void fill_json_schema(nlohmann::json &schema, const ClaimsDigest *claims_digest_type)
Definition claims_digest.h:65
STL namespace.
Definition entity_id.h:14
EntityId(const EntityId &id_)=default
EntityId(const Value &id_)
Definition entity_id.h:27
static constexpr size_t LENGTH
Definition entity_id.h:18
EntityId(EntityId &&id_) noexcept
Definition entity_id.h:29
char const * data() const
Definition entity_id.h:77
size_t size() const
Definition entity_id.h:82
bool operator<(const EntityId &other) const
Definition entity_id.h:62
EntityId(Value &&id_)
Definition entity_id.h:28
EntityId & operator=(EntityId &&other)=default
const Value & value() const
Definition entity_id.h:72
EntityId()=default
std::string Value
Definition entity_id.h:19
Value & value()
Definition entity_id.h:67
EntityId & operator=(const Value &id_)
Definition entity_id.h:46
bool operator!=(const EntityId &other) const
Definition entity_id.h:57
bool operator==(const EntityId &other) const
Definition entity_id.h:52
EntityId & operator=(const EntityId &other)
Definition entity_id.h:37
Definition entity_id.h:134
static constexpr auto ID_LABEL
Definition entity_id.h:140
static std::string format(const std::string &core)
Definition entity_id.h:135
Definition entity_id.h:156
static std::string format(const std::string &core)
Definition entity_id.h:157
static constexpr auto ID_LABEL
Definition entity_id.h:162
Definition entity_id.h:145
static constexpr auto ID_LABEL
Definition entity_id.h:151
static std::string format(const std::string &core)
Definition entity_id.h:146
static SerialisedEntry to_serialised(const ccf::EntityId< FmtExtender > &entity_id)
Definition entity_id.h:221
static ccf::EntityId< FmtExtender > from_serialised(const SerialisedEntry &data)
Definition entity_id.h:228
Definition blit_serialiser.h:14
auto format(const ccf::EntityId< FmtExtender > &v, FormatContext &ctx) const
Definition entity_id.h:207
constexpr auto parse(ParseContext &ctx)
Definition entity_id.h:201
size_t operator()(const ccf::EntityId< FmtExtender > &entity_id) const
Definition entity_id.h:188