Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
## 1.9.3-beta.6

- `TypeInfoEntityExtension`, `TypeReflectionEntityExtension`:
- `entityType`: also handle `List<E>`, returning the `List` generic type (`E`).

- `TypeInfoEntityExtension`:
- Added `toCastedList`.

- `DBSQLAdapter`:
- `_checkDBTableScheme`:
- Separate references and collection references in `referenceFields` and `collectionReferenceFields`.
- `_DBTableCheck`: added field `missingCollectionReferenceColumns`.

- `EntityHandler`:
- `resolveFieldsValues`: ensure that `List<E>` fields are casted to the list, using `entityType.toCastedList(val)`.

- New `InitializationError`.

- `Initializable`: better handling of errors of dependencies while initializing.

- async_extension: ^1.2.15
- args: ^2.7.0
- postgres: ^3.5.5
- archive: ^4.0.5
- postgres: ^3.5.6
- archive: ^4.0.7

- coverage: ^1.12.0

## 1.9.3-beta.5

Expand Down
5 changes: 4 additions & 1 deletion lib/src/bones_api_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,9 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {
entityProvider: entityProvider,
entityHandlerProvider: entityHandlerProvider,
entityCache: entityCache));
} else if (t.isListEntity) {
var entityType = t.arguments0 ?? TypeInfo.tObject;
v2 = v2.resolveMapped((val) => entityType.toCastedList(val));
}
}

Expand Down Expand Up @@ -1414,7 +1417,7 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {

if (enumFields.containsKey(field)) return null;

if (typeInfo.isEntityReferenceListType) return null;
if (typeInfo.isListEntityOrReference) return null;

var entityType = typeInfo.entityType;
if (entityType == null) return null;
Expand Down
64 changes: 52 additions & 12 deletions lib/src/bones_api_entity_db_sql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,10 @@
alterTablesSQLs.removeAt(0);
}

_log.info(
'Suggestion of SQLs (`$dialectName`) to fix missing columns in tables:\n\n ${alterTablesSQLs.join('\n ')}\n');
if (alterTablesSQLs.isNotEmpty) {
_log.info(
'Suggestion of SQLs (`$dialectName`) to fix missing columns in tables:\n\n ${alterTablesSQLs.join('\n ')}\n');

Check warning on line 584 in lib/src/bones_api_entity_db_sql.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_sql.dart#L582-L584

Added lines #L582 - L584 were not covered by tests
}

var allErrors = repositoriesChecksErrors.join('\n');

Expand Down Expand Up @@ -664,7 +666,15 @@

var referenceFields = repoFieldsNotInScheme
.map((f) => _checkDBTableSchemeReferenceField(
entityHandler, scheme, repoTable, f))
entityHandler, scheme, repoTable, f,
allowCollectionFields: false))
.nonNulls
.toMapFromEntries();

var collectionReferenceFields = repoFieldsNotInScheme
.map((f) => _checkDBTableSchemeReferenceField(
entityHandler, scheme, repoTable, f,
allowCollectionFields: true))
.nonNulls
.toMapFromEntries();

Expand All @@ -673,9 +683,16 @@
.map((e) => e.key)
.toList();

var missingCollectionReferenceColumns = collectionReferenceFields.entries
.where((e) => e.value == null)
.map((e) => e.key)
.toList();

var missingColumns = repoFieldsNames
.whereNot(
(f) => fieldsInScheme.contains(f) || referenceFields.containsKey(f))
.whereNot((f) =>
fieldsInScheme.contains(f) ||
referenceFields.containsKey(f) ||
collectionReferenceFields.containsKey(f))
.toList();

var missingFieldReferenceConstraints = <String>{};
Expand Down Expand Up @@ -797,6 +814,7 @@

if (missingColumns.isNotEmpty ||
missingReferenceColumns.isNotEmpty ||
missingCollectionReferenceColumns.isNotEmpty ||
missingFieldReferenceConstraints.isNotEmpty ||
missingUniqueConstraints.isNotEmpty ||
missingEnumConstraints.isNotEmpty ||
Expand All @@ -805,6 +823,7 @@
"ERROR Checking table `$schemeTableName`> entityType: `$repoType`\n"
"${missingColumns.isNotEmpty ? '-- missingColumns: $missingColumns\n' : ''}"
"${missingReferenceColumns.isNotEmpty ? '-- missingReferenceColumns: $missingReferenceColumns\n' : ''}"
"${missingCollectionReferenceColumns.isNotEmpty ? '-- missingCollectionReferenceColumns: $missingCollectionReferenceColumns\n' : ''}"

Check warning on line 826 in lib/src/bones_api_entity_db_sql.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_sql.dart#L826

Added line #L826 was not covered by tests
"${missingFieldReferenceConstraints.isNotEmpty ? '-- missingFieldReferenceConstraints: $missingFieldReferenceConstraints\n' : ''}"
"${missingUniqueConstraints.isNotEmpty ? '-- missingUniqueConstraints: $missingUniqueConstraints\n' : ''}"
"${missingEnumConstraints.isNotEmpty ? '-- missingEnumConstraints: $missingEnumConstraints\n' : ''}"
Expand All @@ -819,6 +838,9 @@
missingReferenceColumns
.map((f) => _DBTableColumn.fromEntityHandler(entityHandler, f))
.toList(),
missingCollectionReferenceColumns
.map((f) => _DBTableColumn.fromEntityHandler(entityHandler, f))
.toList(),

Check warning on line 843 in lib/src/bones_api_entity_db_sql.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_sql.dart#L842-L843

Added lines #L842 - L843 were not covered by tests
missingFieldReferenceConstraints
.map((f) => _DBTableColumn.fromEntityHandler(entityHandler, f))
.toList(),
Expand All @@ -842,17 +864,20 @@
}

MapEntry<String, TableRelationshipReference?>?
_checkDBTableSchemeReferenceField(
EntityHandler<Object> entityHandler,
TableScheme scheme,
String table,
String fieldName,
) {
_checkDBTableSchemeReferenceField(EntityHandler<Object> entityHandler,
TableScheme scheme, String table, String fieldName,
{required bool allowCollectionFields}) {
var fieldType =
entityHandler.getFieldType(null, fieldName, resolveFiledName: false);
if (fieldType == null) return null;

if (!allowCollectionFields && fieldType.isListEntityOrReference) {
return null;
}

var entityType = fieldType.entityTypeInfo ?? fieldType.listEntityType;
if (entityType == null) return null;

var targetTable = getTableForType(entityType);
if (targetTable == null) return null;

Expand Down Expand Up @@ -2597,6 +2622,8 @@

List<_DBTableColumn>? missingReferenceColumns;

List<_DBTableColumn>? missingCollectionReferenceColumns;

List<_DBTableColumn>? missingReferenceConstraints;

List<_DBTableColumn>? missingUniqueConstraints;
Expand All @@ -2609,6 +2636,7 @@
this.scheme,
this.missingColumns,
this.missingReferenceColumns,
this.missingCollectionReferenceColumns,
this.missingReferenceConstraints,
this.missingUniqueConstraints,
this.missingEnumConstraints,
Expand All @@ -2624,6 +2652,7 @@
error == null &&
(missingColumns?.isEmpty ?? true) &&
(missingReferenceColumns?.isEmpty ?? true) &&
(missingCollectionReferenceColumns?.isEmpty ?? true) &&
(missingReferenceConstraints?.isEmpty ?? true) &&
(missingUniqueConstraints?.isEmpty ?? true) &&
(missingEnumConstraints?.isEmpty ?? true) &&
Expand Down Expand Up @@ -2715,8 +2744,12 @@
String toString() {
var missingColumns = this.missingColumns ?? [];
var missingReferenceColumns = this.missingReferenceColumns ?? [];
var missingCollectionReferenceColumns =
this.missingCollectionReferenceColumns ?? [];

Check warning on line 2748 in lib/src/bones_api_entity_db_sql.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_sql.dart#L2748

Added line #L2748 was not covered by tests

if (missingColumns.isNotEmpty || missingReferenceColumns.isNotEmpty) {
if (missingColumns.isNotEmpty ||
missingReferenceColumns.isNotEmpty ||
missingCollectionReferenceColumns.isNotEmpty) {

Check warning on line 2752 in lib/src/bones_api_entity_db_sql.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_sql.dart#L2750-L2752

Added lines #L2750 - L2752 were not covered by tests
var repoType = repository?.type;
var schemeTableName = scheme?.name;

Expand All @@ -2728,13 +2761,20 @@
.map((e) => '${e.type.toString(withT: false)} ${e.name}')
.join(' , ');

var missingCollectionReferenceColumnsStr =
missingCollectionReferenceColumns
.map((e) => '${e.type.toString(withT: false)} ${e.name}')
.join(' , ');

Check warning on line 2767 in lib/src/bones_api_entity_db_sql.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_sql.dart#L2766-L2767

Added lines #L2766 - L2767 were not covered by tests

var errorMsg = [
"Can't find all `$repoType` fields in table `$schemeTableName` scheme:",
" -- repository: $repository\n -- scheme: $scheme",
if (missingColumns.isNotEmpty)
" -- missingColumns: [$missingColumnsStr]",
if (missingReferenceColumns.isNotEmpty)
" -- missingReferenceColumns: [$missingReferenceColumnsStr]",
if (missingCollectionReferenceColumns.isNotEmpty)
" -- missingCollectionReferenceColumns: [$missingCollectionReferenceColumnsStr]",

Check warning on line 2777 in lib/src/bones_api_entity_db_sql.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_entity_db_sql.dart#L2776-L2777

Added lines #L2776 - L2777 were not covered by tests
].join('\n');

return '$errorMsg\n';
Expand Down
25 changes: 23 additions & 2 deletions lib/src/bones_api_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@
bool get isEntityReferenceListType => type == EntityReferenceList;

/// Returns a valid entity [Type].
/// If this [TypeInfo] is an [EntityReference] it will return the [EntityReference.type].
/// - If this [TypeInfo] is an [EntityReferenceBase] it will return the [EntityReferenceBase.type].
/// - If this [TypeInfo] is an [List] it will return the [arguments0].
/// See [EntityHandler.isValidEntityType].
Type? get entityType {
var type = this.type;
Expand All @@ -487,6 +488,8 @@
if (type.isEntityReferenceBaseType) {
var arguments = this.arguments;
entityType = arguments.isNotEmpty ? arguments0!.type : null;
} else if (isListType) {
entityType = arguments0?.type;

Check warning on line 492 in lib/src/bones_api_extension.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_extension.dart#L491-L492

Added lines #L491 - L492 were not covered by tests
} else {
entityType = type;
}
Expand Down Expand Up @@ -540,14 +543,18 @@
type == EntityReference || type == EntityReferenceList;

/// Returns a valid entity [Type].
/// If this [TypeInfo] is an [EntityReference] it will return the [EntityReference.type].
/// - If this [TypeInfo] is an [EntityReferenceBase] it will return the [EntityReferenceBase.type].
/// - If this [TypeInfo] is an [List] it will return the [arguments0].
/// See [EntityHandler.isValidEntityType].
Type? get entityType {
var type = this.type;

Type? entityType;

if (type.isEntityReferenceBaseType) {
entityType = arguments0?.type;
} else if (isList) {
entityType = arguments0?.type;
} else {
entityType = type;
}
Expand Down Expand Up @@ -764,6 +771,20 @@
}
}

List<E>? toCastedList<E>(Object? val) {
return callCasted<List?>(<A>() {
if (val is List<A>) {
return val;
} else if (val is Iterable<A>) {
return val.toList();
} else if (val is A) {
return <A>[val];

Check warning on line 781 in lib/src/bones_api_extension.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/bones_api_extension.dart#L778-L781

Added lines #L778 - L781 were not covered by tests
} else {
return null;
}
}) as List<E>?;
}

EntityReferenceList<T> toEntityReferenceList(Object? o,
{Type? type,
String? typeName,
Expand Down
Loading