Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
Fix Dart 2 runtime issues. (#53)
Browse files Browse the repository at this point in the history
Upgrade to Dart 2.0.0-dev.54.0 to fix issues with mirrors in Dart 2
mode. Added lots of types.
  • Loading branch information
jakobr-google committed May 18, 2018
1 parent a509960 commit bd6de71
Show file tree
Hide file tree
Showing 24 changed files with 174 additions and 158 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.dart_tool/
pubspec.lock
packages
.pub
.packages
.packages
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0

* Fixes to support Dart 2.

## 0.4.0+1

* Made a number of strong-mode improvements.
Expand Down
4 changes: 2 additions & 2 deletions lib/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class StreamFromPages<T> {
bool _paused = false;
bool _cancelled = false;
Page _currentPage;
StreamController _controller;
StreamController<T> _controller;

StreamFromPages(this._firstPageProvider) {
_controller = new StreamController(
_controller = new StreamController<T>(
sync: true,
onListen: _onListen,
onPause: _onPause,
Expand Down
2 changes: 1 addition & 1 deletion lib/datastore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Key {

factory Key.fromParent(String kind, int id, {Key parent}) {
var partition;
var elements = [];
var elements = <KeyElement>[];
if (parent != null) {
partition = parent.partition;
elements.addAll(parent.elements);
Expand Down
2 changes: 1 addition & 1 deletion lib/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'dart:core' as core;
import 'dart:mirrors' as mirrors;

import 'common.dart' show StreamFromPages;
import 'datastore.dart' as datastore;
import 'datastore.dart' as ds;
import 'service_scope.dart' as ss;

part 'src/db/annotations.dart';
Expand Down
4 changes: 2 additions & 2 deletions lib/service_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ void register(Object key, Object value, {onScopeExit()}) {
///
/// The registered on-scope-exit functions are executed in reverse registration
/// order.
Object registerScopeExitCallback(onScopeExitCallback()) {
void registerScopeExitCallback(onScopeExitCallback()) {
var serviceScope = _serviceScope;
if (serviceScope == null) {
throw new StateError('Not running inside a service scope zone.');
}
return serviceScope.registerOnScopeExitCallback(onScopeExitCallback);
serviceScope.registerOnScopeExitCallback(onScopeExitCallback);
}

/// Look up an item by it's key in the currently active service scope.
Expand Down
13 changes: 7 additions & 6 deletions lib/src/datastore_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ class DatastoreImpl implements datastore.Datastore {
}

static datastore.Entity _convertApi2DatastoreEntity(api.Entity entity) {
var unindexedProperties = new Set();
var properties = {};
var unindexedProperties = new Set<String>();
var properties = <String, Object>{};

if (entity.properties != null) {
entity.properties.forEach((String name, api.Value value) {
Expand Down Expand Up @@ -267,7 +267,7 @@ class DatastoreImpl implements datastore.Datastore {
return orders.map(_convertDatastore2ApiOrder).toList();
}

static Future _handleError(error, stack) {
static Future<Null> _handleError(error, stack) {
if (error is api.DetailedApiRequestError) {
if (error.status == 400) {
return new Future.error(
Expand Down Expand Up @@ -317,7 +317,7 @@ class DatastoreImpl implements datastore.Datastore {
request.mode = 'NON_TRANSACTIONAL';
}

var mutations = request.mutations = [];
var mutations = request.mutations = <api.Mutation>[];
if (inserts != null) {
for (int i = 0; i < inserts.length; i++) {
mutations.add(new api.Mutation()
Expand Down Expand Up @@ -349,7 +349,8 @@ class DatastoreImpl implements datastore.Datastore {
keys = mutationResults
.skip(autoIdStartIndex)
.take(autoIdInserts.length)
.map((api.MutationResult r) => _convertApi2DatastoreKey(r.key))
.map<datastore.Key>(
(api.MutationResult r) => _convertApi2DatastoreKey(r.key))
.toList();
}
return new datastore.CommitResult(keys);
Expand Down Expand Up @@ -495,7 +496,7 @@ class QueryPageImpl implements Page<datastore.Entity> {
request.query.limit = batchLimit;

return api.projects.runQuery(request, project).then((response) {
var returnedEntities = const [];
var returnedEntities = const <datastore.Entity>[];

var batch = response.batch;
if (batch.entityResults != null) {
Expand Down
11 changes: 8 additions & 3 deletions lib/src/db/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class ModelKeyProperty extends PrimitiveProperty {

Object decodePrimitiveValue(ModelDB db, Object value) {
if (value == null) return null;
return db.fromDatastoreKey(value as datastore.Key);
return db.fromDatastoreKey(value as ds.Key);
}
}

Expand All @@ -201,13 +201,13 @@ class BlobProperty extends PrimitiveProperty {

Object encodeValue(ModelDB db, Object value, {bool forComparison: false}) {
if (value == null) return null;
return new datastore.BlobValue(value);
return new ds.BlobValue(value);
}

Object decodePrimitiveValue(ModelDB db, Object value) {
if (value == null) return null;

return (value as datastore.BlobValue).bytes;
return (value as ds.BlobValue).bytes;
}
}

Expand Down Expand Up @@ -298,4 +298,9 @@ class StringListProperty extends ListProperty {
const StringListProperty({String propertyName, bool indexed: true})
: super(const StringProperty(),
propertyName: propertyName, indexed: indexed);

@override
Object decodePrimitiveValue(ModelDB db, Object value) {
return (super.decodePrimitiveValue(db, value) as core.List).cast<String>();
}
}
54 changes: 27 additions & 27 deletions lib/src/db/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Transaction {
static const int _TRANSACTION_COMMITTED = 2;

final DatastoreDB db;
final datastore.Transaction _datastoreTransaction;
final ds.Transaction _datastoreTransaction;

final List<Model> _inserts = [];
final List<Key> _deletes = [];
Expand Down Expand Up @@ -108,30 +108,30 @@ class Transaction {
}

class Query {
final _relationMapping = const <String, datastore.FilterRelation>{
'<': datastore.FilterRelation.LessThan,
'<=': datastore.FilterRelation.LessThanOrEqual,
'>': datastore.FilterRelation.GreatherThan,
'>=': datastore.FilterRelation.GreatherThanOrEqual,
'=': datastore.FilterRelation.Equal,
final _relationMapping = const <String, ds.FilterRelation>{
'<': ds.FilterRelation.LessThan,
'<=': ds.FilterRelation.LessThanOrEqual,
'>': ds.FilterRelation.GreatherThan,
'>=': ds.FilterRelation.GreatherThanOrEqual,
'=': ds.FilterRelation.Equal,
};

final DatastoreDB _db;
final datastore.Transaction _transaction;
final ds.Transaction _transaction;
final String _kind;

final Partition _partition;
final Key _ancestorKey;

final List<datastore.Filter> _filters = [];
final List<datastore.Order> _orders = [];
final List<ds.Filter> _filters = [];
final List<ds.Order> _orders = [];
int _offset;
int _limit;

Query(DatastoreDB dbImpl, Type kind,
{Partition partition,
Key ancestorKey,
datastore.Transaction datastoreTransaction})
ds.Transaction datastoreTransaction})
: _db = dbImpl,
_kind = dbImpl.modelDB.kindName(kind),
_partition = partition,
Expand Down Expand Up @@ -165,11 +165,11 @@ class Query {
// This is for backwards compatibility: We allow [datastore.Key]s for now.
// TODO: We should remove the condition in a major version update of
// `package:gcloud`.
if (comparisonObject is! datastore.Key) {
if (comparisonObject is! ds.Key) {
comparisonObject = _db.modelDB
.toDatastoreValue(_kind, name, comparisonObject, forComparison: true);
}
_filters.add(new datastore.Filter(
_filters.add(new ds.Filter(
_relationMapping[comparison], propertyName, comparisonObject));
}

Expand All @@ -182,11 +182,11 @@ class Query {
void order(String orderString) {
// TODO: validate [orderString] (e.g. is name valid)
if (orderString.startsWith('-')) {
_orders.add(new datastore.Order(datastore.OrderDirection.Decending,
_orders.add(new ds.Order(ds.OrderDirection.Decending,
_convertToDatastoreName(orderString.substring(1))));
} else {
_orders.add(new datastore.Order(datastore.OrderDirection.Ascending,
_convertToDatastoreName(orderString)));
_orders.add(new ds.Order(
ds.OrderDirection.Ascending, _convertToDatastoreName(orderString)));
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ class Query {
if (_ancestorKey != null) {
ancestorKey = _db.modelDB.toDatastoreKey(_ancestorKey);
}
var query = new datastore.Query(
var query = new ds.Query(
ancestorKey: ancestorKey,
kind: _kind,
filters: _filters,
Expand All @@ -230,10 +230,10 @@ class Query {

var partition;
if (_partition != null) {
partition = new datastore.Partition(_partition.namespace);
partition = new ds.Partition(_partition.namespace);
}

return new StreamFromPages((int pageSize) {
return new StreamFromPages<ds.Entity>((int pageSize) {
return _db.datastore
.query(query, transaction: _transaction, partition: partition);
}).stream.map(_db.modelDB.fromDatastoreEntity);
Expand All @@ -254,7 +254,7 @@ class Query {
}

class DatastoreDB {
final datastore.Datastore datastore;
final ds.Datastore datastore;
final ModelDB _modelDB;
Partition _defaultPartition;

Expand Down Expand Up @@ -356,13 +356,13 @@ class DatastoreDB {
Future _commitHelper(DatastoreDB db,
{List<Model> inserts,
List<Key> deletes,
datastore.Transaction datastoreTransaction}) {
ds.Transaction datastoreTransaction}) {
var entityInserts, entityAutoIdInserts, entityDeletes;
var autoIdModelInserts;
if (inserts != null) {
entityInserts = [];
entityAutoIdInserts = [];
autoIdModelInserts = [];
entityInserts = <ds.Entity>[];
entityAutoIdInserts = <ds.Entity>[];
autoIdModelInserts = <Model>[];

for (var model in inserts) {
// If parent was not explicitly set, we assume this model will map to
Expand All @@ -388,7 +388,7 @@ Future _commitHelper(DatastoreDB db,
autoIdInserts: entityAutoIdInserts,
deletes: entityDeletes,
transaction: datastoreTransaction)
.then((datastore.CommitResult result) {
.then((ds.CommitResult result) {
if (entityAutoIdInserts != null && entityAutoIdInserts.length > 0) {
for (var i = 0; i < result.autoIdInsertKeys.length; i++) {
var key = db.modelDB.fromDatastoreKey(result.autoIdInsertKeys[i]);
Expand All @@ -400,11 +400,11 @@ Future _commitHelper(DatastoreDB db,
}

Future<List<Model>> _lookupHelper(DatastoreDB db, List<Key> keys,
{datastore.Transaction datastoreTransaction}) {
{ds.Transaction datastoreTransaction}) {
var entityKeys = keys.map(db.modelDB.toDatastoreKey).toList();
return db.datastore
.lookup(entityKeys, transaction: datastoreTransaction)
.then((List<datastore.Entity> entities) {
.then((List<ds.Entity> entities) {
return entities.map(db.modelDB.fromDatastoreEntity).toList();
});
}
16 changes: 8 additions & 8 deletions lib/src/db/model_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ part of gcloud.db;
*/
abstract class ModelDB {
/**
* Converts a [datastore.Key] to a [Key].
* Converts a [ds.Key] to a [Key].
*/
Key fromDatastoreKey(datastore.Key datastoreKey);
Key fromDatastoreKey(ds.Key datastoreKey);

/**
* Converts a [Key] to a [datastore.Key].
* Converts a [Key] to a [ds.Key].
*/
datastore.Key toDatastoreKey(Key dbKey);
ds.Key toDatastoreKey(Key dbKey);

/**
* Converts a [Model] instance to a [datastore.Entity].
* Converts a [Model] instance to a [ds.Entity].
*/
datastore.Entity toDatastoreEntity(Model model);
ds.Entity toDatastoreEntity(Model model);

/**
* Converts a [datastore.Entity] to a [Model] instance.
* Converts a [ds.Entity] to a [Model] instance.
*/
Model fromDatastoreEntity(datastore.Entity entity);
Model fromDatastoreEntity(ds.Entity entity);

/**
* Returns the kind name for instances of [type].
Expand Down
Loading

0 comments on commit bd6de71

Please sign in to comment.