Hide Firestore codec class and move each class into separate files (#452)
diff --git a/packages/cloud_firestore/lib/cloud_firestore.dart b/packages/cloud_firestore/lib/cloud_firestore.dart index bf1e390..161244f 100755 --- a/packages/cloud_firestore/lib/cloud_firestore.dart +++ b/packages/cloud_firestore/lib/cloud_firestore.dart
@@ -20,8 +20,11 @@ part 'src/document_snapshot.dart'; part 'src/document_reference.dart'; part 'src/firestore.dart'; +part 'src/geo_point.dart'; part 'src/query.dart'; part 'src/query_snapshot.dart'; part 'src/set_options.dart'; +part 'src/firestore_message_codec.dart'; part 'src/snapshot_metadata.dart'; +part 'src/transaction.dart'; part 'src/write_batch.dart';
diff --git a/packages/cloud_firestore/lib/src/firestore.dart b/packages/cloud_firestore/lib/src/firestore.dart index 161143d..db7868d 100644 --- a/packages/cloud_firestore/lib/src/firestore.dart +++ b/packages/cloud_firestore/lib/src/firestore.dart
@@ -106,108 +106,3 @@ return result?.cast<String, dynamic>() ?? <String, dynamic>{}; } } - -typedef Future<dynamic> TransactionHandler(Transaction transaction); - -class Transaction { - int _transactionId; - - Transaction(this._transactionId); - - Future<DocumentSnapshot> get(DocumentReference documentReference) async { - final dynamic result = await Firestore.channel - .invokeMethod('Transaction#get', <String, dynamic>{ - 'transactionId': _transactionId, - 'path': documentReference.path, - }); - if (result != null) { - return new DocumentSnapshot._(documentReference.path, - result['data'].cast<String, dynamic>(), Firestore.instance); - } else { - return null; - } - } - - Future<void> delete(DocumentReference documentReference) async { - return Firestore.channel - .invokeMethod('Transaction#delete', <String, dynamic>{ - 'transactionId': _transactionId, - 'path': documentReference.path, - }); - } - - Future<void> update( - DocumentReference documentReference, Map<String, dynamic> data) async { - return Firestore.channel - .invokeMethod('Transaction#update', <String, dynamic>{ - 'transactionId': _transactionId, - 'path': documentReference.path, - 'data': data, - }); - } - - Future<void> set( - DocumentReference documentReference, Map<String, dynamic> data) async { - return Firestore.channel.invokeMethod('Transaction#set', <String, dynamic>{ - 'transactionId': _transactionId, - 'path': documentReference.path, - 'data': data, - }); - } -} - -class FirestoreMessageCodec extends StandardMessageCodec { - const FirestoreMessageCodec(); - - static const int _kDateTime = 128; - static const int _kGeoPoint = 129; - static const int _kDocumentReference = 130; - - @override - void writeValue(WriteBuffer buffer, dynamic value) { - if (value is DateTime) { - buffer.putUint8(_kDateTime); - buffer.putInt64(value.millisecondsSinceEpoch); - } else if (value is GeoPoint) { - buffer.putUint8(_kGeoPoint); - buffer.putFloat64(value.latitude); - buffer.putFloat64(value.longitude); - } else if (value is DocumentReference) { - buffer.putUint8(_kDocumentReference); - final List<int> bytes = utf8.encoder.convert(value.path); - writeSize(buffer, bytes.length); - buffer.putUint8List(bytes); - } else { - super.writeValue(buffer, value); - } - } - - @override - dynamic readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case _kDateTime: - return new DateTime.fromMillisecondsSinceEpoch(buffer.getInt64()); - case _kGeoPoint: - return new GeoPoint(buffer.getFloat64(), buffer.getFloat64()); - case _kDocumentReference: - final int length = readSize(buffer); - final String path = utf8.decoder.convert(buffer.getUint8List(length)); - return Firestore.instance.document(path); - default: - return super.readValueOfType(type, buffer); - } - } -} - -class GeoPoint { - final double latitude; - final double longitude; - const GeoPoint(this.latitude, this.longitude); - - @override - bool operator ==(dynamic o) => - o is GeoPoint && o.latitude == latitude && o.longitude == longitude; - - @override - int get hashCode => hashValues(latitude, longitude); -}
diff --git a/packages/cloud_firestore/lib/src/firestore_message_codec.dart b/packages/cloud_firestore/lib/src/firestore_message_codec.dart new file mode 100644 index 0000000..8d30b3f --- /dev/null +++ b/packages/cloud_firestore/lib/src/firestore_message_codec.dart
@@ -0,0 +1,49 @@ +// Copyright 2017, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +part of cloud_firestore; + +@visibleForTesting +class FirestoreMessageCodec extends StandardMessageCodec { + const FirestoreMessageCodec(); + + static const int _kDateTime = 128; + static const int _kGeoPoint = 129; + static const int _kDocumentReference = 130; + + @override + void writeValue(WriteBuffer buffer, dynamic value) { + if (value is DateTime) { + buffer.putUint8(_kDateTime); + buffer.putInt64(value.millisecondsSinceEpoch); + } else if (value is GeoPoint) { + buffer.putUint8(_kGeoPoint); + buffer.putFloat64(value.latitude); + buffer.putFloat64(value.longitude); + } else if (value is DocumentReference) { + buffer.putUint8(_kDocumentReference); + final List<int> bytes = utf8.encoder.convert(value.path); + writeSize(buffer, bytes.length); + buffer.putUint8List(bytes); + } else { + super.writeValue(buffer, value); + } + } + + @override + dynamic readValueOfType(int type, ReadBuffer buffer) { + switch (type) { + case _kDateTime: + return new DateTime.fromMillisecondsSinceEpoch(buffer.getInt64()); + case _kGeoPoint: + return new GeoPoint(buffer.getFloat64(), buffer.getFloat64()); + case _kDocumentReference: + final int length = readSize(buffer); + final String path = utf8.decoder.convert(buffer.getUint8List(length)); + return Firestore.instance.document(path); + default: + return super.readValueOfType(type, buffer); + } + } +}
diff --git a/packages/cloud_firestore/lib/src/geo_point.dart b/packages/cloud_firestore/lib/src/geo_point.dart new file mode 100644 index 0000000..3619da8 --- /dev/null +++ b/packages/cloud_firestore/lib/src/geo_point.dart
@@ -0,0 +1,18 @@ +// Copyright 2017, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +part of cloud_firestore; + +class GeoPoint { + final double latitude; + final double longitude; + const GeoPoint(this.latitude, this.longitude); + + @override + bool operator ==(dynamic o) => + o is GeoPoint && o.latitude == latitude && o.longitude == longitude; + + @override + int get hashCode => hashValues(latitude, longitude); +}
diff --git a/packages/cloud_firestore/lib/src/transaction.dart b/packages/cloud_firestore/lib/src/transaction.dart new file mode 100644 index 0000000..fde6d11 --- /dev/null +++ b/packages/cloud_firestore/lib/src/transaction.dart
@@ -0,0 +1,54 @@ +// Copyright 2017, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +part of cloud_firestore; + +typedef Future<dynamic> TransactionHandler(Transaction transaction); + +class Transaction { + int _transactionId; + + Transaction(this._transactionId); + + Future<DocumentSnapshot> get(DocumentReference documentReference) async { + final dynamic result = await Firestore.channel + .invokeMethod('Transaction#get', <String, dynamic>{ + 'transactionId': _transactionId, + 'path': documentReference.path, + }); + if (result != null) { + return new DocumentSnapshot._(documentReference.path, + result['data'].cast<String, dynamic>(), Firestore.instance); + } else { + return null; + } + } + + Future<void> delete(DocumentReference documentReference) async { + return Firestore.channel + .invokeMethod('Transaction#delete', <String, dynamic>{ + 'transactionId': _transactionId, + 'path': documentReference.path, + }); + } + + Future<void> update( + DocumentReference documentReference, Map<String, dynamic> data) async { + return Firestore.channel + .invokeMethod('Transaction#update', <String, dynamic>{ + 'transactionId': _transactionId, + 'path': documentReference.path, + 'data': data, + }); + } + + Future<void> set( + DocumentReference documentReference, Map<String, dynamic> data) async { + return Firestore.channel.invokeMethod('Transaction#set', <String, dynamic>{ + 'transactionId': _transactionId, + 'path': documentReference.path, + 'data': data, + }); + } +}