Visitar URL original
feat(firestore,windows): add support for Blob decoding on Windows (#1… · firebase/flutterfire@068db51 · GitHub
Skip to content

Commit 068db51

Browse files
authored
feat(firestore,windows): add support for Blob decoding on Windows (#18432)
* feat(firestore,windows): add support for Blob decoding on Windows * fix
1 parent 755bf69 commit 068db51

5 files changed

Lines changed: 59 additions & 8 deletions

File tree

‎packages/cloud_firestore/cloud_firestore/example/integration_test/document_reference_e2e.dart‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ void runDocumentReferenceTests() {
268268
});
269269

270270
group('DocumentReference.get()', () {
271+
test('gets blob data', () async {
272+
final document = await initializeTest('document-get-blob');
273+
final blob = Blob(Uint8List.fromList(<int>[0, 127, 255]));
274+
await document.set(<String, Object?>{'blob': blob});
275+
276+
final snapshot = await document.get();
277+
278+
expect(snapshot.get('blob'), blob);
279+
});
280+
271281
test(
272282
'preserves native error messages when offline',
273283
() async {

‎packages/cloud_firestore/cloud_firestore/ios/cloud_firestore/Sources/cloud_firestore/FLTFirebaseFirestoreWriter.m‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#import "include/cloud_firestore/Private/FLTFirebaseFirestoreUtils.h"
1010
#import "include/cloud_firestore/Public/FLTFirebaseFirestorePlugin.h"
1111

12+
static const UInt8 FLTStandardFieldList = 12;
13+
static const UInt8 FLTStandardFieldMap = 13;
14+
1215
@implementation FLTFirebaseFirestoreWriter : FlutterStandardWriter
1316
- (void)writeValue:(id)value {
1417
if ([value isKindOfClass:[NSDate class]]) {
@@ -51,15 +54,30 @@ - (void)writeValue:(id)value {
5154
[self writeValue:extension.databaseURL];
5255

5356
} else if ([value isKindOfClass:[FIRDocumentSnapshot class]]) {
54-
[super writeValue:[self FIRDocumentSnapshot:value]];
57+
[self writeValue:[self FIRDocumentSnapshot:value]];
5558
} else if ([value isKindOfClass:[FIRLoadBundleTaskProgress class]]) {
56-
[super writeValue:[self FIRLoadBundleTaskProgress:value]];
59+
[self writeValue:[self FIRLoadBundleTaskProgress:value]];
5760
} else if ([value isKindOfClass:[FIRQuerySnapshot class]]) {
58-
[super writeValue:[self FIRQuerySnapshot:value]];
61+
[self writeValue:[self FIRQuerySnapshot:value]];
5962
} else if ([value isKindOfClass:[FIRDocumentChange class]]) {
60-
[super writeValue:[self FIRDocumentChange:value]];
63+
[self writeValue:[self FIRDocumentChange:value]];
6164
} else if ([value isKindOfClass:[FIRSnapshotMetadata class]]) {
62-
[super writeValue:[self FIRSnapshotMetadata:value]];
65+
[self writeValue:[self FIRSnapshotMetadata:value]];
66+
} else if ([value isKindOfClass:[NSArray class]]) {
67+
NSArray *list = value;
68+
[self writeByte:FLTStandardFieldList];
69+
[self writeSize:(UInt32)list.count];
70+
for (id item in list) {
71+
[self writeValue:item];
72+
}
73+
} else if ([value isKindOfClass:[NSDictionary class]]) {
74+
NSDictionary *map = value;
75+
[self writeByte:FLTStandardFieldMap];
76+
[self writeSize:(UInt32)map.count];
77+
for (id key in map) {
78+
[self writeValue:key];
79+
[self writeValue:map[key]];
80+
}
6381
} else if ([value isKindOfClass:[NSNumber class]]) {
6482
NSNumber *number = (NSNumber *)value;
6583

‎packages/cloud_firestore/cloud_firestore/windows/cloud_firestore_plugin.cpp‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <mutex>
2222
#include <queue>
2323
#include <sstream>
24+
#include <vector>
2425

2526
#include "cloud_firestore/plugin_version.h"
2627
#include "firebase/app.h"
@@ -470,7 +471,8 @@ GetServerTimestampBehaviorFromPigeon(
470471
}
471472
}
472473

473-
EncodableValue ConvertFieldValueToEncodableValue(const FieldValue& fieldValue) {
474+
EncodableValue CloudFirestorePlugin::ConvertFieldValueToEncodableValue(
475+
const FieldValue& fieldValue) {
474476
switch (fieldValue.type()) {
475477
case FieldValue::Type::kNull:
476478
return EncodableValue();
@@ -492,6 +494,15 @@ EncodableValue ConvertFieldValueToEncodableValue(const FieldValue& fieldValue) {
492494
case FieldValue::Type::kString:
493495
return EncodableValue(fieldValue.string_value());
494496

497+
case FieldValue::Type::kBlob: {
498+
std::vector<uint8_t> bytes;
499+
if (fieldValue.blob_size() > 0) {
500+
bytes.assign(fieldValue.blob_value(),
501+
fieldValue.blob_value() + fieldValue.blob_size());
502+
}
503+
return CustomEncodableValue(bytes);
504+
}
505+
495506
case FieldValue::Type::kMap: {
496507
EncodableMap encodableMap;
497508
for (const auto& [key, val] : fieldValue.map_value()) {
@@ -527,8 +538,9 @@ flutter::EncodableMap ConvertToEncodableMap(
527538
EncodableMap convertedMap;
528539
for (const auto& kv : originalMap) {
529540
EncodableValue key = EncodableValue(kv.first);
530-
EncodableValue value = ConvertFieldValueToEncodableValue(
531-
kv.second); // convert FieldValue to EncodableValue
541+
EncodableValue value =
542+
CloudFirestorePlugin::ConvertFieldValueToEncodableValue(
543+
kv.second); // convert FieldValue to EncodableValue
532544
convertedMap[key] = value; // insert into the new map
533545
}
534546
return convertedMap;

‎packages/cloud_firestore/cloud_firestore/windows/cloud_firestore_plugin.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class CloudFirestorePlugin : public flutter::Plugin,
2727
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows* registrar);
2828
static firebase::firestore::FieldValue ConvertToFieldValue(
2929
const flutter::EncodableValue& variant);
30+
static flutter::EncodableValue ConvertFieldValueToEncodableValue(
31+
const firebase::firestore::FieldValue& fieldValue);
3032

3133
CloudFirestorePlugin();
3234

‎packages/cloud_firestore/cloud_firestore/windows/firestore_codec.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <memory>
99
#include <optional>
1010
#include <string>
11+
#include <vector>
1112

1213
#include "cloud_firestore_plugin.h"
1314
#include "firebase/app.h"
@@ -65,6 +66,14 @@ void cloud_firestore_windows::FirestoreCodec::WriteValue(
6566
flutter::StandardCodecSerializer::WriteValue(appName, stream);
6667
flutter::StandardCodecSerializer::WriteValue(reference.path(), stream);
6768
flutter::StandardCodecSerializer::WriteValue(databaseUrl, stream);
69+
} else if (custom_value.type() == typeid(std::vector<uint8_t>)) {
70+
const std::vector<uint8_t>& bytes =
71+
std::any_cast<const std::vector<uint8_t>&>(custom_value);
72+
stream->WriteByte(DATA_TYPE_BLOB);
73+
WriteSize(bytes.size(), stream);
74+
if (!bytes.empty()) {
75+
stream->WriteBytes(bytes.data(), bytes.size());
76+
}
6877
} else if (custom_value.type() ==
6978
typeid(double)) { // Assuming Double is standard C++ double
7079
const double& myDouble = std::any_cast<double>(custom_value);

0 commit comments

Comments
 (0)