From 97fbd375ee1d3f5c4d94865e127a28883bfd4960 Mon Sep 17 00:00:00 2001 From: gmpassos Date: Tue, 21 May 2024 01:59:47 -0300 Subject: [PATCH] v1.7.5 - New `IterableEntityReferenceExtension` and `IterableEntityReferenceListExtension`. - `EntityReference`: added `idAsInt` and `idNotNullAsInt`. - `EntityReferenceList`: added `idsAsInt` and `idsNotNullAsInt`. --- CHANGELOG.md | 7 ++++ lib/src/bones_api_base.dart | 2 +- lib/src/bones_api_entity_reference.dart | 52 +++++++++++++++++++++++++ pubspec.yaml | 2 +- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fa9dcd..1824071 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.7.5 + +- New `IterableEntityReferenceExtension` and `IterableEntityReferenceListExtension`. + +- `EntityReference`: added `idAsInt` and `idNotNullAsInt`. +- `EntityReferenceList`: added `idsAsInt` and `idsNotNullAsInt`. + ## 1.7.4 - `TimedMap`: diff --git a/lib/src/bones_api_base.dart b/lib/src/bones_api_base.dart index e066d1c..c1aec31 100644 --- a/lib/src/bones_api_base.dart +++ b/lib/src/bones_api_base.dart @@ -42,7 +42,7 @@ typedef APILogger = void Function(APIRoot apiRoot, String type, String? message, /// Bones API Library class. class BonesAPI { // ignore: constant_identifier_names - static const String VERSION = '1.7.4'; + static const String VERSION = '1.7.5'; static bool _boot = false; diff --git a/lib/src/bones_api_entity_reference.dart b/lib/src/bones_api_entity_reference.dart index 6d4954c..70bd66e 100644 --- a/lib/src/bones_api_entity_reference.dart +++ b/lib/src/bones_api_entity_reference.dart @@ -704,6 +704,17 @@ class EntityReference extends EntityReferenceBase { /// See [idAs]. I idNotNullAs() => id as I; + /// Returns [id] as [int] or null. + /// See [idNotNullAs]. + int? get idAsInt { + var id = this.id; + return id is int ? id : null; + } + + /// Returns [id] as [int]. + /// See [idAsInt]. + int get idNotNullAsInt => id as int; + Object? _resolveID() { var id = _id; if (id != null) return id; @@ -1649,6 +1660,15 @@ class EntityReferenceList extends EntityReferenceBase { /// See [idsAs]. List idsNotNullAs() => ids?.whereType().toList() ?? []; + /// Returns the [ids] with type [int] or null. + /// See [idsNotNullAs]. + List get idsAsInt => + ids?.map((e) => e is int ? e : null).toList() ?? []; + + /// Returns the [ids] with type [int]. + /// See [idsAsInt]. + List get idsNotNullAsInt => ids?.whereType().toList() ?? []; + List? _resolveIDs() { var ids = _ids; if (ids != null) return ids; @@ -2356,6 +2376,38 @@ extension NullEntityReferenceListExtension on EntityReferenceList? { FutureOr?> getNotNull() => this?.getNotNull(); } +extension IterableEntityReferenceExtension on Iterable> { + List get allIDs => map((e) => e.id).whereNotNull().toList(); + + List allIDsAs() => map((e) => e.idAs()).whereType().toList(); + + List get allIDsAsInt => map((e) => e.idNotNullAsInt).toList(); + + List get allEntities => map((e) => e.entity).whereType().toList(); +} + +extension IterableEntityReferenceListExtension + on Iterable> { + List get allIDs => expand((e) => e.ids ?? []).whereNotNull().toList(); + + List allIDsAs() => expand((e) => e.idsAs()).whereType().toList(); + + List get allIDsAsInt => expand((e) => e.idsNotNullAsInt).toList(); + + List get allEntities => expand((e) => e.entitiesNotNull).toList(); +} + +extension IterableOfIterableEntityReferenceListExtension + on Iterable>> { + List get allIDs => expand((e) => e.allIDs).toList(); + + List allIDsAs() => expand((e) => e.allIDsAs()).toList(); + + List get allIDsAsInt => expand((e) => e.allIDsAsInt).toList(); + + List get allEntities => expand((e) => e.allEntities).toList(); +} + extension _ListExtension on List { List mergeWith(List other) => List.generate(length, (i) { T a = this[i]; diff --git a/pubspec.yaml b/pubspec.yaml index f77b6c6..afff727 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: bones_api description: Bones_API - A powerful API backend framework for Dart. It comes with a built-in HTTP Server, route handler, entity handler, SQL translator, and DB adapters. -version: 1.7.4 +version: 1.7.5 homepage: https://github.com/Colossus-Services/bones_api environment: