Skip to content

Commit f486ffa

Browse files
committed
Extract duplicated code in SyncUserProfile
1 parent 0779013 commit f486ffa

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

src/realm/object-store/sync/sync_user.hpp

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -75,82 +75,55 @@ struct SyncUserProfile {
7575
// The full name of the user.
7676
util::Optional<std::string> name() const
7777
{
78-
if (m_data.find("name") == m_data.end()) {
79-
return util::none;
80-
}
81-
return static_cast<std::string>(m_data.at("name"));
78+
return get_field("name");
8279
}
8380
// The email address of the user.
8481
util::Optional<std::string> email() const
8582
{
86-
if (m_data.find("email") == m_data.end()) {
87-
return util::none;
88-
}
89-
return static_cast<std::string>(m_data.at("email"));
83+
return get_field("email");
9084
}
9185
// A URL to the user's profile picture.
9286
util::Optional<std::string> picture_url() const
9387
{
94-
if (m_data.find("picture_url") == m_data.end()) {
95-
return util::none;
96-
}
97-
return static_cast<std::string>(m_data.at("picture_url"));
88+
return get_field("picture_url");
9889
}
9990
// The first name of the user.
10091
util::Optional<std::string> first_name() const
10192
{
102-
if (m_data.find("first_name") == m_data.end()) {
103-
return util::none;
104-
}
105-
return static_cast<std::string>(m_data.at("first_name"));
93+
return get_field("first_name");
10694
}
10795
// The last name of the user.
10896
util::Optional<std::string> last_name() const
10997
{
110-
if (m_data.find("last_name") == m_data.end()) {
111-
return util::none;
112-
}
113-
return static_cast<std::string>(m_data.at("last_name"));
98+
return get_field("last_name");
11499
}
115100
// The gender of the user.
116101
util::Optional<std::string> gender() const
117102
{
118-
if (m_data.find("gender") == m_data.end()) {
119-
return util::none;
120-
}
121-
return static_cast<std::string>(m_data.at("gender"));
103+
return get_field("gender");
122104
}
123105
// The birthdate of the user.
124106
util::Optional<std::string> birthday() const
125107
{
126-
if (m_data.find("birthday") == m_data.end()) {
127-
return util::none;
128-
}
129-
return static_cast<std::string>(m_data.at("birthday"));
108+
return get_field("birthday");
130109
}
131110
// The minimum age of the user.
132111
util::Optional<std::string> min_age() const
133112
{
134-
if (m_data.find("min_age") == m_data.end()) {
135-
return util::none;
136-
}
137-
return static_cast<std::string>(m_data.at("min_age"));
113+
return get_field("min_age");
138114
}
139115
// The maximum age of the user.
140116
util::Optional<std::string> max_age() const
141117
{
142-
if (m_data.find("max_age") == m_data.end()) {
143-
return util::none;
144-
}
145-
return static_cast<std::string>(m_data.at("max_age"));
118+
return get_field("max_age");
146119
}
147120

148121
bson::Bson operator[](const std::string& key) const
149122
{
150123
return m_data.at(key);
151124
}
152125

153-
bson::BsonDocument data() const
126+
const bson::BsonDocument& data() const
154127
{
155128
return m_data;
156129
}
@@ -163,6 +136,15 @@ struct SyncUserProfile {
163136

164137
private:
165138
bson::BsonDocument m_data;
139+
140+
util::Optional<std::string> get_field(const char* name) const
141+
{
142+
auto it = m_data.find(name);
143+
if (it == m_data.end()) {
144+
return util::none;
145+
}
146+
return static_cast<std::string>((*it).second);
147+
}
166148
};
167149

168150
// A struct that represents an identity that a `User` is linked to

0 commit comments

Comments
 (0)