23
23
#include < utility>
24
24
#include < vector>
25
25
26
+ #include " Firestore/core/src/firebase/firestore/immutable/sorted_map.h"
26
27
#include " Firestore/core/src/firebase/firestore/util/comparison.h"
27
28
#include " Firestore/core/src/firebase/firestore/util/hard_assert.h"
28
29
#include " absl/memory/memory.h"
@@ -91,8 +92,8 @@ FieldValue& FieldValue::operator=(const FieldValue& value) {
91
92
}
92
93
case Type::Object: {
93
94
// copy-and-swap
94
- ObjectValue:: Map tmp = value.object_value_ -> internal_value ;
95
- std::swap (object_value_-> internal_value , tmp);
95
+ Map tmp = * value.object_value_ ;
96
+ std::swap (* object_value_, tmp);
96
97
break ;
97
98
}
98
99
default :
@@ -143,45 +144,41 @@ bool FieldValue::Comparable(Type lhs, Type rhs) {
143
144
}
144
145
}
145
146
146
- FieldValue FieldValue::Set (const FieldPath& field_path,
147
- const FieldValue& value) const {
148
- HARD_ASSERT (type () == Type::Object,
149
- " Cannot set field for non-object FieldValue" );
147
+ // TODO(rsgowman): Reorder this file to match its header.
148
+ ObjectValue ObjectValue::Set (const FieldPath& field_path,
149
+ const FieldValue& value) const {
150
150
HARD_ASSERT (!field_path.empty (),
151
151
" Cannot set field for empty path on FieldValue" );
152
152
// Set the value by recursively calling on child object.
153
153
const std::string& child_name = field_path.first_segment ();
154
- const ObjectValue::Map& object_map = object_value_->internal_value ;
155
154
if (field_path.size () == 1 ) {
156
155
return SetChild (child_name, value);
157
156
} else {
158
- FieldValue child;
159
- const auto iter = object_map.find (child_name);
160
- if (iter != object_map.end () && iter->second .type () == Type::Object) {
161
- child = iter->second ;
162
- } else {
163
- child = EmptyObject ();
157
+ ObjectValue child = ObjectValue::Empty ();
158
+ const auto iter = fv_.object_value_ ->find (child_name);
159
+ if (iter != fv_.object_value_ ->end () &&
160
+ iter->second .type () == Type::Object) {
161
+ child = ObjectValue (iter->second );
164
162
}
165
- FieldValue new_child = child.Set (field_path.PopFirst (), value);
166
- return SetChild (child_name, new_child);
163
+ ObjectValue new_child = child.Set (field_path.PopFirst (), value);
164
+ return SetChild (child_name, new_child. fv_ );
167
165
}
168
166
}
169
167
170
- FieldValue FieldValue::Delete (const FieldPath& field_path) const {
171
- HARD_ASSERT (type () == Type::Object,
172
- " Cannot delete field for non-object FieldValue" );
168
+ ObjectValue ObjectValue::Delete (const FieldPath& field_path) const {
173
169
HARD_ASSERT (!field_path.empty (),
174
170
" Cannot delete field for empty path on FieldValue" );
175
171
// Delete the value by recursively calling on child object.
176
172
const std::string& child_name = field_path.first_segment ();
177
- const ObjectValue::Map& object_map = object_value_->internal_value ;
178
173
if (field_path.size () == 1 ) {
179
- return FieldValue ::FromMap (object_map. erase (child_name));
174
+ return ObjectValue ::FromMap (fv_. object_value_ -> erase (child_name));
180
175
} else {
181
- const auto iter = object_map.find (child_name);
182
- if (iter != object_map.end () && iter->second .type () == Type::Object) {
183
- FieldValue new_child = iter->second .Delete (field_path.PopFirst ());
184
- return SetChild (child_name, new_child);
176
+ const auto iter = fv_.object_value_ ->find (child_name);
177
+ if (iter != fv_.object_value_ ->end () &&
178
+ iter->second .type () == Type::Object) {
179
+ ObjectValue new_child =
180
+ ObjectValue (iter->second ).Delete (field_path.PopFirst ());
181
+ return SetChild (child_name, new_child.fv_ );
185
182
} else {
186
183
// If the found value isn't an object, it cannot contain the remaining
187
184
// segments of the path. We don't actually change a primitive value to
@@ -191,17 +188,14 @@ FieldValue FieldValue::Delete(const FieldPath& field_path) const {
191
188
}
192
189
}
193
190
194
- absl::optional<FieldValue> FieldValue::Get (const FieldPath& field_path) const {
195
- HARD_ASSERT (type () == Type::Object,
196
- " Cannot get field for non-object FieldValue" );
197
- const FieldValue* current = this ;
191
+ absl::optional<FieldValue> ObjectValue::Get (const FieldPath& field_path) const {
192
+ const FieldValue* current = &this ->fv_ ;
198
193
for (const auto & path : field_path) {
199
194
if (current->type () != Type::Object) {
200
195
return absl::nullopt;
201
196
}
202
- const ObjectValue::Map& object_map = current->object_value_ ->internal_value ;
203
- const auto iter = object_map.find (path);
204
- if (iter == object_map.end ()) {
197
+ const auto iter = current->object_value_ ->find (path);
198
+ if (iter == current->object_value_ ->end ()) {
205
199
return absl::nullopt;
206
200
} else {
207
201
current = &iter->second ;
@@ -210,12 +204,9 @@ absl::optional<FieldValue> FieldValue::Get(const FieldPath& field_path) const {
210
204
return *current;
211
205
}
212
206
213
- FieldValue FieldValue::SetChild (const std::string& child_name,
214
- const FieldValue& value) const {
215
- HARD_ASSERT (type () == Type::Object,
216
- " Cannot set child for non-object FieldValue" );
217
- return FieldValue::FromMap (
218
- object_value_->internal_value .insert (child_name, value));
207
+ ObjectValue ObjectValue::SetChild (const std::string& child_name,
208
+ const FieldValue& value) const {
209
+ return ObjectValue::FromMap (fv_.object_value_ ->insert (child_name, value));
219
210
}
220
211
221
212
FieldValue FieldValue::Null () {
@@ -239,7 +230,7 @@ FieldValue FieldValue::Nan() {
239
230
}
240
231
241
232
FieldValue FieldValue::EmptyObject () {
242
- return FieldValue::FromMap (ObjectValue::Empty ());
233
+ return FieldValue::FromMap (FieldValue::Map ());
243
234
}
244
235
245
236
FieldValue FieldValue::FromInteger (int64_t value) {
@@ -344,19 +335,19 @@ FieldValue FieldValue::FromArray(std::vector<FieldValue>&& value) {
344
335
return result;
345
336
}
346
337
347
- FieldValue FieldValue::FromMap (const ObjectValue ::Map& value) {
348
- ObjectValue ::Map copy (value);
338
+ FieldValue FieldValue::FromMap (const FieldValue ::Map& value) {
339
+ FieldValue ::Map copy (value);
349
340
return FromMap (std::move (copy));
350
341
}
351
342
352
- FieldValue FieldValue::FromMap (ObjectValue ::Map&& value) {
343
+ FieldValue FieldValue::FromMap (FieldValue ::Map&& value) {
353
344
FieldValue result;
354
345
result.SwitchTo (Type::Object);
355
- std::swap (result.object_value_ -> internal_value , value);
346
+ std::swap (* result.object_value_ , value);
356
347
return result;
357
348
}
358
349
359
- bool operator <(const ObjectValue ::Map& lhs, const ObjectValue ::Map& rhs) {
350
+ bool operator <(const FieldValue ::Map& lhs, const FieldValue ::Map& rhs) {
360
351
return std::lexicographical_compare (lhs.begin (), lhs.end (), rhs.begin (),
361
352
rhs.end ());
362
353
}
@@ -454,7 +445,7 @@ void FieldValue::SwitchTo(const Type type) {
454
445
array_value_.~unique_ptr<std::vector<FieldValue>>();
455
446
break ;
456
447
case Type::Object:
457
- object_value_.~unique_ptr<ObjectValue >();
448
+ object_value_.~unique_ptr<Map >();
458
449
break ;
459
450
default : {} // The other types where there is nothing to worry about.
460
451
}
@@ -491,13 +482,20 @@ void FieldValue::SwitchTo(const Type type) {
491
482
absl::make_unique<std::vector<FieldValue>>());
492
483
break ;
493
484
case Type::Object:
494
- new (&object_value_)
495
- std::unique_ptr<ObjectValue>(absl::make_unique<ObjectValue>());
485
+ new (&object_value_) std::unique_ptr<Map>(absl::make_unique<Map>());
496
486
break ;
497
487
default : {} // The other types where there is nothing to worry about.
498
488
}
499
489
}
500
490
491
+ ObjectValue ObjectValue::FromMap (const FieldValue::Map& value) {
492
+ return ObjectValue (FieldValue::FromMap (value));
493
+ }
494
+
495
+ ObjectValue ObjectValue::FromMap (FieldValue::Map&& value) {
496
+ return ObjectValue (FieldValue::FromMap (std::move (value)));
497
+ }
498
+
501
499
} // namespace model
502
500
} // namespace firestore
503
501
} // namespace firebase
0 commit comments