Skip to content

Commit

Permalink
more null refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
j4qfrost committed May 6, 2024
1 parent 187915a commit dbac94c
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 107 deletions.
4 changes: 2 additions & 2 deletions packages/core/lib/managed_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ManagedAuthToken extends ManagedObject<_ManagedAuthToken>
/// Instance from an [AuthToken].
ManagedAuthToken.fromToken(AuthToken t) : super() {
final tokenResourceOwner =
entity.relationships!["resourceOwner"]!.destinationEntity.instanceOf();
entity.relationships["resourceOwner"]!.destinationEntity.instanceOf();
tokenResourceOwner["id"] = t.resourceOwnerIdentifier;
this
..accessToken = t.accessToken
Expand All @@ -64,7 +64,7 @@ class ManagedAuthToken extends ManagedObject<_ManagedAuthToken>
/// Instance from an [AuthCode].
ManagedAuthToken.fromCode(AuthCode code) : super() {
final tokenResourceOwner =
entity.relationships!["resourceOwner"]!.destinationEntity.instanceOf();
entity.relationships["resourceOwner"]!.destinationEntity.instanceOf();
tokenResourceOwner["id"] = code.resourceOwnerIdentifier;

this
Expand Down
16 changes: 7 additions & 9 deletions packages/core/lib/src/db/managed/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ManagedEntity implements APIComponentDocumenter {
/// transient property declared in the instance type.
/// The keys are the case-sensitive name of the attribute. Values that represent a relationship to another object
/// are not stored in [attributes].
late Map<String?, ManagedAttributeDescription?> attributes;
late Map<String, ManagedAttributeDescription?> attributes;

/// All relationship values of this entity.
///
Expand All @@ -68,17 +68,15 @@ class ManagedEntity implements APIComponentDocumenter {
/// for [ManagedRelationshipType.hasMany] or [ManagedRelationshipType.hasOne] properties, as those values are derived by the foreign key reference
/// on the inverse relationship property.
/// Keys are the case-sensitive name of the relationship.
Map<String, ManagedRelationshipDescription?>? relationships;
late Map<String, ManagedRelationshipDescription?> relationships;

/// All properties (relationships and attributes) of this entity.
///
/// The string key is the name of the property, case-sensitive. Values will be instances of either [ManagedAttributeDescription]
/// or [ManagedRelationshipDescription]. This is the concatenation of [attributes] and [relationships].
Map<String, ManagedPropertyDescription?> get properties {
final all = Map<String, ManagedPropertyDescription?>.from(attributes);
if (relationships != null) {
all.addAll(relationships!);
}
all.addAll(relationships);
return all;
}

Expand Down Expand Up @@ -117,7 +115,7 @@ class ManagedEntity implements APIComponentDocumenter {
);

elements.addAll(
relationships!.values
relationships.values
.where(
(prop) =>
prop!.isIncludedInDefaultResultSet &&
Expand Down Expand Up @@ -213,7 +211,7 @@ class ManagedEntity implements APIComponentDocumenter {
final propertyName = elements.first!.name;
final attribute = attributes[propertyName];
if (attribute == null) {
if (relationships!.containsKey(propertyName)) {
if (relationships.containsKey(propertyName)) {
throw ArgumentError(
"Invalid property selection. Property '$propertyName' on "
"'$name' "
Expand Down Expand Up @@ -258,7 +256,7 @@ class ManagedEntity implements APIComponentDocumenter {
}

final propertyName = elements.first!.name;
final desc = relationships![propertyName];
final desc = relationships[propertyName];
if (desc == null) {
throw ArgumentError(
"Invalid property selection. Relationship named '$propertyName' on table '$tableName' is not a relationship.",
Expand Down Expand Up @@ -344,7 +342,7 @@ class ManagedEntity implements APIComponentDocumenter {
});

buf.writeln("Relationships:");
relationships!.forEach((name, rel) {
relationships.forEach((name, rel) {
buf.writeln("\t$rel");
});

Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/src/db/managed/property_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,11 @@ class ManagedRelationshipDescription extends ManagedPropertyDescription {
final ManagedRelationshipType relationshipType;

/// The name of the [ManagedRelationshipDescription] on [destinationEntity] that represents the inverse of this relationship.
final String? inverseKey;
final String inverseKey;

/// The [ManagedRelationshipDescription] on [destinationEntity] that represents the inverse of this relationship.
ManagedRelationshipDescription? get inverse =>
destinationEntity.relationships![inverseKey];
destinationEntity.relationships[inverseKey];

/// Whether or not this relationship is on the belonging side.
bool get isBelongsTo => relationshipType == ManagedRelationshipType.belongsTo;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/db/managed/set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ManagedSet<InstanceType extends ManagedObject> extends Object
_innerValues = List<InstanceType>.from(items);
}

late List<InstanceType> _innerValues;
late final List<InstanceType> _innerValues;

/// The number of elements in this set.
@override
Expand Down
17 changes: 7 additions & 10 deletions packages/core/lib/src/db/query/mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ mixin QueryMixin<InstanceType extends ManagedObject>
QuerySortPredicate? sortPredicate;

QueryPage? pageDescriptor;
List<QuerySortDescriptor>? sortDescriptors;
Map<ManagedRelationshipDescription, Query>? subQueries;
final List<QuerySortDescriptor> sortDescriptors = <QuerySortDescriptor>[];
final Map<ManagedRelationshipDescription, Query> subQueries = {};

QueryMixin? _parentQuery;
List<QueryExpression<dynamic, dynamic>> expressions = [];
Expand Down Expand Up @@ -113,8 +113,7 @@ mixin QueryMixin<InstanceType extends ManagedObject>
) {
final attribute = entity.identifyAttribute(propertyIdentifier);

sortDescriptors ??= <QuerySortDescriptor>[];
sortDescriptors!.add(QuerySortDescriptor(attribute.name, order));
sortDescriptors.add(QuerySortDescriptor(attribute.name, order));
}

@override
Expand Down Expand Up @@ -156,7 +155,7 @@ mixin QueryMixin<InstanceType extends ManagedObject>
Query<T> _createSubquery<T extends ManagedObject>(
ManagedRelationshipDescription fromRelationship,
) {
if (subQueries?.containsKey(fromRelationship) ?? false) {
if (subQueries.containsKey(fromRelationship)) {
throw StateError(
"Invalid query. Cannot join same property more than once.",
);
Expand All @@ -165,8 +164,8 @@ mixin QueryMixin<InstanceType extends ManagedObject>
// Ensure we don't cyclically join
var parent = _parentQuery;
while (parent != null) {
if (parent.subQueries!.containsKey(fromRelationship.inverse)) {
final validJoins = fromRelationship.entity.relationships!.values
if (parent.subQueries.containsKey(fromRelationship.inverse)) {
final validJoins = fromRelationship.entity.relationships.values
.where((r) => !identical(r, fromRelationship))
.map((r) => "'${r!.name}'")
.join(", ");
Expand All @@ -183,11 +182,9 @@ mixin QueryMixin<InstanceType extends ManagedObject>
parent = parent._parentQuery;
}

subQueries ??= {};

final subquery = Query<T>(context);
(subquery as QueryMixin)._parentQuery = this;
subQueries![fromRelationship] = subquery;
subQueries[fromRelationship] = subquery;

return subquery;
}
Expand Down
13 changes: 4 additions & 9 deletions packages/core/lib/src/db/query/predicate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,23 @@ class QueryPredicate {
///
/// If [predicates] is null or empty, an empty predicate is returned. If [predicates] contains only
/// one predicate, that predicate is returned.
factory QueryPredicate.and(Iterable<QueryPredicate?>? predicates) {
final predicateList = predicates
?.where((p) => p?.format != null && p!.format.isNotEmpty)
.toList();
if (predicateList == null) {
return QueryPredicate.empty();
}
factory QueryPredicate.and(Iterable<QueryPredicate> predicates) {
final predicateList = predicates.where((p) => p.format.isNotEmpty).toList();

if (predicateList.isEmpty) {
return QueryPredicate.empty();
}

if (predicateList.length == 1) {
return predicateList.first!;
return predicateList.first;
}

// If we have duplicate keys anywhere, we need to disambiguate them.
int dupeCounter = 0;
final allFormatStrings = [];
final valueMap = <String, dynamic>{};
for (final predicate in predicateList) {
final duplicateKeys = predicate!.parameters.keys
final duplicateKeys = predicate.parameters.keys
.where((k) => valueMap.keys.contains(k))
.toList();

Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/src/db/query/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ abstract class Query<InstanceType extends ManagedObject> {
/// var q = Query<User>()
/// ..where.id = whereEqualTo(1);
/// var deletedCount = await q.delete();
Future<int?> delete();
Future<int> delete();
}

/// Order value for [Query.pageBy] and [Query.sortBy].
Expand Down
8 changes: 4 additions & 4 deletions packages/core/lib/src/runtime/orm_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ return entity.symbolMap[Symbol(symbolName)];
final className = MirrorSystem.getName(instanceType.simpleName);
final originalFileUri = instanceType.location!.sourceUri.toString();
final relationshipsStr = (await Future.wait(
entity.relationships!.keys.map((name) async {
return "'$name': ${await _getRelationshipInstantiator(ctx, entity.relationships![name]!, importUris: importUris)}";
entity.relationships.keys.map((name) async {
return "'$name': ${await _getRelationshipInstantiator(ctx, entity.relationships[name]!, importUris: importUris)}";
}),
))
.join(", ");
Expand All @@ -453,7 +453,7 @@ return entity.symbolMap[Symbol(symbolName)];

// Need to import any relationships types and metadata types
// todo: limit import of importUris to only show symbols required to replicate metadata
final directives = entity.relationships!.values.map((r) {
final directives = entity.relationships.values.map((r) {
var mirror = reflectType(r!.declaredType!);
if (mirror.isSubtypeOf(reflectType(ManagedSet))) {
mirror = mirror.typeArguments.first;
Expand Down Expand Up @@ -487,7 +487,7 @@ class ManagedEntityRuntimeImpl extends ManagedEntityRuntime {
_entity.relationships = {$relationshipsStr};
_entity.validators = [];
_entity.validators.addAll(_entity.attributes.values.expand((a) => a!.validators));
_entity.validators.addAll(_entity.relationships?.values.expand((a) => a!.validators) ?? []);
_entity.validators.addAll(_entity.relationships.values.expand((a) => a!.validators));
entity.uniquePropertySet = $uniqueStr;
}
Expand Down
Loading

0 comments on commit dbac94c

Please sign in to comment.