Skip to content

Commit 453a5cb

Browse files
authored
Replace absl::optional usage in API (#8902)
1 parent 6a723ec commit 453a5cb

15 files changed

+464
-36
lines changed

Firestore/Source/API/FIRCollectionReference.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "Firestore/core/src/api/collection_reference.h"
2727
#include "Firestore/core/src/api/document_reference.h"
28+
#include "Firestore/core/src/api/optional.h"
2829
#include "Firestore/core/src/core/user_data.h"
2930
#include "Firestore/core/src/model/resource_path.h"
3031
#include "Firestore/core/src/util/error_apple.h"
@@ -33,6 +34,7 @@
3334

3435
using firebase::firestore::api::CollectionReference;
3536
using firebase::firestore::api::DocumentReference;
37+
using firebase::firestore::api::Optional;
3638
using firebase::firestore::core::ParsedSetData;
3739
using firebase::firestore::model::ResourcePath;
3840
using firebase::firestore::util::MakeCallback;
@@ -91,7 +93,7 @@ - (NSString *)collectionID {
9193
}
9294

9395
- (FIRDocumentReference *_Nullable)parent {
94-
absl::optional<DocumentReference> parent = self.reference.parent();
96+
Optional<DocumentReference> parent = self.reference.parent();
9597
if (!parent) {
9698
return nil;
9799
}

Firestore/Source/API/FIRDocumentSnapshot+Internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <memory>
2020

2121
#include "Firestore/core/src/api/api_fwd.h"
22+
#include "Firestore/core/src/api/optional.h"
2223
#include "Firestore/core/src/model/model_fwd.h"
2324

2425
@class FIRFirestore;
@@ -48,7 +49,7 @@ NS_ASSUME_NONNULL_BEGIN
4849
/** Internal FIRDocumentSnapshot API we don't want exposed in our public header files. */
4950
@interface FIRDocumentSnapshot (Internal)
5051

51-
- (const absl::optional<model::Document> &)internalDocument;
52+
- (const api::Optional<model::Document> &)internalDocument;
5253

5354
@end
5455

Firestore/Source/API/FIRDocumentSnapshot.mm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "Firestore/core/src/api/document_reference.h"
3535
#include "Firestore/core/src/api/document_snapshot.h"
3636
#include "Firestore/core/src/api/firestore.h"
37+
#include "Firestore/core/src/api/optional.h"
3738
#include "Firestore/core/src/api/settings.h"
3839
#include "Firestore/core/src/model/database_id.h"
3940
#include "Firestore/core/src/model/document_key.h"
@@ -50,6 +51,7 @@
5051
using firebase::firestore::api::Firestore;
5152
using firebase::firestore::api::MakeFIRGeoPoint;
5253
using firebase::firestore::api::MakeFIRTimestamp;
54+
using firebase::firestore::api::Optional;
5355
using firebase::firestore::api::SnapshotMetadata;
5456
using firebase::firestore::model::DatabaseId;
5557
using firebase::firestore::model::Document;
@@ -126,7 +128,7 @@ - (BOOL)exists {
126128
return _snapshot.exists();
127129
}
128130

129-
- (const absl::optional<Document> &)internalDocument {
131+
- (const Optional<Document> &)internalDocument {
130132
return _snapshot.internal_document();
131133
}
132134

@@ -153,7 +155,7 @@ - (FIRSnapshotMetadata *)metadata {
153155

154156
- (nullable NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
155157
(FIRServerTimestampBehavior)serverTimestampBehavior {
156-
absl::optional<google_firestore_v1_Value> data = _snapshot.GetValue(FieldPath::EmptyPath());
158+
Optional<google_firestore_v1_Value> data = _snapshot.GetValue(FieldPath::EmptyPath());
157159
if (!data) return nil;
158160

159161
FSTUserDataWriter *dataWriter =
@@ -176,7 +178,7 @@ - (nullable id)valueForField:(id)field
176178
} else {
177179
ThrowInvalidArgument("Subscript key must be an NSString or FIRFieldPath.");
178180
}
179-
absl::optional<google_firestore_v1_Value> fieldValue = _snapshot.GetValue(fieldPath);
181+
Optional<google_firestore_v1_Value> fieldValue = _snapshot.GetValue(fieldPath);
180182
if (!fieldValue) return nil;
181183
FSTUserDataWriter *dataWriter =
182184
[[FSTUserDataWriter alloc] initWithFirestore:_snapshot.firestore()

Firestore/core/src/api/collection_reference.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ const std::string& CollectionReference::collection_id() const {
6767
return query().path().last_segment();
6868
}
6969

70-
absl::optional<DocumentReference> CollectionReference::parent() const {
70+
Optional<DocumentReference> CollectionReference::parent() const {
7171
ResourcePath parent_path = query().path().PopLast();
7272
if (parent_path.empty()) {
73-
return absl::nullopt;
73+
return {};
7474
} else {
75-
return DocumentReference(DocumentKey(std::move(parent_path)), firestore());
75+
return Optional<DocumentReference>(
76+
DocumentReference(DocumentKey(std::move(parent_path)), firestore()));
7677
}
7778
}
7879

Firestore/core/src/api/collection_reference.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
#include <string>
2222

2323
#include "Firestore/core/src/api/api_fwd.h"
24+
#include "Firestore/core/src/api/optional.h"
2425
#include "Firestore/core/src/api/query_core.h"
2526
#include "Firestore/core/src/core/core_fwd.h"
26-
#include "absl/types/optional.h"
2727

2828
namespace firebase {
2929
namespace firestore {
@@ -52,7 +52,7 @@ class CollectionReference : public Query {
5252
* For subcollections, `parent` returns the containing `DocumentReference`.
5353
* For root collections, nullopt is returned.
5454
*/
55-
absl::optional<DocumentReference> parent() const;
55+
Optional<DocumentReference> parent() const;
5656

5757
/**
5858
* A string containing the slash-separated path to this `CollectionReference`

Firestore/core/src/api/common.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_API_COMMON_H_
18+
#define FIRESTORE_CORE_SRC_API_COMMON_H_
19+
20+
// This file is copied from
21+
// https://github.com/firebase/firebase-cpp-sdk/blob/58dbf86c8b767b90f8427275388f6309617650fa/app/src/include/firebase/internal/common.h
22+
// in order to support optional.h
23+
24+
// Include a STL header file, othewise _STLPORT_VERSION won't be set.
25+
#include <utility>
26+
27+
// Move operators use rvalue references, which are a C++11 extension.
28+
// Also, Visual Studio 2010 and later actually support move operators despite
29+
// reporting __cplusplus to be 199711L, so explicitly check for that.
30+
// Also, stlport doesn't implement std::move().
31+
#if (__cplusplus >= 201103L || _MSC_VER >= 1600) && !defined(_STLPORT_VERSION)
32+
#define FIREBASE_USE_MOVE_OPERATORS
33+
#endif
34+
35+
// stlport doesn't implement std::function.
36+
#if !defined(_STLPORT_VERSION)
37+
#define FIREBASE_USE_STD_FUNCTION
38+
#endif // !defined(_STLPORT_VERSION)
39+
40+
// stlport doesn't implement std::aligned_storage.
41+
#if defined(_STLPORT_VERSION)
42+
#include <cstddef>
43+
44+
namespace firebase {
45+
template <std::size_t Length, std::size_t Alignment>
46+
struct AlignedStorage {
47+
struct type {
48+
alignas(Alignment) unsigned char data[Length];
49+
};
50+
};
51+
} // namespace firebase
52+
#define FIREBASE_ALIGNED_STORAGE ::firebase::AlignedStorage
53+
#else
54+
#include <type_traits>
55+
#define FIREBASE_ALIGNED_STORAGE std::aligned_storage
56+
#endif // defined(_STLPORT_VERSION)
57+
58+
// Visual Studio 2013 does not support snprintf, so use streams instead.
59+
#if !(defined(_MSC_VER) && _MSC_VER <= 1800)
60+
#define FIREBASE_USE_SNPRINTF
61+
#endif // !(defined(_MSC_VER) && _MSC_VER <= 1800)
62+
63+
#if !(defined(_MSC_VER) && _MSC_VER <= 1800)
64+
#define FIREBASE_USE_EXPLICIT_DEFAULT_METHODS
65+
#endif // !(defined(_MSC_VER) && _MSC_VER <= 1800)
66+
67+
#if !defined(DOXYGEN) && !defined(SWIG)
68+
#if !defined(_WIN32) && !defined(__CYGWIN__)
69+
// Prevent GCC & Clang from stripping a symbol.
70+
#define FIREBASE_APP_KEEP_SYMBOL __attribute__((used))
71+
#else
72+
// MSVC needs to reference a symbol directly in the application for it to be
73+
// kept in the final executable. In this case, the end user's application
74+
// must include the appropriate Firebase header (e.g firebase/analytics.h) to
75+
// initialize the module.
76+
#define FIREBASE_APP_KEEP_SYMBOL
77+
#endif // !defined(_WIN32) && !defined(__CYGWIN__)
78+
79+
// Module initializer's name.
80+
//
81+
// This can be used to explicitly include a module initializer in an application
82+
// to prevent the object from being stripped by the linker. The symbol is
83+
// located in the "firebase" namespace so can be referenced using:
84+
//
85+
// ::firebase::FIREBASE_APP_REGISTER_CALLBACKS_REFERENCE_NAME(name)
86+
//
87+
// Where "name" is the module name, for example "analytics".
88+
#define FIREBASE_APP_REGISTER_CALLBACKS_INITIALIZER_NAME(module_name) \
89+
g_##module_name##_initializer
90+
91+
// Declare a module initializer variable as a global.
92+
#define FIREBASE_APP_REGISTER_CALLBACKS_INITIALIZER_VARIABLE(module_name) \
93+
namespace firebase { \
94+
extern void* FIREBASE_APP_REGISTER_CALLBACKS_INITIALIZER_NAME(module_name); \
95+
} /* namespace firebase */
96+
97+
// Generates code which references a module initializer.
98+
// For example, FIREBASE_APP_REGISTER_REFERENCE(analytics) will register the
99+
// module initializer for the analytics module.
100+
#define FIREBASE_APP_REGISTER_CALLBACKS_REFERENCE(module_name) \
101+
FIREBASE_APP_REGISTER_CALLBACKS_INITIALIZER_VARIABLE(module_name) \
102+
namespace firebase { \
103+
static void* module_name##_ref FIREBASE_APP_KEEP_SYMBOL = \
104+
&FIREBASE_APP_REGISTER_CALLBACKS_INITIALIZER_NAME(module_name); \
105+
} /* namespace firebase */
106+
#endif // !defined(DOXYGEN) && !defined(SWIG)
107+
108+
#if defined(SWIG) || defined(DOXYGEN)
109+
// SWIG needs to ignore the FIREBASE_DEPRECATED tag.
110+
#define FIREBASE_DEPRECATED
111+
#endif // defined(SWIG) || defined(DOXYGEN)
112+
113+
#ifndef FIREBASE_DEPRECATED
114+
#ifdef __GNUC__
115+
#define FIREBASE_DEPRECATED __attribute__((deprecated))
116+
#elif defined(_MSC_VER)
117+
#define FIREBASE_DEPRECATED __declspec(deprecated)
118+
#else
119+
// We don't know how to mark functions as "deprecated" with this compiler.
120+
#define FIREBASE_DEPRECATED
121+
#endif
122+
#endif // FIREBASE_DEPRECATED
123+
124+
// Calculates the number of elements in an array.
125+
#define FIREBASE_ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0]))
126+
127+
// Guaranteed compile time strlen.
128+
#define FIREBASE_STRLEN(s) (FIREBASE_ARRAYSIZE(s) - sizeof((s)[0]))
129+
130+
#endif // FIRESTORE_CORE_SRC_API_COMMON_H_

Firestore/core/src/api/document_reference.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Firestore/core/src/api/collection_reference.h"
2323
#include "Firestore/core/src/api/document_snapshot.h"
2424
#include "Firestore/core/src/api/firestore.h"
25+
#include "Firestore/core/src/api/optional.h"
2526
#include "Firestore/core/src/api/query_listener_registration.h"
2627
#include "Firestore/core/src/api/source.h"
2728
#include "Firestore/core/src/core/firestore_client.h"
@@ -219,7 +220,9 @@ std::unique_ptr<ListenerRegistration> DocumentReference::AddSnapshotListener(
219220
: false;
220221

221222
DocumentSnapshot result{
222-
firestore_, key_, document,
223+
firestore_, key_,
224+
document ? Optional<Document>(document.value())
225+
: Optional<Document>(),
223226
SnapshotMetadata{has_pending_writes, snapshot.from_cache()}};
224227
user_listener_->OnEvent(std::move(result));
225228
}

Firestore/core/src/api/document_snapshot.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "Firestore/core/src/api/document_reference.h"
2222
#include "Firestore/core/src/model/resource_path.h"
2323
#include "Firestore/core/src/util/hashing.h"
24-
#include "absl/types/optional.h"
2524

2625
namespace firebase {
2726
namespace firestore {
@@ -36,21 +35,22 @@ DocumentSnapshot DocumentSnapshot::FromDocument(
3635
std::shared_ptr<Firestore> firestore,
3736
model::Document document,
3837
SnapshotMetadata metadata) {
39-
return DocumentSnapshot{std::move(firestore), document->key(), document,
38+
return DocumentSnapshot{std::move(firestore), document->key(),
39+
Optional<model::Document>(document),
4040
std::move(metadata)};
4141
}
4242

4343
DocumentSnapshot DocumentSnapshot::FromNoDocument(
4444
std::shared_ptr<Firestore> firestore,
4545
model::DocumentKey key,
4646
SnapshotMetadata metadata) {
47-
return DocumentSnapshot{std::move(firestore), std::move(key), absl::nullopt,
48-
std::move(metadata)};
47+
return DocumentSnapshot{std::move(firestore), std::move(key),
48+
Optional<model::Document>(), std::move(metadata)};
4949
}
5050

5151
DocumentSnapshot::DocumentSnapshot(std::shared_ptr<Firestore> firestore,
5252
model::DocumentKey document_key,
53-
absl::optional<Document> document,
53+
Optional<Document> document,
5454
SnapshotMetadata metadata)
5555
: firestore_{std::move(firestore)},
5656
internal_key_{std::move(document_key)},
@@ -67,7 +67,7 @@ bool DocumentSnapshot::exists() const {
6767
return internal_document_.has_value();
6868
}
6969

70-
const absl::optional<Document>& DocumentSnapshot::internal_document() const {
70+
const Optional<Document>& DocumentSnapshot::internal_document() const {
7171
return internal_document_;
7272
}
7373

@@ -79,10 +79,15 @@ const std::string& DocumentSnapshot::document_id() const {
7979
return internal_key_.path().last_segment();
8080
}
8181

82-
absl::optional<google_firestore_v1_Value> DocumentSnapshot::GetValue(
82+
Optional<google_firestore_v1_Value> DocumentSnapshot::GetValue(
8383
const FieldPath& field_path) const {
84-
return internal_document_ ? (*internal_document_)->field(field_path)
85-
: absl::nullopt;
84+
if (internal_document_) {
85+
auto value = (*internal_document_)->field(field_path);
86+
if (value) {
87+
return Optional<google_firestore_v1_Value>(value.value());
88+
}
89+
}
90+
return {};
8691
}
8792

8893
bool operator==(const DocumentSnapshot& lhs, const DocumentSnapshot& rhs) {

Firestore/core/src/api/document_snapshot.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
#include <string>
2222
#include <utility>
2323

24+
#include "Firestore/core/src/api/optional.h"
2425
#include "Firestore/core/src/api/snapshot_metadata.h"
2526
#include "Firestore/core/src/core/event_listener.h"
2627
#include "Firestore/core/src/model/document.h"
2728
#include "Firestore/core/src/model/document_key.h"
2829
#include "Firestore/core/src/model/model_fwd.h"
29-
#include "absl/types/optional.h"
3030

3131
namespace firebase {
3232
namespace firestore {
@@ -50,7 +50,7 @@ class DocumentSnapshot {
5050
size_t Hash() const;
5151

5252
bool exists() const;
53-
const absl::optional<model::Document>& internal_document() const;
53+
const Optional<model::Document>& internal_document() const;
5454
const std::string& document_id() const;
5555

5656
const SnapshotMetadata& metadata() const {
@@ -59,7 +59,7 @@ class DocumentSnapshot {
5959

6060
DocumentReference CreateReference() const;
6161

62-
absl::optional<google_firestore_v1_Value> GetValue(
62+
Optional<google_firestore_v1_Value> GetValue(
6363
const model::FieldPath& field_path) const;
6464

6565
const std::shared_ptr<Firestore>& firestore() const {
@@ -76,13 +76,13 @@ class DocumentSnapshot {
7676

7777
DocumentSnapshot(std::shared_ptr<Firestore> firestore,
7878
model::DocumentKey document_key,
79-
absl::optional<model::Document> document,
79+
Optional<model::Document> document,
8080
SnapshotMetadata metadata);
8181

8282
private:
8383
std::shared_ptr<Firestore> firestore_;
8484
model::DocumentKey internal_key_;
85-
absl::optional<model::Document> internal_document_;
85+
Optional<model::Document> internal_document_;
8686
SnapshotMetadata metadata_;
8787
};
8888

0 commit comments

Comments
 (0)