Skip to content

Commit 5efbb08

Browse files
authored
Add a ToString function to create human-readable debug descriptions (#2384)
The motivation is to avoid writing lots of boilerplate for containers.
1 parent c09966e commit 5efbb08

File tree

18 files changed

+485
-87
lines changed

18 files changed

+485
-87
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@
201201
B67BF449216EB43000CA9097 /* create_noop_connectivity_monitor.cc in Sources */ = {isa = PBXBuildFile; fileRef = B67BF448216EB43000CA9097 /* create_noop_connectivity_monitor.cc */; };
202202
B686F2AF2023DDEE0028D6BE /* field_path_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B686F2AD2023DDB20028D6BE /* field_path_test.cc */; };
203203
B686F2B22025000D0028D6BE /* resource_path_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B686F2B02024FFD70028D6BE /* resource_path_test.cc */; };
204+
B68B1E012213A765008977EF /* to_string_apple_test.mm in Sources */ = {isa = PBXBuildFile; fileRef = B68B1E002213A764008977EF /* to_string_apple_test.mm */; };
204205
B68FC0E521F6848700A7055C /* watch_change_test.mm in Sources */ = {isa = PBXBuildFile; fileRef = B68FC0E421F6848700A7055C /* watch_change_test.mm */; };
206+
B696858E2214B53900271095 /* to_string_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B696858D2214B53900271095 /* to_string_test.cc */; };
205207
B6BBE43121262CF400C6A53E /* grpc_stream_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6BBE42F21262CF400C6A53E /* grpc_stream_test.cc */; };
206208
B6D1B68520E2AB1B00B35856 /* exponential_backoff_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6D1B68420E2AB1A00B35856 /* exponential_backoff_test.cc */; };
207209
B6D9649121544D4F00EB9CFB /* grpc_connection_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = B6D9649021544D4F00EB9CFB /* grpc_connection_test.cc */; };
@@ -522,7 +524,9 @@
522524
B67BF448216EB43000CA9097 /* create_noop_connectivity_monitor.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = create_noop_connectivity_monitor.cc; sourceTree = "<group>"; };
523525
B686F2AD2023DDB20028D6BE /* field_path_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = field_path_test.cc; sourceTree = "<group>"; };
524526
B686F2B02024FFD70028D6BE /* resource_path_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = resource_path_test.cc; sourceTree = "<group>"; };
527+
B68B1E002213A764008977EF /* to_string_apple_test.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = to_string_apple_test.mm; sourceTree = "<group>"; };
525528
B68FC0E421F6848700A7055C /* watch_change_test.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = watch_change_test.mm; sourceTree = "<group>"; };
529+
B696858D2214B53900271095 /* to_string_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = to_string_test.cc; sourceTree = "<group>"; };
526530
B69CF05A219B9105004C434D /* FIRFirestore+Testing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FIRFirestore+Testing.h"; sourceTree = "<group>"; };
527531
B6BBE42F21262CF400C6A53E /* grpc_stream_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = grpc_stream_test.cc; sourceTree = "<group>"; };
528532
B6D1B68420E2AB1A00B35856 /* exponential_backoff_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = exponential_backoff_test.cc; sourceTree = "<group>"; };
@@ -701,7 +705,6 @@
701705
546854A720A3681B004BDBD5 /* remote */ = {
702706
isa = PBXGroup;
703707
children = (
704-
B68FC0E421F6848700A7055C /* watch_change_test.mm */,
705708
546854A820A36867004BDBD5 /* datastore_test.mm */,
706709
B6D1B68420E2AB1A00B35856 /* exponential_backoff_test.cc */,
707710
B6D9649021544D4F00EB9CFB /* grpc_connection_test.cc */,
@@ -710,6 +713,7 @@
710713
B6D964942163E63900EB9CFB /* grpc_unary_call_test.cc */,
711714
61F72C5520BC48FD001A68CB /* serializer_test.cc */,
712715
B66D8995213609EE0086DA0C /* stream_test.mm */,
716+
B68FC0E421F6848700A7055C /* watch_change_test.mm */,
713717
);
714718
path = remote;
715719
sourceTree = "<group>";
@@ -752,6 +756,8 @@
752756
54131E9620ADE678001DF3FF /* string_format_test.cc */,
753757
AB380CFC201A2EE200D97691 /* string_util_test.cc */,
754758
79507DF8378D3C42F5B36268 /* string_win_test.cc */,
759+
B68B1E002213A764008977EF /* to_string_apple_test.mm */,
760+
B696858D2214B53900271095 /* to_string_test.cc */,
755761
2A0CF41BA5AED6049B0BEB2C /* type_traits_apple_test.mm */,
756762
);
757763
path = util;
@@ -1678,8 +1684,6 @@
16781684
buildActionMask = 2147483647;
16791685
files = (
16801686
);
1681-
inputFileListPaths = (
1682-
);
16831687
inputPaths = (
16841688
"${SRCROOT}/Pods/Target Support Files/Pods-macOS_example/Pods-macOS_example-frameworks.sh",
16851689
"${BUILT_PRODUCTS_DIR}/BoringSSL-GRPC-macOS/openssl_grpc.framework",
@@ -1692,8 +1696,6 @@
16921696
"${BUILT_PRODUCTS_DIR}/nanopb-macOS/nanopb.framework",
16931697
);
16941698
name = "[CP] Embed Pods Frameworks";
1695-
outputFileListPaths = (
1696-
);
16971699
outputPaths = (
16981700
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/openssl_grpc.framework",
16991701
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GTMSessionFetcher.framework",
@@ -1769,15 +1771,11 @@
17691771
buildActionMask = 2147483647;
17701772
files = (
17711773
);
1772-
inputFileListPaths = (
1773-
);
17741774
inputPaths = (
17751775
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
17761776
"${PODS_ROOT}/Manifest.lock",
17771777
);
17781778
name = "[CP] Check Pods Manifest.lock";
1779-
outputFileListPaths = (
1780-
);
17811779
outputPaths = (
17821780
"$(DERIVED_FILE_DIR)/Pods-macOS_example-checkManifestLockResult.txt",
17831781
);
@@ -1981,7 +1979,6 @@
19811979
5492E0BA2021555100B64F25 /* FSTDocumentSetTests.mm in Sources */,
19821980
5492E0BD2021555100B64F25 /* FSTDocumentTests.mm in Sources */,
19831981
5492E03E2021401F00B64F25 /* FSTEventAccumulator.mm in Sources */,
1984-
B68FC0E521F6848700A7055C /* watch_change_test.mm in Sources */,
19851982
5492E067202154B900B64F25 /* FSTEventManagerTests.mm in Sources */,
19861983
5492E0BF2021555100B64F25 /* FSTFieldValueTests.mm in Sources */,
19871984
54764FAF1FAA21B90085E60A /* FSTGoogleTestTests.mm in Sources */,
@@ -2099,11 +2096,14 @@
20992096
AB380CFB2019388600D97691 /* target_id_generator_test.cc in Sources */,
21002097
54A0352A20A3B3BD003E0143 /* testutil.cc in Sources */,
21012098
ABF6506C201131F8005F2C74 /* timestamp_test.cc in Sources */,
2099+
B68B1E012213A765008977EF /* to_string_apple_test.mm in Sources */,
2100+
B696858E2214B53900271095 /* to_string_test.cc in Sources */,
21022101
ABC1D7E12023A40C00BA84F0 /* token_test.cc in Sources */,
21032102
54A0352720A3AED0003E0143 /* transform_operations_test.mm in Sources */,
21042103
549CCA5120A36DBC00BCEB75 /* tree_sorted_map_test.cc in Sources */,
21052104
C80B10E79CDD7EF7843C321E /* type_traits_apple_test.mm in Sources */,
21062105
ABC1D7DE2023A05300BA84F0 /* user_test.cc in Sources */,
2106+
B68FC0E521F6848700A7055C /* watch_change_test.mm in Sources */,
21072107
544129DE21C2DDC800EFB9CC /* write.pb.cc in Sources */,
21082108
);
21092109
runOnlyForDeploymentPostprocessing = 0;

Firestore/Example/Tests/Model/FSTFieldValueTests.mm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@
5454
} else if ([value isKindOfClass:[FSTDocumentKeyReference class]]) {
5555
// We directly convert these here so that the databaseIDs can be different.
5656
FSTDocumentKeyReference *reference = (FSTDocumentKeyReference *)value;
57-
wrappedValue = [FSTReferenceValue referenceValue:reference.key
58-
databaseID:reference.databaseID];
57+
wrappedValue =
58+
[FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:reference.key]
59+
databaseID:reference.databaseID];
5960
} else {
6061
wrappedValue = FSTTestFieldValue(value);
6162
}
@@ -258,7 +259,7 @@ - (void)testWrapResourceNames {
258259
for (FSTDocumentKeyReference *value in values) {
259260
FSTFieldValue *wrapped = FSTTestFieldValue(value);
260261
XCTAssertEqualObjects([wrapped class], [FSTReferenceValue class]);
261-
XCTAssertEqualObjects([wrapped value], value.key);
262+
XCTAssertEqualObjects([wrapped value], [FSTDocumentKey keyWithDocumentKey:value.key]);
262263
XCTAssertTrue(*((FSTReferenceValue *)wrapped).databaseID ==
263264
*(const DatabaseId *)(value.databaseID));
264265
}
@@ -459,7 +460,9 @@ - (void)testValueEquality {
459460
],
460461
@[ FSTTestFieldValue(FSTTestGeoPoint(1, 0)) ],
461462
@[
462-
[FSTReferenceValue referenceValue:FSTTestDocKey(@"coll/doc1") databaseID:&database_id],
463+
[FSTReferenceValue
464+
referenceValue:[FSTDocumentKey keyWithDocumentKey:FSTTestDocKey(@"coll/doc1")]
465+
databaseID:&database_id],
463466
FSTTestFieldValue(FSTTestRef("project", DatabaseId::kDefault, @"coll/doc1"))
464467
],
465468
@[ FSTTestRef("project", "(default)", @"coll/doc2") ],

Firestore/Source/API/FIRQuery.mm

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,13 @@ - (FIRQuery *)queryWithFilterOperator:(FSTRelationFilterOperator)filterOperator
471471
"a valid document ID, but it was an empty string.");
472472
}
473473
ResourcePath path = self.query.path.Append([documentKey UTF8String]);
474-
fieldValue = [FSTReferenceValue referenceValue:DocumentKey{path}
475-
databaseID:self.firestore.databaseID];
474+
fieldValue =
475+
[FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:DocumentKey{path}]
476+
databaseID:self.firestore.databaseID];
476477
} else if ([value isKindOfClass:[FIRDocumentReference class]]) {
477478
FIRDocumentReference *ref = (FIRDocumentReference *)value;
478-
fieldValue = [FSTReferenceValue referenceValue:ref.key databaseID:self.firestore.databaseID];
479+
fieldValue = [FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:ref.key]
480+
databaseID:self.firestore.databaseID];
479481
} else {
480482
FSTThrowInvalidArgument(@"Invalid query. When querying by document ID you must provide a "
481483
"valid string or DocumentReference, but it was of type: %@",
@@ -571,8 +573,9 @@ - (FSTBound *)boundFromSnapshot:(FIRDocumentSnapshot *)snapshot isBefore:(BOOL)i
571573
// orders), multiple documents could match the position, yielding duplicate results.
572574
for (FSTSortOrder *sortOrder in self.query.sortOrders) {
573575
if (sortOrder.field == FieldPath::KeyFieldPath()) {
574-
[components addObject:[FSTReferenceValue referenceValue:document.key
575-
databaseID:self.firestore.databaseID]];
576+
[components addObject:[FSTReferenceValue
577+
referenceValue:[FSTDocumentKey keyWithDocumentKey:document.key]
578+
databaseID:self.firestore.databaseID]];
576579
} else {
577580
FSTFieldValue *value = [document fieldForPath:sortOrder.field];
578581

@@ -621,8 +624,9 @@ - (FSTBound *)boundFromFieldValues:(NSArray<id> *)fieldValues isBefore:(BOOL)isB
621624
@"Invalid query. Document ID '%@' contains a slash.", documentID);
622625
}
623626
const DocumentKey key{self.query.path.Append([documentID UTF8String])};
624-
[components addObject:[FSTReferenceValue referenceValue:key
625-
databaseID:self.firestore.databaseID]];
627+
[components
628+
addObject:[FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:key]
629+
databaseID:self.firestore.databaseID]];
626630
} else {
627631
FSTFieldValue *fieldValue = [self.firestore.dataConverter parsedQueryValue:rawValue];
628632
[components addObject:fieldValue];

Firestore/Source/API/FSTUserDataConverter.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ - (nullable FSTFieldValue *)parseScalarValue:(nullable id)input context:(ParseCo
455455
self.databaseID->project_id().c_str(), self.databaseID->database_id().c_str(),
456456
context.FieldDescription().c_str());
457457
}
458-
return [FSTReferenceValue referenceValue:reference.key databaseID:self.databaseID];
458+
return [FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:reference.key]
459+
databaseID:self.databaseID];
459460

460461
} else {
461462
FSTThrowInvalidArgument(@"Unsupported type: %@%s", NSStringFromClass([input class]),

Firestore/Source/Core/FSTView.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ - (BOOL)isEqual:(id)other {
118118

119119
- (NSUInteger)hash {
120120
NSUInteger hash = self.type;
121-
hash = hash * 31u + [self.key hash];
121+
hash = hash * 31u + self.key.Hash();
122122
return hash;
123123
}
124124

@@ -241,8 +241,8 @@ - (FSTViewDocumentChanges *)computeChangesWithDocuments:(const MaybeDocumentMap
241241
newDoc = (FSTDocument *)maybeNewDoc;
242242
}
243243
if (newDoc) {
244-
HARD_ASSERT(key == newDoc.key, "Mismatching key in document changes: %s != %s", key,
245-
newDoc.key.ToString());
244+
HARD_ASSERT(key == newDoc.key, "Mismatching key in document changes: %s != %s",
245+
key.ToString(), newDoc.key.ToString());
246246
if (![self.query matchesDocument:newDoc]) {
247247
newDoc = nil;
248248
}

Firestore/Source/Core/FSTViewSnapshot.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ - (NSString *)description {
9797
std::string result = absl::StrJoin(
9898
_changeMap, ",",
9999
[](std::string *out, const std::pair<DocumentKey, FSTDocumentViewChange *> &kv) {
100-
out->append(StringFormat("%s: %s", kv.first, kv.second));
100+
out->append(StringFormat("%s: %s", kv.first.ToString(), kv.second));
101101
});
102102
return WrapNSString(std::string{"{"} + result + "}");
103103
}

Firestore/Source/Model/FSTDocument.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ - (BOOL)isEqual:(id)other {
153153
}
154154

155155
- (NSUInteger)hash {
156-
NSUInteger result = [self.key hash];
156+
NSUInteger result = self.key.Hash();
157157
result = result * 31 + self.version.Hash();
158158
result = result * 31 + [self.data hash];
159159
result = result * 31 + _documentState;
@@ -212,7 +212,7 @@ - (BOOL)isEqual:(id)other {
212212
}
213213

214214
- (NSUInteger)hash {
215-
NSUInteger result = [self.key hash];
215+
NSUInteger result = self.key.Hash();
216216
result = result * 31 + self.version.Hash();
217217
result = result * 31 + (_hasCommittedMutations ? 1 : 0);
218218
return result;
@@ -250,7 +250,7 @@ - (BOOL)isEqual:(id)other {
250250
}
251251

252252
- (NSUInteger)hash {
253-
NSUInteger result = [self.key hash];
253+
NSUInteger result = self.key.Hash();
254254
result = result * 31 + self.version.Hash();
255255
return result;
256256
}

Firestore/Source/Remote/FSTSerializerBeta.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ - (FSTReferenceValue *)decodedReferenceValue:(NSString *)resourceName {
355355
HARD_ASSERT(database_id == *self.databaseID, "Database %s:%s cannot encode reference from %s:%s",
356356
self.databaseID->project_id(), self.databaseID->database_id(),
357357
database_id.project_id(), database_id.database_id());
358-
return [FSTReferenceValue referenceValue:key databaseID:self.databaseID];
358+
return [FSTReferenceValue referenceValue:[FSTDocumentKey keyWithDocumentKey:key]
359+
databaseID:self.databaseID];
359360
}
360361

361362
- (GCFSArrayValue *)encodedArrayValue:(FSTArrayValue *)arrayValue {

Firestore/core/src/firebase/firestore/immutable/sorted_map.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace immutable {
3838
template <typename K, typename V, typename C = util::Comparator<K>>
3939
class SortedMap : public impl::SortedMapBase {
4040
public:
41+
using key_type = K;
42+
using mapped_type = V;
4143
/** The type of the entries stored in the map. */
4244
using value_type = std::pair<K, V>;
4345
using array_type = impl::ArraySortedMap<K, V, C>;

Firestore/core/src/firebase/firestore/local/leveldb_mutation_queue.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "Firestore/core/src/firebase/firestore/model/mutation_batch.h"
3131
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
3232
#include "Firestore/core/src/firebase/firestore/util/string_util.h"
33+
#include "Firestore/core/src/firebase/firestore/util/to_string.h"
3334
#include "absl/strings/match.h"
3435

3536
NS_ASSUME_NONNULL_BEGIN

Firestore/core/src/firebase/firestore/model/document_key.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ class DocumentKey {
5151
explicit DocumentKey(ResourcePath&& path);
5252

5353
#if defined(__OBJC__)
54-
operator FSTDocumentKey*() const {
55-
return [FSTDocumentKey keyWithDocumentKey:*this];
56-
}
57-
5854
NSUInteger Hash() const {
5955
return util::Hash(ToString());
6056
}

Firestore/core/src/firebase/firestore/util/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ cc_library(
252252
range.h
253253
string_util.cc
254254
string_util.h
255+
to_string.h
255256
type_traits.h
256257
DEPENDS
257258
absl_base

Firestore/core/src/firebase/firestore/util/string_format.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct FormatChoice<5> {};
5353
*
5454
* Chooses a conversion to a text form in this order:
5555
* * If the value is exactly of `bool` type (without implicit conversions)
56-
* the text will the "true" or "false".
56+
* the text will be "true" or "false".
5757
* * If the value is of type `const char*`, the text will be the value
5858
* interpreted as a C string. To show the address of a single char or to
5959
* show the `const char*` as an address, cast to `void*`.

Firestore/core/src/firebase/firestore/util/string_util.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,6 @@ std::string PrefixSuccessor(absl::string_view prefix);
6565
*/
6666
std::string ImmediateSuccessor(absl::string_view s);
6767

68-
/**
69-
* Returns a string description of the contents of the given collection.
70-
*/
71-
template <typename Container>
72-
std::string ToString(const Container& container) {
73-
std::string result;
74-
result.append("[");
75-
const char* sep = "";
76-
for (auto&& item : container) {
77-
result.append(sep);
78-
result.append(item);
79-
sep = ", ";
80-
}
81-
result.append("]");
82-
return result;
83-
}
84-
8568
} // namespace util
8669
} // namespace firestore
8770
} // namespace firebase

0 commit comments

Comments
 (0)