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
30 inline operator std::string() const
31 {
32 return id;
33 }
34
35 void operator=(const EntityId& other)
36 {
37 id = other.id;
38 }
39
40 void operator=(const Value& id_)
41 {
42 id = id_;
43 }
44
45 bool operator==(const EntityId& other) const
46 {
47 return id == other.id;
48 }
49
50 bool operator!=(const EntityId& other) const
51 {
52 return !(*this == other);
53 }
54
55 bool operator<(const EntityId& other) const
56 {
57 return id < other.id;
58 }
59
61 {
62 return id;
63 }
64
65 const Value& value() const
66 {
67 return id;
68 }
69
70 char const* data() const
71 {
72 return id.data();
73 }
74
75 size_t size() const
76 {
77 return id.size();
78 }
79 };
80
81 template <typename FmtExtender>
82 inline void to_json(nlohmann::json& j, const EntityId<FmtExtender>& entity_id)
83 {
84 j = entity_id.value();
85 }
86
87 template <typename FmtExtender>
88 inline void from_json(
89 const nlohmann::json& j, EntityId<FmtExtender>& entity_id)
90 {
91 if (j.is_string())
92 {
93 entity_id = j.get<std::string>();
94 }
95 else
96 {
97 throw ccf::JsonParseError(fmt::format(
98 "{} should be hex-encoded string: {}",
99 FmtExtender::ID_LABEL,
100 j.dump()));
101 }
102 }
103
104 template <typename FmtExtender>
105 inline std::string schema_name(const EntityId<FmtExtender>*)
106 {
107 return FmtExtender::ID_LABEL;
108 }
109
110 template <typename FmtExtender>
111 inline void fill_json_schema(
112 nlohmann::json& schema, const EntityId<FmtExtender>*)
113 {
114 schema["type"] = "string";
115
116 // According to the spec, "format is an open value, so you can use any
117 // formats, even not those defined by the OpenAPI Specification"
118 // https://swagger.io/docs/specification/data-models/data-types/#format
119 schema["format"] = "hex";
120 schema["pattern"] =
121 fmt::format("^[a-f0-9]{{{}}}$", EntityId<FmtExtender>::LENGTH);
122 }
123
125 {
126 static std::string format(const std::string& core)
127 {
128 return fmt::format("m[{}]", core);
129 }
130
131 static constexpr auto ID_LABEL = "MemberId";
132 };
134
136 {
137 static std::string format(const std::string& core)
138 {
139 return fmt::format("u[{}]", core);
140 }
141
142 static constexpr auto ID_LABEL = "UserId";
143 };
145
147 {
148 static std::string format(const std::string& core)
149 {
150 return fmt::format("n[{}]", core);
151 }
152
153 static constexpr auto ID_LABEL = "NodeId";
154 };
156}
157
158namespace std
159{
160 template <typename FmtExtender>
161 static inline std::ostream& operator<<(
162 std::ostream& os, const ccf::EntityId<FmtExtender>& entity_id)
163 {
164 if constexpr (std::is_same_v<FmtExtender, void>)
165 {
166 os << entity_id.value();
167 }
168 else
169 {
170 os << FmtExtender::format(entity_id.value());
171 }
172 return os;
173 }
174
175 template <typename FmtExtender>
176 struct hash<ccf::EntityId<FmtExtender>>
177 {
178 size_t operator()(const ccf::EntityId<FmtExtender>& entity_id) const
179 {
180 return std::hash<std::string>{}(entity_id.value());
181 }
182 };
183}
184
185FMT_BEGIN_NAMESPACE
186template <typename FmtExtender>
187struct formatter<ccf::EntityId<FmtExtender>>
188{
189 template <typename ParseContext>
190 constexpr auto parse(ParseContext& ctx)
191 {
192 return ctx.begin();
193 }
194
195 template <typename FormatContext>
196 auto format(const ccf::EntityId<FmtExtender>& v, FormatContext& ctx) const
197 {
198 std::stringstream ss;
199 ss << v;
200 return format_to(ctx.out(), "{}", ss.str());
201 }
202};
203FMT_END_NAMESPACE
204
205namespace ccf::kv::serialisers
206{
207 template <typename FmtExtender>
208 struct BlitSerialiser<ccf::EntityId<FmtExtender>>
209 {
211 const ccf::EntityId<FmtExtender>& entity_id)
212 {
213 const auto& data = entity_id.value();
214 return SerialisedEntry(data.begin(), data.end());
215 }
216
218 const SerialisedEntry& data)
219 {
220 return ccf::EntityId<FmtExtender>(std::string(data.begin(), data.end()));
221 }
222 };
223}
Definition json.h:26
Definition sha256_hash.h:80
ccf::ByteVector SerialisedEntry
Definition serialised_entry.h:8
Definition app_interface.h:14
void fill_json_schema(nlohmann::json &schema, const ClaimsDigest *)
Definition claims_digest.h:64
std::string schema_name(const ClaimsDigest *)
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
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
void operator=(const Value &id_)
Definition entity_id.h:40
char const * data() const
Definition entity_id.h:70
size_t size() const
Definition entity_id.h:75
bool operator<(const EntityId &other) const
Definition entity_id.h:55
EntityId(Value &&id_)
Definition entity_id.h:28
const Value & value() const
Definition entity_id.h:65
EntityId()=default
std::string Value
Definition entity_id.h:19
void operator=(const EntityId &other)
Definition entity_id.h:35
Value & value()
Definition entity_id.h:60
bool operator!=(const EntityId &other) const
Definition entity_id.h:50
bool operator==(const EntityId &other) const
Definition entity_id.h:45
Definition entity_id.h:125
static constexpr auto ID_LABEL
Definition entity_id.h:131
static std::string format(const std::string &core)
Definition entity_id.h:126
Definition entity_id.h:147
static std::string format(const std::string &core)
Definition entity_id.h:148
static constexpr auto ID_LABEL
Definition entity_id.h:153
Definition entity_id.h:136
static constexpr auto ID_LABEL
Definition entity_id.h:142
static std::string format(const std::string &core)
Definition entity_id.h:137
static SerialisedEntry to_serialised(const ccf::EntityId< FmtExtender > &entity_id)
Definition entity_id.h:210
static ccf::EntityId< FmtExtender > from_serialised(const SerialisedEntry &data)
Definition entity_id.h:217
Definition blit_serialiser.h:14
auto format(const ccf::EntityId< FmtExtender > &v, FormatContext &ctx) const
Definition entity_id.h:196
constexpr auto parse(ParseContext &ctx)
Definition entity_id.h:190
size_t operator()(const ccf::EntityId< FmtExtender > &entity_id) const
Definition entity_id.h:178