From ae2123b6eb105deca7b78cebbc8cfd4fa2892fa8 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Mon, 30 Oct 2023 19:45:37 +0100 Subject: [PATCH 01/33] Fixed controllers.md --- docs/controllers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/controllers.md b/docs/controllers.md index 17a1d865..a308ecd5 100644 --- a/docs/controllers.md +++ b/docs/controllers.md @@ -149,7 +149,7 @@ part 'city_controller.g.dart'; @Controller('/cities') class CitiesController { @Get('/') - String findById(@PathParam() id) { + String findById(@PathParam() int id) { return 'This action returns a city with id: $id'; } } From 1b7705c58e388018c391faca8d8a227637635039 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Mon, 30 Oct 2023 19:45:56 +0100 Subject: [PATCH 02/33] Added type --- packages/dartness_generator/lib/src/controller_generator.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/dartness_generator/lib/src/controller_generator.dart b/packages/dartness_generator/lib/src/controller_generator.dart index 911366fd..345cdc37 100644 --- a/packages/dartness_generator/lib/src/controller_generator.dart +++ b/packages/dartness_generator/lib/src/controller_generator.dart @@ -181,9 +181,9 @@ class ControllerGenerator extends GeneratorForAnnotation { } Expression _paramElementToParamRef(final ParameterElement param) { - final isQuery = _queryParamType.hasAnnotationOfExact(param); + final bool isQuery = _queryParamType.hasAnnotationOfExact(param); bool isPath = _pathParamType.hasAnnotationOfExact(param); - final isBody = _bodyType.hasAnnotationOfExact(param); + final bool isBody = _bodyType.hasAnnotationOfExact(param); if (isQuery && isPath) { throw InvalidGenerationSourceError( 'Param `${param.name}` cannot be both @QueryParam and @PathParam'); From 9b23e10624d23e6f14cd4fcf110ef086e490f41e Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Tue, 31 Oct 2023 12:23:30 +0100 Subject: [PATCH 03/33] abstract class to abstract interface class --- .../lib/src/exception/dartness_error_handler_register.dart | 2 +- .../lib/src/exception/http_status_exception.dart | 2 +- packages/dartness_server/lib/src/provider.dart | 2 +- packages/dartness_server/lib/src/route/dartness_router.dart | 2 +- .../lib/src/route/shelf_route/shelf_handler.dart | 2 +- .../dartness_server/lib/src/server/dartness_interceptor.dart | 2 +- .../dartness_server/lib/src/server/dartness_middleware.dart | 2 +- packages/dartness_server/lib/src/server/dartness_pipeline.dart | 2 +- packages/dartness_server/lib/src/server/dartness_server.dart | 2 +- .../lib/src/server/shelf_middleware/shelf_middleware.dart | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/dartness_server/lib/src/exception/dartness_error_handler_register.dart b/packages/dartness_server/lib/src/exception/dartness_error_handler_register.dart index c59187fb..862c3fb3 100644 --- a/packages/dartness_server/lib/src/exception/dartness_error_handler_register.dart +++ b/packages/dartness_server/lib/src/exception/dartness_error_handler_register.dart @@ -3,7 +3,7 @@ import 'package:shelf/shelf.dart'; import 'dartness_catch_handler.dart'; /// Interface that is used to handle the errors. -abstract class DartnessCatchErrorRegister { +abstract interface class DartnessCatchErrorRegister { /// Adds an error handler to the error handler. void addCatchError(final DartnessCatchError errorHandler); diff --git a/packages/dartness_server/lib/src/exception/http_status_exception.dart b/packages/dartness_server/lib/src/exception/http_status_exception.dart index 731118a1..b5248241 100644 --- a/packages/dartness_server/lib/src/exception/http_status_exception.dart +++ b/packages/dartness_server/lib/src/exception/http_status_exception.dart @@ -1,5 +1,5 @@ /// Exception for HTTP status code. -/// This abstract class is used to create custom exceptions for HTTP status codes. +/// This abstract interface class is used to create custom exceptions for HTTP status codes. /// /// /// Example: diff --git a/packages/dartness_server/lib/src/provider.dart b/packages/dartness_server/lib/src/provider.dart index 997ccbc8..6bdfe41b 100644 --- a/packages/dartness_server/lib/src/provider.dart +++ b/packages/dartness_server/lib/src/provider.dart @@ -1 +1 @@ -abstract class Provider {} +abstract interface class Provider {} diff --git a/packages/dartness_server/lib/src/route/dartness_router.dart b/packages/dartness_server/lib/src/route/dartness_router.dart index a6081db7..fc698567 100644 --- a/packages/dartness_server/lib/src/route/dartness_router.dart +++ b/packages/dartness_server/lib/src/route/dartness_router.dart @@ -3,7 +3,7 @@ import 'package:shelf_router/shelf_router.dart'; import 'controller_route.dart'; /// A router that can be used to handle requests. -abstract class DartnessRouter { +abstract interface class DartnessRouter { Router get router; /// Adds a new [ControllerRoute]. diff --git a/packages/dartness_server/lib/src/route/shelf_route/shelf_handler.dart b/packages/dartness_server/lib/src/route/shelf_route/shelf_handler.dart index 8cd59c43..389bd070 100644 --- a/packages/dartness_server/lib/src/route/shelf_route/shelf_handler.dart +++ b/packages/dartness_server/lib/src/route/shelf_route/shelf_handler.dart @@ -5,6 +5,6 @@ import 'package:shelf_router/shelf_router.dart'; /// /// Since in [Router.add] the handler is a [Function] and not having a logical /// representation by code. -abstract class ShelfHandler { +abstract interface class ShelfHandler { Function get handler; } diff --git a/packages/dartness_server/lib/src/server/dartness_interceptor.dart b/packages/dartness_server/lib/src/server/dartness_interceptor.dart index 8e2cb0a2..eaef2560 100644 --- a/packages/dartness_server/lib/src/server/dartness_interceptor.dart +++ b/packages/dartness_server/lib/src/server/dartness_interceptor.dart @@ -4,7 +4,7 @@ import 'dartness_response.dart'; /// DartnessInterceptor is an interface in order to handle the request before it is executed /// and handle the response after the request is executed. -abstract class DartnessInterceptor implements Provider { +abstract interface class DartnessInterceptor implements Provider { /// This method is called before the request is executed. void onRequest(final DartnessRequest request); diff --git a/packages/dartness_server/lib/src/server/dartness_middleware.dart b/packages/dartness_server/lib/src/server/dartness_middleware.dart index 086298ee..b0860321 100644 --- a/packages/dartness_server/lib/src/server/dartness_middleware.dart +++ b/packages/dartness_server/lib/src/server/dartness_middleware.dart @@ -3,7 +3,7 @@ import 'dartness_request.dart'; /// DartnessInterceptor is an interface in order to handle the request before it is executed /// this can be helpful if you want to log the request or do something else before the request is executed. -abstract class DartnessMiddleware implements Provider { +abstract interface class DartnessMiddleware implements Provider { /// This method is called before the request is executed. void handle(final DartnessRequest request); } diff --git a/packages/dartness_server/lib/src/server/dartness_pipeline.dart b/packages/dartness_server/lib/src/server/dartness_pipeline.dart index 1a25bf0c..bb443c56 100644 --- a/packages/dartness_server/lib/src/server/dartness_pipeline.dart +++ b/packages/dartness_server/lib/src/server/dartness_pipeline.dart @@ -6,7 +6,7 @@ import 'dartness_middleware.dart'; /// A helper that makes it easy to compose a set of [DartnessMiddleware], [DartnessMiddleware] and a /// [Handler]. -abstract class DartnessPipeline { +abstract interface class DartnessPipeline { /// Returns a new [DartnessPipeline] with [dartnessMiddleware] added to the existing set of /// [DartnessMiddleware]. DartnessPipeline addMiddleware(final DartnessMiddleware dartnessMiddleware); diff --git a/packages/dartness_server/lib/src/server/dartness_server.dart b/packages/dartness_server/lib/src/server/dartness_server.dart index 61be0047..1dda6ed5 100644 --- a/packages/dartness_server/lib/src/server/dartness_server.dart +++ b/packages/dartness_server/lib/src/server/dartness_server.dart @@ -6,7 +6,7 @@ import 'dartness_interceptor.dart'; import 'dartness_middleware.dart'; /// An interface that defines the methods that a server must implement. -abstract class DartnessServer { +abstract interface class DartnessServer { /// Starts the server. Future start(); diff --git a/packages/dartness_server/lib/src/server/shelf_middleware/shelf_middleware.dart b/packages/dartness_server/lib/src/server/shelf_middleware/shelf_middleware.dart index b7a530d2..070b404b 100644 --- a/packages/dartness_server/lib/src/server/shelf_middleware/shelf_middleware.dart +++ b/packages/dartness_server/lib/src/server/shelf_middleware/shelf_middleware.dart @@ -2,6 +2,6 @@ import 'package:shelf/shelf.dart'; /// A representation as an interface of a [Middleware] used by [shelf](https://github.com/dart-lang/shelf) /// to handle requests. -abstract class ShelfMiddleware { +abstract interface class ShelfMiddleware { Middleware get middleware; } From 61847dd2c46404efe1a5561707960a87a061e009 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Tue, 31 Oct 2023 17:33:23 +0100 Subject: [PATCH 04/33] Added name attribute "name" in ProviderMetadata --- examples/dartness_simple/lib/src/app.g.dart | 1 + .../lib/src/controllers/city/city_module.dart | 4 ++ examples/dartness_simple/pubspec.yaml | 6 +- .../lib/src/application_generator.dart | 59 +++++++++++++++---- packages/dartness_generator/pubspec.yaml | 7 ++- .../lib/src/modules/provider_metadata.dart | 13 +++- 6 files changed, 70 insertions(+), 20 deletions(-) diff --git a/examples/dartness_simple/lib/src/app.g.dart b/examples/dartness_simple/lib/src/app.g.dart index 4f5c5dea..2e6422fe 100644 --- a/examples/dartness_simple/lib/src/app.g.dart +++ b/examples/dartness_simple/lib/src/app.g.dart @@ -11,6 +11,7 @@ extension AppExtension on App { final injectRegister = InstanceRegister.instance; injectRegister.register(UserController()); injectRegister.register(CityService()); + injectRegister.register(CityService(), name: "CITY_SECOND"); injectRegister.register(CityController( injectRegister.resolve(), )); diff --git a/examples/dartness_simple/lib/src/controllers/city/city_module.dart b/examples/dartness_simple/lib/src/controllers/city/city_module.dart index 7db0e2cd..609460f4 100644 --- a/examples/dartness_simple/lib/src/controllers/city/city_module.dart +++ b/examples/dartness_simple/lib/src/controllers/city/city_module.dart @@ -14,6 +14,10 @@ const cityModule = Module( ProviderMetadata( classType: CityService, ), + ProviderMetadata( + classType: CityService, + name: 'CITY_SECOND', + ), ], ), ); diff --git a/examples/dartness_simple/pubspec.yaml b/examples/dartness_simple/pubspec.yaml index a3b830bd..43784980 100644 --- a/examples/dartness_simple/pubspec.yaml +++ b/examples/dartness_simple/pubspec.yaml @@ -9,10 +9,12 @@ environment: sdk: ">=3.0.0 <4.0.0" dependencies: - dartness_server: 0.5.0-alpha + dartness_server: + path: ../../packages/dartness_server dev_dependencies: lints: ^2.1.1 test: ^1.24.7 build_runner: ^2.4.6 - dartness_generator: 0.5.0-alpha + dartness_generator: + path: ../../packages/dartness_generator diff --git a/packages/dartness_generator/lib/src/application_generator.dart b/packages/dartness_generator/lib/src/application_generator.dart index 61cebd5e..444b5375 100644 --- a/packages/dartness_generator/lib/src/application_generator.dart +++ b/packages/dartness_generator/lib/src/application_generator.dart @@ -68,23 +68,55 @@ class ApplicationGenerator extends GeneratorForAnnotation { 'to create an instance of the class'); } - buffer.writeln('injectRegister.register<${providerElement.name}>('); - buffer.writeln('${providerElement.name}('); - - for (final constructorParam in constructor.parameters) { - buffer.writeln( - 'injectRegister.resolve<${constructorParam.type.getDisplayString( - withNullability: false, - )}>(),'); + _registerClass(buffer, providerElement); + + final Iterable allClassTypeProviders = + allInstances.where((element) { + final instanceElement = + element.getField('classType')?.toTypeValue()?.element; + return instanceElement == providerElement; + }).where((element) => element.getField('name')?.toStringValue() != null); + + if (allClassTypeProviders.isNotEmpty) { + for (final nameTypeProvider in allClassTypeProviders) { + final nameField = nameTypeProvider.getField('name'); + _registerClass( + buffer, + providerElement, + name: nameField?.toStringValue(), + ); + } } - - buffer.writeln('));'); } // initDependencies method end buffer.writeln('}'); } + void _registerClass( + final StringBuffer buffer, + final ClassElement providerElement, { + final String? name, + }) { + final constructors = providerElement.constructors; + final constructor = constructors.first; + buffer.writeln('injectRegister.register<${providerElement.name}>('); + buffer.writeln('${providerElement.name}('); + + for (final constructorParam in constructor.parameters) { + final String className = constructorParam.type.getDisplayString( + withNullability: false, + ); + buffer.writeln('injectRegister.resolve<$className>(),'); + } + + if (name != null && name.isNotEmpty) { + buffer.writeln('), name: "$name");'); + } else { + buffer.writeln('));'); + } + } + /// Obtains all the controllers from [moduleMetadata] List _getAllControllers(final DartObject? moduleMetadata) { return _getAllFieldFromModuleMetadata(moduleMetadata, 'controllers'); @@ -131,8 +163,11 @@ class ApplicationGenerator extends GeneratorForAnnotation { /// Marks the [dependency] as visited in [visited] set and add it also in /// [sorted] list - void _visit(final ClassElement dependency, final Set visited, - final List sorted) { + void _visit( + final ClassElement dependency, + final Set visited, + final List sorted, + ) { if (visited.contains(dependency)) { return; } diff --git a/packages/dartness_generator/pubspec.yaml b/packages/dartness_generator/pubspec.yaml index cc6a7904..e196b523 100644 --- a/packages/dartness_generator/pubspec.yaml +++ b/packages/dartness_generator/pubspec.yaml @@ -16,10 +16,11 @@ dependencies: analyzer: ^6.3.0 code_builder: ^4.7.0 graphs: ^2.3.1 - dartness_server: ^0.5.0-alpha + dartness_server: + path: ../dartness_server dev_dependencies: - lints: ^2.1.1 + lints: ^3.0.0 build_runner: ^2.4.6 - test: ^1.24.7 + test: ^1.24.9 source_gen_test: ^1.0.6 diff --git a/packages/dartness_server/lib/src/modules/provider_metadata.dart b/packages/dartness_server/lib/src/modules/provider_metadata.dart index 13cc74d9..486b2796 100644 --- a/packages/dartness_server/lib/src/modules/provider_metadata.dart +++ b/packages/dartness_server/lib/src/modules/provider_metadata.dart @@ -1,10 +1,17 @@ +/// Define a Dart class called ProviderMetadata. class ProviderMetadata { - // It will be available in the next version - // final String? name; + /// The class type that must be created final Type classType; + /// Optional name to give to the provider instance + final String? name; + + /// [Function] to create the provider by custom code + final Function? useFactory; + const ProviderMetadata({ - // this.name, required this.classType, + this.name, + this.useFactory, }); } From 3003bcfdc1a0b16a0f080068a0724f103448ba5b Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Tue, 31 Oct 2023 17:33:47 +0100 Subject: [PATCH 05/33] Fixed application_generator.dart --- .../lib/src/application_generator.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/dartness_generator/lib/src/application_generator.dart b/packages/dartness_generator/lib/src/application_generator.dart index 444b5375..d5d90988 100644 --- a/packages/dartness_generator/lib/src/application_generator.dart +++ b/packages/dartness_generator/lib/src/application_generator.dart @@ -72,10 +72,13 @@ class ApplicationGenerator extends GeneratorForAnnotation { final Iterable allClassTypeProviders = allInstances.where((element) { - final instanceElement = - element.getField('classType')?.toTypeValue()?.element; + final classType = element.getField('classType'); + final instanceElement = classType?.toTypeValue()?.element; return instanceElement == providerElement; - }).where((element) => element.getField('name')?.toStringValue() != null); + }).where((element) { + final name = element.getField('name')?.toStringValue(); + return name != null; + }); if (allClassTypeProviders.isNotEmpty) { for (final nameTypeProvider in allClassTypeProviders) { From 1c4eba0389ec1bbf143590aadc3201385f4777bb Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Tue, 31 Oct 2023 17:34:08 +0100 Subject: [PATCH 06/33] Removed == in ProviderMetadata --- .../lib/src/modules/provider_metadata.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/dartness_server/lib/src/modules/provider_metadata.dart b/packages/dartness_server/lib/src/modules/provider_metadata.dart index 486b2796..40201daf 100644 --- a/packages/dartness_server/lib/src/modules/provider_metadata.dart +++ b/packages/dartness_server/lib/src/modules/provider_metadata.dart @@ -14,4 +14,16 @@ class ProviderMetadata { this.name, this.useFactory, }); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is ProviderMetadata && + runtimeType == other.runtimeType && + classType == other.classType && + name == other.name && + useFactory == other.useFactory; + + @override + int get hashCode => classType.hashCode ^ name.hashCode ^ useFactory.hashCode; } From 15daa2eccab73b813be93dd4338537d403a1ac1f Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Tue, 31 Oct 2023 17:38:18 +0100 Subject: [PATCH 07/33] Improved simple example structure --- examples/dartness_simple/lib/app_module.dart | 20 ------------------- examples/dartness_simple/lib/src/app.dart | 6 ++---- .../{city => }/city_controller.dart | 4 ++-- .../{city => }/city_controller.g.dart | 0 .../{user => }/user_controller.dart | 0 .../{user => }/user_controller.g.dart | 0 .../city => modules}/city_module.dart | 4 ++-- .../user => modules}/user_module.dart | 2 +- 8 files changed, 7 insertions(+), 29 deletions(-) delete mode 100644 examples/dartness_simple/lib/app_module.dart rename examples/dartness_simple/lib/src/controllers/{city => }/city_controller.dart (86%) rename examples/dartness_simple/lib/src/controllers/{city => }/city_controller.g.dart (100%) rename examples/dartness_simple/lib/src/controllers/{user => }/user_controller.dart (100%) rename examples/dartness_simple/lib/src/controllers/{user => }/user_controller.g.dart (100%) rename examples/dartness_simple/lib/src/{controllers/city => modules}/city_module.dart (81%) rename examples/dartness_simple/lib/src/{controllers/user => modules}/user_module.dart (81%) diff --git a/examples/dartness_simple/lib/app_module.dart b/examples/dartness_simple/lib/app_module.dart deleted file mode 100644 index 5b001e87..00000000 --- a/examples/dartness_simple/lib/app_module.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'package:dartness_server/modules.dart'; -import 'package:example/src/services/city_service.dart'; - -import 'src/controllers/city/city_controller.dart'; - -@Module( - metadata: ModuleMetadata( - controllers: [ - ProviderMetadata( - classType: CityController, - ), - ], - providers: [ - ProviderMetadata( - classType: CityService, - ), - ], - ), -) -class AppModule {} diff --git a/examples/dartness_simple/lib/src/app.dart b/examples/dartness_simple/lib/src/app.dart index 3caf66fc..9b7c9b90 100644 --- a/examples/dartness_simple/lib/src/app.dart +++ b/examples/dartness_simple/lib/src/app.dart @@ -1,14 +1,12 @@ import 'package:dartness_server/dartness.dart'; import 'package:dartness_server/modules.dart'; import 'package:dartness_server/server.dart'; -import 'package:example/src/controllers/city/city_module.dart'; -import 'package:example/src/controllers/user/user_module.dart'; import 'package:example/src/services/city_service.dart'; -import 'controllers/city/city_controller.dart'; import 'controllers/health_controller.dart'; -import 'controllers/user/user_controller.dart'; import 'error_handlers/example_error_handler.dart'; +import 'modules/city_module.dart'; +import 'modules/user_module.dart'; part 'app.g.dart'; diff --git a/examples/dartness_simple/lib/src/controllers/city/city_controller.dart b/examples/dartness_simple/lib/src/controllers/city_controller.dart similarity index 86% rename from examples/dartness_simple/lib/src/controllers/city/city_controller.dart rename to examples/dartness_simple/lib/src/controllers/city_controller.dart index ca6f03ab..bde2698a 100644 --- a/examples/dartness_simple/lib/src/controllers/city/city_controller.dart +++ b/examples/dartness_simple/lib/src/controllers/city_controller.dart @@ -2,8 +2,8 @@ import 'dart:io'; import 'package:dartness_server/route.dart'; -import '../../dtos/city_dto.dart'; -import '../../services/city_service.dart'; +import '../dtos/city_dto.dart'; +import '../services/city_service.dart'; part 'city_controller.g.dart'; diff --git a/examples/dartness_simple/lib/src/controllers/city/city_controller.g.dart b/examples/dartness_simple/lib/src/controllers/city_controller.g.dart similarity index 100% rename from examples/dartness_simple/lib/src/controllers/city/city_controller.g.dart rename to examples/dartness_simple/lib/src/controllers/city_controller.g.dart diff --git a/examples/dartness_simple/lib/src/controllers/user/user_controller.dart b/examples/dartness_simple/lib/src/controllers/user_controller.dart similarity index 100% rename from examples/dartness_simple/lib/src/controllers/user/user_controller.dart rename to examples/dartness_simple/lib/src/controllers/user_controller.dart diff --git a/examples/dartness_simple/lib/src/controllers/user/user_controller.g.dart b/examples/dartness_simple/lib/src/controllers/user_controller.g.dart similarity index 100% rename from examples/dartness_simple/lib/src/controllers/user/user_controller.g.dart rename to examples/dartness_simple/lib/src/controllers/user_controller.g.dart diff --git a/examples/dartness_simple/lib/src/controllers/city/city_module.dart b/examples/dartness_simple/lib/src/modules/city_module.dart similarity index 81% rename from examples/dartness_simple/lib/src/controllers/city/city_module.dart rename to examples/dartness_simple/lib/src/modules/city_module.dart index 609460f4..4942a54f 100644 --- a/examples/dartness_simple/lib/src/controllers/city/city_module.dart +++ b/examples/dartness_simple/lib/src/modules/city_module.dart @@ -1,7 +1,7 @@ import 'package:dartness_server/modules.dart'; -import '../../services/city_service.dart'; -import 'city_controller.dart'; +import '../controllers/city_controller.dart'; +import '../services/city_service.dart'; const cityModule = Module( metadata: ModuleMetadata( diff --git a/examples/dartness_simple/lib/src/controllers/user/user_module.dart b/examples/dartness_simple/lib/src/modules/user_module.dart similarity index 81% rename from examples/dartness_simple/lib/src/controllers/user/user_module.dart rename to examples/dartness_simple/lib/src/modules/user_module.dart index 55d5dbd4..de22457f 100644 --- a/examples/dartness_simple/lib/src/controllers/user/user_module.dart +++ b/examples/dartness_simple/lib/src/modules/user_module.dart @@ -1,6 +1,6 @@ import 'package:dartness_server/modules.dart'; -import '../user/user_controller.dart'; +import '../controllers/user_controller.dart'; const userModule = Module( metadata: ModuleMetadata( From 8a4d13a0f31a3501a21c25e8f2c729784e8e4187 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Tue, 31 Oct 2023 17:38:46 +0100 Subject: [PATCH 08/33] Fixed simple example --- examples/dartness_simple/lib/src/app.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/dartness_simple/lib/src/app.dart b/examples/dartness_simple/lib/src/app.dart index 9b7c9b90..483f62b2 100644 --- a/examples/dartness_simple/lib/src/app.dart +++ b/examples/dartness_simple/lib/src/app.dart @@ -3,7 +3,9 @@ import 'package:dartness_server/modules.dart'; import 'package:dartness_server/server.dart'; import 'package:example/src/services/city_service.dart'; +import 'controllers/city_controller.dart'; import 'controllers/health_controller.dart'; +import 'controllers/user_controller.dart'; import 'error_handlers/example_error_handler.dart'; import 'modules/city_module.dart'; import 'modules/user_module.dart'; From f2f3e3a44bc38a4fcf3a0664aae2f8fe28994148 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 12:50:43 +0100 Subject: [PATCH 09/33] Added 'useFactory' generation code --- .../lib/src/application_generator.dart | 85 ++++++++++++++++--- 1 file changed, 71 insertions(+), 14 deletions(-) diff --git a/packages/dartness_generator/lib/src/application_generator.dart b/packages/dartness_generator/lib/src/application_generator.dart index d5d90988..c57ce9d3 100644 --- a/packages/dartness_generator/lib/src/application_generator.dart +++ b/packages/dartness_generator/lib/src/application_generator.dart @@ -1,6 +1,7 @@ import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:build/build.dart'; +import 'package:collection/collection.dart'; import 'package:dartness_server/dartness.dart'; import 'package:source_gen/source_gen.dart'; @@ -51,7 +52,8 @@ class ApplicationGenerator extends GeneratorForAnnotation { .whereType() .toList(); - final topologicalProviderElements = _topologicalSort(allProviderElements); + final topologicalProviderElements = + _topologicalSort(allProviderElements, allInstances); for (final providerElement in topologicalProviderElements) { final constructors = providerElement.constructors; @@ -68,8 +70,37 @@ class ApplicationGenerator extends GeneratorForAnnotation { 'to create an instance of the class'); } - _registerClass(buffer, providerElement); - + final providerObject = allInstances.firstWhere((element) => + element.getField('classType')?.toTypeValue()?.element == + providerElement); + + final useFactory = providerObject.getField('useFactory'); + final hasUseFactory = useFactory?.isNull == false; + if (hasUseFactory) { + final useFactoryFunc = useFactory?.toFunctionValue(); + if (useFactoryFunc is FunctionElement) { + final variableResult = '${useFactoryFunc.name}Result'; + buffer.writeln('final $variableResult = '); + if (useFactoryFunc.isAsynchronous) { + buffer.writeln('await ${useFactoryFunc.name}.call('); + } else { + buffer.writeln('${useFactoryFunc.name}.call('); + } + final resolves = useFactoryFunc.parameters.map((param) { + final String className = param.type.getDisplayString( + withNullability: false, + ); + buffer.writeln('injectRegister.resolve<$className>()'); + }).join(', '); + buffer.write(resolves); + buffer.writeln(');'); + + buffer.writeln( + 'injectRegister.register<${providerElement.name}>($variableResult);'); + } + } else { + _registerClass(buffer, providerElement); + } final Iterable allClassTypeProviders = allInstances.where((element) { final classType = element.getField('classType'); @@ -153,15 +184,18 @@ class ApplicationGenerator extends GeneratorForAnnotation { /// Sort the dependencies by a topological sort to add the dependencies /// for each class that requires as inversion control - List _topologicalSort(final List dependencies) { + List _topologicalSort( + final List dependencies, + final List objects, + ) { final visited = {}; final sorted = []; for (final dependency in dependencies) { - _visit(dependency, visited, sorted); + _visit(dependency, visited, sorted, objects); } - return sorted.toList(); + return sorted; } /// Marks the [dependency] as visited in [visited] set and add it also in @@ -170,6 +204,7 @@ class ApplicationGenerator extends GeneratorForAnnotation { final ClassElement dependency, final Set visited, final List sorted, + final List allDependencies, ) { if (visited.contains(dependency)) { return; @@ -177,28 +212,49 @@ class ApplicationGenerator extends GeneratorForAnnotation { visited.add(dependency); - final dependencies = _getDependencies(dependency); + final dependencies = _getDependencies(dependency, allDependencies); for (final dep in dependencies) { - _visit(dep, visited, sorted); + _visit(dep, visited, sorted, allDependencies); } sorted.add(dependency); } /// Obtains the dependencies of a class by his constructor - List _getDependencies(final ClassElement clazz) { + List _getDependencies( + final ClassElement clazz, + final List allDependencies, + ) { final dependencies = []; - - final constructor = clazz.unnamedConstructor; - if (constructor != null) { - final parameters = constructor.parameters; - for (final param in parameters) { + final foundObject = allDependencies.firstWhereOrNull((element) => + element.getField('classType')?.toTypeValue()?.element == clazz); + if (foundObject == null) { + throw Exception('${clazz.name} not registered as dependency'); + } + final useFeatureField = foundObject.getField('useFeature'); + final hasUseFeature = useFeatureField?.isNull ?? false; + if (hasUseFeature) { + final useFeatureFunction = useFeatureField?.toFunctionValue(); + final params = useFeatureFunction?.parameters ?? []; + for (final param in params) { final paramElement = param.type.element; if (paramElement is ClassElement) { dependencies.add(paramElement); } } + } else { + final constructor = + clazz.constructors.firstWhereOrNull((element) => !element.isFactory); + if (constructor != null) { + final parameters = constructor.parameters; + for (final param in parameters) { + final paramElement = param.type.element; + if (paramElement is ClassElement) { + dependencies.add(paramElement); + } + } + } } return dependencies; @@ -218,6 +274,7 @@ class ApplicationGenerator extends GeneratorForAnnotation { final allControllers = _getAllControllers(rootModuleMetadata); final controllerElements = allControllers + .where((e) => e.getField('useFactory')?.isNull == true) .map((e) => e.getField('classType')) .map((e) => e?.toTypeValue()?.element) .whereType() From 1510a85754e34fbdbb62528d2e3ad189725165a5 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 12:51:06 +0100 Subject: [PATCH 10/33] Created TodoModule to show how to use 'useFactory' --- examples/dartness_simple/lib/src/app.dart | 19 ++++++++++++------- examples/dartness_simple/lib/src/app.g.dart | 11 +++++++++-- .../src/controllers/health_controller.dart | 15 --------------- .../lib/src/controllers/todos_controller.dart | 19 +++++++++++++++++++ ...troller.g.dart => todos_controller.g.dart} | 12 ++++++------ .../lib/src/modules/todos_module.dart | 19 +++++++++++++++++++ .../lib/src/services/city_service.dart | 1 + .../lib/src/services/todos_service.dart | 12 ++++++++++++ examples/dartness_simple/pubspec.yaml | 5 +++-- 9 files changed, 81 insertions(+), 32 deletions(-) delete mode 100644 examples/dartness_simple/lib/src/controllers/health_controller.dart create mode 100644 examples/dartness_simple/lib/src/controllers/todos_controller.dart rename examples/dartness_simple/lib/src/controllers/{health_controller.g.dart => todos_controller.g.dart} (69%) create mode 100644 examples/dartness_simple/lib/src/modules/todos_module.dart create mode 100644 examples/dartness_simple/lib/src/services/todos_service.dart diff --git a/examples/dartness_simple/lib/src/app.dart b/examples/dartness_simple/lib/src/app.dart index 483f62b2..49e3ab49 100644 --- a/examples/dartness_simple/lib/src/app.dart +++ b/examples/dartness_simple/lib/src/app.dart @@ -1,34 +1,39 @@ import 'package:dartness_server/dartness.dart'; import 'package:dartness_server/modules.dart'; import 'package:dartness_server/server.dart'; -import 'package:example/src/services/city_service.dart'; +import 'package:dio/dio.dart'; import 'controllers/city_controller.dart'; -import 'controllers/health_controller.dart'; +import 'controllers/todos_controller.dart'; import 'controllers/user_controller.dart'; import 'error_handlers/example_error_handler.dart'; import 'modules/city_module.dart'; +import 'modules/todos_module.dart'; import 'modules/user_module.dart'; +import 'services/city_service.dart'; +import 'services/todos_service.dart'; part 'app.g.dart'; +Dio createDio() => Dio(); + @Application( module: Module( metadata: ModuleMetadata( - controllers: [ - ProviderMetadata( - classType: HealthController, - ), - ], providers: [ ProviderMetadata( classType: ExampleErrorHandler, ), + ProviderMetadata( + classType: Dio, + useFactory: createDio, + ), ], exports: [], imports: [ userModule, cityModule, + todosModule, ], ), ), diff --git a/examples/dartness_simple/lib/src/app.g.dart b/examples/dartness_simple/lib/src/app.g.dart index 2e6422fe..b279da10 100644 --- a/examples/dartness_simple/lib/src/app.g.dart +++ b/examples/dartness_simple/lib/src/app.g.dart @@ -15,7 +15,14 @@ extension AppExtension on App { injectRegister.register(CityController( injectRegister.resolve(), )); - injectRegister.register(HealthController()); + final createDioResult = createDio.call(); + injectRegister.register(createDioResult); + injectRegister.register(TodosService( + injectRegister.resolve(), + )); + injectRegister.register(TodosController( + injectRegister.resolve(), + )); injectRegister.register(ExampleErrorHandler()); } @@ -27,7 +34,7 @@ extension AppExtension on App { controllers: [ UserDartnessController(injectRegister.resolve()), CityDartnessController(injectRegister.resolve()), - HealthDartnessController(injectRegister.resolve()), + TodosDartnessController(injectRegister.resolve()), ], options: DartnessApplicationOptions( logRequest: false, diff --git a/examples/dartness_simple/lib/src/controllers/health_controller.dart b/examples/dartness_simple/lib/src/controllers/health_controller.dart deleted file mode 100644 index 5fd59739..00000000 --- a/examples/dartness_simple/lib/src/controllers/health_controller.dart +++ /dev/null @@ -1,15 +0,0 @@ -import 'dart:io'; - -import 'package:dartness_server/route.dart'; - -part 'health_controller.g.dart'; - -@Controller('/health') -@Header(HttpHeaders.contentTypeHeader, 'application/json') -class HealthController { - @HttpCode(202) - @Get() - bool getAlive() { - return true; - } -} diff --git a/examples/dartness_simple/lib/src/controllers/todos_controller.dart b/examples/dartness_simple/lib/src/controllers/todos_controller.dart new file mode 100644 index 00000000..347f26d5 --- /dev/null +++ b/examples/dartness_simple/lib/src/controllers/todos_controller.dart @@ -0,0 +1,19 @@ +import 'dart:io'; + +import 'package:dartness_server/route.dart'; +import 'package:example/src/services/todos_service.dart'; + +part 'todos_controller.g.dart'; + +@Controller('/todos') +@Header(HttpHeaders.contentTypeHeader, 'application/json') +class TodosController { + final TodosService _todoService; + + TodosController(this._todoService); + + @Get() + Future> getAlive() { + return _todoService.getTodos(); + } +} diff --git a/examples/dartness_simple/lib/src/controllers/health_controller.g.dart b/examples/dartness_simple/lib/src/controllers/todos_controller.g.dart similarity index 69% rename from examples/dartness_simple/lib/src/controllers/health_controller.g.dart rename to examples/dartness_simple/lib/src/controllers/todos_controller.g.dart index 26528a21..e1161f16 100644 --- a/examples/dartness_simple/lib/src/controllers/health_controller.g.dart +++ b/examples/dartness_simple/lib/src/controllers/todos_controller.g.dart @@ -1,28 +1,28 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'health_controller.dart'; +part of 'todos_controller.dart'; // ************************************************************************** // ControllerGenerator // ************************************************************************** -extension HealthControllerRoutes on HealthController { +extension TodosControllerRoutes on TodosController { List getRoutes() { final routes = []; routes.add(ControllerRoute( method: 'GET', - path: '/health', + path: '/todos', handler: getAlive, params: [], - httpCode: 202, + httpCode: null, headers: {'content-type': 'application/json'}, )); return routes; } } -class HealthDartnessController extends DartnessController { - HealthDartnessController(HealthController controller) +class TodosDartnessController extends DartnessController { + TodosDartnessController(TodosController controller) : super( controller, controller.getRoutes(), diff --git a/examples/dartness_simple/lib/src/modules/todos_module.dart b/examples/dartness_simple/lib/src/modules/todos_module.dart new file mode 100644 index 00000000..df7e68f4 --- /dev/null +++ b/examples/dartness_simple/lib/src/modules/todos_module.dart @@ -0,0 +1,19 @@ +import 'package:dartness_server/modules.dart'; + +import '../controllers/todos_controller.dart'; +import '../services/todos_service.dart'; + +const todosModule = Module( + metadata: ModuleMetadata( + controllers: [ + ProviderMetadata( + classType: TodosController, + ), + ], + providers: [ + ProviderMetadata( + classType: TodosService, + ), + ], + ), +); diff --git a/examples/dartness_simple/lib/src/services/city_service.dart b/examples/dartness_simple/lib/src/services/city_service.dart index 997c0051..083720de 100644 --- a/examples/dartness_simple/lib/src/services/city_service.dart +++ b/examples/dartness_simple/lib/src/services/city_service.dart @@ -1,3 +1,4 @@ + import '../dtos/city_dto.dart'; import '../error_handlers/not_found_exception.dart'; diff --git a/examples/dartness_simple/lib/src/services/todos_service.dart b/examples/dartness_simple/lib/src/services/todos_service.dart new file mode 100644 index 00000000..60cf2ff0 --- /dev/null +++ b/examples/dartness_simple/lib/src/services/todos_service.dart @@ -0,0 +1,12 @@ +import 'package:dio/dio.dart'; + +class TodosService { + final Dio _dio; + + TodosService(this._dio); + + Future> getTodos() async { + final result = await _dio.get('https://dummyjson.com/todos/user/5'); + return result.data as Map; + } +} diff --git a/examples/dartness_simple/pubspec.yaml b/examples/dartness_simple/pubspec.yaml index 43784980..56de3f5d 100644 --- a/examples/dartness_simple/pubspec.yaml +++ b/examples/dartness_simple/pubspec.yaml @@ -11,10 +11,11 @@ environment: dependencies: dartness_server: path: ../../packages/dartness_server + dio: ^5.3.3 dev_dependencies: - lints: ^2.1.1 - test: ^1.24.7 + lints: ^3.0.0 + test: ^1.24.9 build_runner: ^2.4.6 dartness_generator: path: ../../packages/dartness_generator From ee1c616d41fe5ee817f057910436a215fcd9a0db Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 12:51:24 +0100 Subject: [PATCH 11/33] Removed equals and hashCode in ProviderMetadata --- .../lib/src/modules/provider_metadata.dart | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/packages/dartness_server/lib/src/modules/provider_metadata.dart b/packages/dartness_server/lib/src/modules/provider_metadata.dart index 40201daf..2ba029bc 100644 --- a/packages/dartness_server/lib/src/modules/provider_metadata.dart +++ b/packages/dartness_server/lib/src/modules/provider_metadata.dart @@ -1,7 +1,7 @@ /// Define a Dart class called ProviderMetadata. -class ProviderMetadata { +class ProviderMetadata { /// The class type that must be created - final Type classType; + final T classType; /// Optional name to give to the provider instance final String? name; @@ -14,16 +14,4 @@ class ProviderMetadata { this.name, this.useFactory, }); - - @override - bool operator ==(Object other) => - identical(this, other) || - other is ProviderMetadata && - runtimeType == other.runtimeType && - classType == other.classType && - name == other.name && - useFactory == other.useFactory; - - @override - int get hashCode => classType.hashCode ^ name.hashCode ^ useFactory.hashCode; } From 077dc4f70c584726dd4b3a64ac19ae820d26040a Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 15:16:08 +0100 Subject: [PATCH 12/33] Changed .call to Function.apply --- examples/dartness_simple/lib/src/app.g.dart | 2 +- .../lib/src/application_generator.dart | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/dartness_simple/lib/src/app.g.dart b/examples/dartness_simple/lib/src/app.g.dart index b279da10..62d3dc40 100644 --- a/examples/dartness_simple/lib/src/app.g.dart +++ b/examples/dartness_simple/lib/src/app.g.dart @@ -15,7 +15,7 @@ extension AppExtension on App { injectRegister.register(CityController( injectRegister.resolve(), )); - final createDioResult = createDio.call(); + final createDioResult = Function.apply(createDio, []); injectRegister.register(createDioResult); injectRegister.register(TodosService( injectRegister.resolve(), diff --git a/packages/dartness_generator/lib/src/application_generator.dart b/packages/dartness_generator/lib/src/application_generator.dart index c57ce9d3..b096db13 100644 --- a/packages/dartness_generator/lib/src/application_generator.dart +++ b/packages/dartness_generator/lib/src/application_generator.dart @@ -82,9 +82,9 @@ class ApplicationGenerator extends GeneratorForAnnotation { final variableResult = '${useFactoryFunc.name}Result'; buffer.writeln('final $variableResult = '); if (useFactoryFunc.isAsynchronous) { - buffer.writeln('await ${useFactoryFunc.name}.call('); + buffer.writeln('await Function.apply(${useFactoryFunc.name},'); } else { - buffer.writeln('${useFactoryFunc.name}.call('); + buffer.writeln('Function.apply(${useFactoryFunc.name},'); } final resolves = useFactoryFunc.parameters.map((param) { final String className = param.type.getDisplayString( @@ -92,7 +92,11 @@ class ApplicationGenerator extends GeneratorForAnnotation { ); buffer.writeln('injectRegister.resolve<$className>()'); }).join(', '); - buffer.write(resolves); + if (resolves.isEmpty) { + buffer.write('[]'); + } else { + buffer.write(resolves); + } buffer.writeln(');'); buffer.writeln( From 35f3e8c318ae7e09db8dd17f70d3f793800bfddf Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 15:38:39 +0100 Subject: [PATCH 13/33] Created inject.dart --- .../dartness_server/lib/src/app/annotation/inject.dart | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 packages/dartness_server/lib/src/app/annotation/inject.dart diff --git a/packages/dartness_server/lib/src/app/annotation/inject.dart b/packages/dartness_server/lib/src/app/annotation/inject.dart new file mode 100644 index 00000000..4cd5b4f6 --- /dev/null +++ b/packages/dartness_server/lib/src/app/annotation/inject.dart @@ -0,0 +1,8 @@ +/// Annotation that marks a constructor parameter as a target for +/// dependence injection +abstract class Inject { + /// Lookup key for the provider to be injected + final String? name; + + const Inject(this.name); +} From 4415e68d14853e202f558a2ed61675c11ae16258 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 16:32:14 +0100 Subject: [PATCH 14/33] inject.dart name as optional --- packages/dartness_server/lib/src/app/annotation/inject.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dartness_server/lib/src/app/annotation/inject.dart b/packages/dartness_server/lib/src/app/annotation/inject.dart index 4cd5b4f6..ce84b4db 100644 --- a/packages/dartness_server/lib/src/app/annotation/inject.dart +++ b/packages/dartness_server/lib/src/app/annotation/inject.dart @@ -4,5 +4,5 @@ abstract class Inject { /// Lookup key for the provider to be injected final String? name; - const Inject(this.name); + const Inject([this.name]); } From 0211dbeb9bc6fa88e725ece18251a4493b4ae477 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:22:49 +0100 Subject: [PATCH 15/33] inject.dart should not be abstract to be an annotation --- packages/dartness_server/lib/src/app/annotation/inject.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dartness_server/lib/src/app/annotation/inject.dart b/packages/dartness_server/lib/src/app/annotation/inject.dart index ce84b4db..f1a0116f 100644 --- a/packages/dartness_server/lib/src/app/annotation/inject.dart +++ b/packages/dartness_server/lib/src/app/annotation/inject.dart @@ -1,6 +1,6 @@ /// Annotation that marks a constructor parameter as a target for /// dependence injection -abstract class Inject { +class Inject { /// Lookup key for the provider to be injected final String? name; From 09f045cd859f50faa6c1b7903b9249ffe61dcc4b Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:23:25 +0100 Subject: [PATCH 16/33] Export inject.dart --- packages/dartness_server/lib/dartness.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/dartness_server/lib/dartness.dart b/packages/dartness_server/lib/dartness.dart index dc1bafab..801a6265 100644 --- a/packages/dartness_server/lib/dartness.dart +++ b/packages/dartness_server/lib/dartness.dart @@ -3,6 +3,7 @@ /// See documentation guides at https://ricardorb.github.io/dartness/#/. library dartness; +export 'src/app/annotation/inject.dart'; export 'src/app/application.dart'; export 'src/app/instance_register.dart'; export 'src/dartness.dart'; From d0a8e1a2ad86357e4ff172fe7202b1245ecbd912 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:23:53 +0100 Subject: [PATCH 17/33] Generated code with inject annotation --- .../lib/src/application_generator.dart | 85 +++++++++++++------ 1 file changed, 57 insertions(+), 28 deletions(-) diff --git a/packages/dartness_generator/lib/src/application_generator.dart b/packages/dartness_generator/lib/src/application_generator.dart index b096db13..f574c129 100644 --- a/packages/dartness_generator/lib/src/application_generator.dart +++ b/packages/dartness_generator/lib/src/application_generator.dart @@ -7,6 +7,7 @@ import 'package:source_gen/source_gen.dart'; class ApplicationGenerator extends GeneratorForAnnotation { static final _applicationType = TypeChecker.fromRuntime(Application); + static final _injectType = TypeChecker.fromRuntime(Inject); @override String? generateForAnnotatedElement( @@ -77,31 +78,7 @@ class ApplicationGenerator extends GeneratorForAnnotation { final useFactory = providerObject.getField('useFactory'); final hasUseFactory = useFactory?.isNull == false; if (hasUseFactory) { - final useFactoryFunc = useFactory?.toFunctionValue(); - if (useFactoryFunc is FunctionElement) { - final variableResult = '${useFactoryFunc.name}Result'; - buffer.writeln('final $variableResult = '); - if (useFactoryFunc.isAsynchronous) { - buffer.writeln('await Function.apply(${useFactoryFunc.name},'); - } else { - buffer.writeln('Function.apply(${useFactoryFunc.name},'); - } - final resolves = useFactoryFunc.parameters.map((param) { - final String className = param.type.getDisplayString( - withNullability: false, - ); - buffer.writeln('injectRegister.resolve<$className>()'); - }).join(', '); - if (resolves.isEmpty) { - buffer.write('[]'); - } else { - buffer.write(resolves); - } - buffer.writeln(');'); - - buffer.writeln( - 'injectRegister.register<${providerElement.name}>($variableResult);'); - } + _registerFactory(useFactory, buffer, providerElement); } else { _registerClass(buffer, providerElement); } @@ -131,6 +108,47 @@ class ApplicationGenerator extends GeneratorForAnnotation { buffer.writeln('}'); } + void _registerFactory( + final DartObject? useFactory, + final StringBuffer buffer, + final ClassElement providerElement, + ) { + final useFactoryFunc = useFactory?.toFunctionValue(); + if (useFactoryFunc is FunctionElement) { + final variableResult = '${useFactoryFunc.name}Result'; + buffer.writeln('final $variableResult = '); + if (useFactoryFunc.isAsynchronous) { + buffer.writeln('await Function.apply(${useFactoryFunc.name},'); + } else { + buffer.writeln('Function.apply(${useFactoryFunc.name},'); + } + final resolves = useFactoryFunc.parameters.map((param) { + final String className = param.type.getDisplayString( + withNullability: false, + ); + final inject = param.metadata + .firstWhereOrNull((element) => element.runtimeType == Inject); + final injectName = + inject?.computeConstantValue()?.getField('name')?.toStringValue(); + if (injectName != null && injectName.isNotEmpty) { + buffer.writeln( + "injectRegister.resolve<$className>(name: '$injectName,')"); + } else { + buffer.writeln('injectRegister.resolve<$className>()'); + } + }).join(', '); + if (resolves.isEmpty) { + buffer.write('[]'); + } else { + buffer.write(resolves); + } + buffer.writeln(');'); + + buffer.writeln( + 'injectRegister.register<${providerElement.name}>($variableResult);'); + } + } + void _registerClass( final StringBuffer buffer, final ClassElement providerElement, { @@ -141,13 +159,24 @@ class ApplicationGenerator extends GeneratorForAnnotation { buffer.writeln('injectRegister.register<${providerElement.name}>('); buffer.writeln('${providerElement.name}('); - for (final constructorParam in constructor.parameters) { + final resolves = constructor.parameters.map((constructorParam) { final String className = constructorParam.type.getDisplayString( withNullability: false, ); - buffer.writeln('injectRegister.resolve<$className>(),'); - } + String resolve = 'injectRegister.resolve<$className>('; + + final injectType = _injectType.firstAnnotationOfExact(constructorParam); + final injectName = injectType?.getField('name')?.toStringValue(); + if (injectName != null && injectName.isNotEmpty) { + resolve += 'name: "$injectName",'; + } + resolve += ')'; + return resolve; + }).join(', '); + + buffer.write(resolves); + // Name for register if (name != null && name.isNotEmpty) { buffer.writeln('), name: "$name");'); } else { From fedf135852c013bc8ac0c03392b8c9e264460ddd Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:24:17 +0100 Subject: [PATCH 18/33] Added @Inject in simple example --- examples/dartness_simple/lib/src/app.g.dart | 17 ++++++++--------- .../lib/src/controllers/city_controller.dart | 3 ++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/dartness_simple/lib/src/app.g.dart b/examples/dartness_simple/lib/src/app.g.dart index 62d3dc40..4a879baf 100644 --- a/examples/dartness_simple/lib/src/app.g.dart +++ b/examples/dartness_simple/lib/src/app.g.dart @@ -12,17 +12,16 @@ extension AppExtension on App { injectRegister.register(UserController()); injectRegister.register(CityService()); injectRegister.register(CityService(), name: "CITY_SECOND"); - injectRegister.register(CityController( - injectRegister.resolve(), - )); + injectRegister.register( + CityController(injectRegister.resolve( + name: "CITY_SECOND", + ))); final createDioResult = Function.apply(createDio, []); injectRegister.register(createDioResult); - injectRegister.register(TodosService( - injectRegister.resolve(), - )); - injectRegister.register(TodosController( - injectRegister.resolve(), - )); + injectRegister + .register(TodosService(injectRegister.resolve())); + injectRegister.register( + TodosController(injectRegister.resolve())); injectRegister.register(ExampleErrorHandler()); } diff --git a/examples/dartness_simple/lib/src/controllers/city_controller.dart b/examples/dartness_simple/lib/src/controllers/city_controller.dart index bde2698a..4fbd7212 100644 --- a/examples/dartness_simple/lib/src/controllers/city_controller.dart +++ b/examples/dartness_simple/lib/src/controllers/city_controller.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:dartness_server/dartness.dart'; import 'package:dartness_server/route.dart'; import '../dtos/city_dto.dart'; @@ -10,7 +11,7 @@ part 'city_controller.g.dart'; @Controller('/cities') @Header(HttpHeaders.contentTypeHeader, 'application/json') class CityController { - CityController(this._cityService); + CityController(@Inject('CITY_SECOND') this._cityService); final CityService _cityService; From 7dd22247490ac5a3bbbaf0649884d115d31920d4 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:28:34 +0100 Subject: [PATCH 19/33] Updated dependencies --- examples/dartness_flutter_melos/apps/my_app/pubspec.yaml | 2 +- .../dartness_flutter_melos/servers/my_server/pubspec.yaml | 8 ++++---- packages/dartness_server/pubspec.yaml | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml b/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml index d580a277..aedb2c16 100644 --- a/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml +++ b/examples/dartness_flutter_melos/apps/my_app/pubspec.yaml @@ -17,7 +17,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^2.0.3 + flutter_lints: ^3.0.0 flutter: uses-material-design: true diff --git a/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml b/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml index 4da1e0e6..8c5c2b4b 100644 --- a/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml +++ b/examples/dartness_flutter_melos/servers/my_server/pubspec.yaml @@ -9,10 +9,10 @@ environment: sdk: ">=3.0.0 <4.0.0" dependencies: - dartness_server: ^0.4.6-alpha + dartness_server: ^0.5.0-alpha dev_dependencies: - lints: ^2.1.1 - test: ^1.24.7 + lints: ^3.0.0 + test: ^1.24.9 build_runner: ^2.4.6 - dartness_generator: ^0.1.3-alpha + dartness_generator: ^0.5.0-alpha diff --git a/packages/dartness_server/pubspec.yaml b/packages/dartness_server/pubspec.yaml index 36addf26..b92fe307 100644 --- a/packages/dartness_server/pubspec.yaml +++ b/packages/dartness_server/pubspec.yaml @@ -23,6 +23,6 @@ dependencies: dev_dependencies: http: ^1.1.0 - lints: ^2.1.1 - test: ^1.24.7 + lints: ^3.0.0 + test: ^1.24.9 build_runner: ^2.4.6 \ No newline at end of file From 140012d57243f5dc641e1c1de69e8c3ca8cd8bb7 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:29:11 +0100 Subject: [PATCH 20/33] - server version: 0.5.1-alpha --- packages/dartness_server/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dartness_server/pubspec.yaml b/packages/dartness_server/pubspec.yaml index b92fe307..081317d3 100644 --- a/packages/dartness_server/pubspec.yaml +++ b/packages/dartness_server/pubspec.yaml @@ -2,7 +2,7 @@ name: dartness_server description: Dartness is a framework for building efficient, scalable dart server-side applications. It provides an easy and quick way to develop modern standalone server. -version: 0.5.0-alpha +version: 0.5.1-alpha homepage: https://ricardorb.github.io/dartness/ repository: https://github.com/RicardoRB/dartness issue_tracker: https://github.com/RicardoRB/dartness/issues From 29fc6a5cf4b7923d49288a0745dfdaaf08c9a577 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:31:05 +0100 Subject: [PATCH 21/33] Added changelog 0.5.1-alpha --- packages/dartness_server/CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/dartness_server/CHANGELOG.md b/packages/dartness_server/CHANGELOG.md index 1ffe3716..87de4212 100644 --- a/packages/dartness_server/CHANGELOG.md +++ b/packages/dartness_server/CHANGELOG.md @@ -37,4 +37,10 @@ ## 0.5.0-alpha -- Easiest way of creating an app by `@Application` annotation and modules \ No newline at end of file +- Easiest way of creating an app by `@Application` annotation and modules + +## 0.5.1-alpha + +- Dependency injection by name +- Custom instance by `useFactory` in `ProviderMetadata` +- Added `@Inject` annotation \ No newline at end of file From 496ba77367c7dae1e2e5eeb3c7fcc1de79137b33 Mon Sep 17 00:00:00 2001 From: RicardoRB Date: Fri, 3 Nov 2023 17:33:31 +0100 Subject: [PATCH 22/33] Updated docs --- .../doc/api/__404error.html | 2 +- .../doc/api/builder/application.html | 2 +- .../doc/api/builder/builder-library.html | 2 +- .../doc/api/builder/controller.html | 2 +- .../doc/api/builder/errorHandler.html | 2 +- .../dartness_generator-library.html | 2 +- .../dartness_generator/doc/api/index.html | 4 +- .../dartness_generator/doc/api/search.html | 2 +- .../dartness_server/doc/api/__404error.html | 2 +- .../doc/api/dartness/Application-class.html | 2 +- .../api/dartness/Application/Application.html | 2 +- .../doc/api/dartness/Application/module.html | 2 +- .../doc/api/dartness/Application/options.html | 2 +- .../doc/api/dartness/Dartness-class.html | 2 +- .../doc/api/dartness/Dartness/Dartness.html | 2 +- .../doc/api/dartness/Dartness/create.html | 2 +- .../api/dartness/Inject-class-sidebar.html | 24 ++ .../doc/api/dartness/Inject-class.html | 231 ++++++++++++++++++ .../doc/api/dartness/Inject/Inject.html | 124 ++++++++++ .../doc/api/dartness/Inject/name.html | 127 ++++++++++ .../api/dartness/InstanceRegister-class.html | 2 +- .../dartness/InstanceRegister/instance.html | 2 +- .../dartness/InstanceRegister/register.html | 2 +- .../dartness/InstanceRegister/resolve.html | 2 +- .../dartness/dartness-library-sidebar.html | 1 + .../doc/api/dartness/dartness-library.html | 11 +- .../doc/api/exception/CatchError-class.html | 2 +- .../api/exception/CatchError/CatchError.html | 2 +- .../doc/api/exception/CatchError/errors.html | 2 +- .../exception/DartnessCatchError-class.html | 2 +- .../DartnessCatchError.html | 2 +- .../DartnessCatchError/canHandle.html | 2 +- .../DartnessCatchError/errorTypes.html | 2 +- .../exception/DartnessCatchError/handler.html | 2 +- .../DartnessCatchErrorRegister-class.html | 3 +- .../DartnessCatchErrorRegister.html | 2 +- .../addCatchError.html | 2 +- .../DartnessCatchErrorRegister/handle.html | 2 +- .../exception/DartnessErrorHandler-class.html | 2 +- .../DartnessErrorHandler.html | 2 +- .../DartnessErrorHandler/catchErrors.html | 2 +- .../DartnessErrorHandler/errorHandler.html | 2 +- .../doc/api/exception/ErrorHandler-class.html | 2 +- .../exception/ErrorHandler/ErrorHandler.html | 2 +- .../exception/HttpStatusException-class.html | 4 +- .../HttpStatusException.html | 2 +- .../HttpStatusException/message.html | 2 +- .../HttpStatusException/statusCode.html | 2 +- .../doc/api/exception/exception-library.html | 4 +- packages/dartness_server/doc/api/index.html | 4 +- packages/dartness_server/doc/api/index.json | 2 +- .../doc/api/modules/Module-class.html | 2 +- .../doc/api/modules/Module/Module.html | 2 +- .../doc/api/modules/Module/metadata.html | 2 +- .../doc/api/modules/ModuleMetadata-class.html | 2 +- .../ModuleMetadata/ModuleMetadata.html | 2 +- .../modules/ModuleMetadata/controllers.html | 2 +- .../api/modules/ModuleMetadata/exports.html | 2 +- .../api/modules/ModuleMetadata/imports.html | 2 +- .../api/modules/ModuleMetadata/providers.html | 2 +- .../ProviderMetadata-class-sidebar.html | 2 + .../api/modules/ProviderMetadata-class.html | 39 ++- .../ProviderMetadata/ProviderMetadata.html | 12 +- .../modules/ProviderMetadata/classType.html | 13 +- .../api/modules/ProviderMetadata/name.html | 127 ++++++++++ .../modules/ProviderMetadata/useFactory.html | 127 ++++++++++ .../doc/api/modules/modules-library.html | 6 +- .../doc/api/route/Bind-class.html | 2 +- .../doc/api/route/Bind/Bind.html | 2 +- .../doc/api/route/Bind/method.html | 2 +- .../doc/api/route/Bind/path.html | 2 +- .../doc/api/route/Body-class.html | 2 +- .../doc/api/route/Body/Body.html | 2 +- .../doc/api/route/Connect-class.html | 2 +- .../doc/api/route/Connect/Connect.html | 2 +- .../doc/api/route/Controller-class.html | 2 +- .../doc/api/route/Controller/Controller.html | 2 +- .../doc/api/route/Controller/path.html | 2 +- .../doc/api/route/ControllerRoute-class.html | 2 +- .../ControllerRoute/ControllerRoute.html | 2 +- .../api/route/ControllerRoute/handler.html | 2 +- .../api/route/ControllerRoute/headers.html | 2 +- .../api/route/ControllerRoute/httpCode.html | 2 +- .../doc/api/route/ControllerRoute/method.html | 2 +- .../doc/api/route/ControllerRoute/params.html | 2 +- .../doc/api/route/ControllerRoute/path.html | 2 +- .../api/route/DartnessController-class.html | 2 +- .../DartnessController.html | 2 +- .../route/DartnessController/controller.html | 2 +- .../api/route/DartnessController/routes.html | 2 +- .../doc/api/route/DartnessParam-class.html | 2 +- .../route/DartnessParam/DartnessParam.html | 2 +- .../api/route/DartnessParam/defaultValue.html | 2 +- .../doc/api/route/DartnessParam/fromJson.html | 2 +- .../doc/api/route/DartnessParam/isBody.html | 2 +- .../doc/api/route/DartnessParam/isNamed.html | 2 +- .../api/route/DartnessParam/isOptional.html | 2 +- .../doc/api/route/DartnessParam/isPath.html | 2 +- .../api/route/DartnessParam/isPositional.html | 2 +- .../doc/api/route/DartnessParam/isQuery.html | 2 +- .../doc/api/route/DartnessParam/name.html | 2 +- .../doc/api/route/DartnessParam/type.html | 2 +- .../doc/api/route/DartnessRouter-class.html | 3 +- .../route/DartnessRouter/DartnessRouter.html | 2 +- .../doc/api/route/DartnessRouter/add.html | 2 +- .../doc/api/route/DartnessRouter/router.html | 2 +- .../route/DartnessRouterHandler-class.html | 2 +- .../DartnessRouterHandler.html | 2 +- .../DartnessRouterHandler/handleRoute.html | 2 +- .../route/DefaultDartnessRouter-class.html | 2 +- .../DefaultDartnessRouter.html | 2 +- .../api/route/DefaultDartnessRouter/add.html | 2 +- .../route/DefaultDartnessRouter/router.html | 2 +- .../doc/api/route/Delete-class.html | 2 +- .../doc/api/route/Delete/Delete.html | 2 +- .../doc/api/route/Get-class.html | 2 +- .../doc/api/route/Get/Get.html | 2 +- .../doc/api/route/Head-class.html | 2 +- .../doc/api/route/Head/Head.html | 2 +- .../doc/api/route/Header-class.html | 2 +- .../doc/api/route/Header/Header.html | 2 +- .../doc/api/route/Header/key.html | 2 +- .../doc/api/route/Header/value.html | 2 +- .../doc/api/route/Headers-class.html | 2 +- .../doc/api/route/Headers/Headers.html | 2 +- .../doc/api/route/Headers/values.html | 2 +- .../doc/api/route/HttpCode-class.html | 2 +- .../doc/api/route/HttpCode/HttpCode.html | 2 +- .../doc/api/route/HttpCode/code.html | 2 +- .../doc/api/route/Options-class.html | 2 +- .../doc/api/route/Options/Options.html | 2 +- .../doc/api/route/Patch-class.html | 2 +- .../doc/api/route/Patch/Patch.html | 2 +- .../doc/api/route/PathParam-class.html | 2 +- .../doc/api/route/PathParam/PathParam.html | 2 +- .../doc/api/route/PathParam/name.html | 2 +- .../doc/api/route/Post-class.html | 2 +- .../doc/api/route/Post/Post.html | 2 +- .../doc/api/route/Put-class.html | 2 +- .../doc/api/route/Put/Put.html | 2 +- .../doc/api/route/QueryParam-class.html | 2 +- .../doc/api/route/QueryParam/QueryParam.html | 2 +- .../doc/api/route/QueryParam/name.html | 2 +- .../doc/api/route/Trace-class.html | 2 +- .../doc/api/route/Trace/Trace.html | 2 +- .../doc/api/route/route-library.html | 2 +- packages/dartness_server/doc/api/search.html | 2 +- .../DartnessApplicationOptions-class.html | 2 +- .../DartnessApplicationOptions.html | 2 +- .../internetAddress.html | 2 +- .../logRequest.html | 2 +- .../DartnessApplicationOptions/port.html | 2 +- .../api/server/DartnessInterceptor-class.html | 3 +- .../DartnessInterceptor.html | 2 +- .../server/DartnessInterceptor/onError.html | 2 +- .../server/DartnessInterceptor/onRequest.html | 2 +- .../DartnessInterceptor/onResponse.html | 2 +- .../api/server/DartnessMiddleware-class.html | 3 +- .../DartnessMiddleware.html | 2 +- .../api/server/DartnessMiddleware/handle.html | 2 +- .../api/server/DartnessPipeline-class.html | 3 +- .../DartnessPipeline/DartnessPipeline.html | 2 +- .../DartnessPipeline/addErrorHandler.html | 2 +- .../server/DartnessPipeline/addHandler.html | 2 +- .../DartnessPipeline/addInterceptor.html | 2 +- .../DartnessPipeline/addMiddleware.html | 2 +- .../doc/api/server/DartnessRequest-class.html | 2 +- .../DartnessRequest.fromShelf.html | 2 +- .../DartnessRequest/DartnessRequest.html | 2 +- .../doc/api/server/DartnessRequest/body.html | 2 +- .../doc/api/server/DartnessRequest/bytes.html | 2 +- .../api/server/DartnessRequest/headers.html | 2 +- .../api/server/DartnessRequest/method.html | 2 +- .../server/DartnessRequest/requestedUri.html | 2 +- .../doc/api/server/DartnessRequest/url.html | 2 +- .../api/server/DartnessResponse-class.html | 2 +- .../DartnessResponse.fromShelf.html | 2 +- .../DartnessResponse/DartnessResponse.html | 2 +- .../doc/api/server/DartnessResponse/body.html | 2 +- .../api/server/DartnessResponse/bytes.html | 2 +- .../api/server/DartnessResponse/headers.html | 2 +- .../server/DartnessResponse/statusCode.html | 2 +- .../doc/api/server/DartnessServer-class.html | 3 +- .../server/DartnessServer/DartnessServer.html | 2 +- .../server/DartnessServer/addController.html | 2 +- .../DartnessServer/addErrorHandler.html | 2 +- .../server/DartnessServer/addInterceptor.html | 2 +- .../server/DartnessServer/addMiddleware.html | 2 +- .../api/server/DartnessServer/getAddress.html | 2 +- .../api/server/DartnessServer/getPort.html | 2 +- .../doc/api/server/DartnessServer/start.html | 2 +- .../doc/api/server/DartnessServer/stop.html | 2 +- .../server/DefaultDartnessPipeline-class.html | 2 +- .../DefaultDartnessPipeline.html | 2 +- .../addErrorHandler.html | 2 +- .../DefaultDartnessPipeline/addHandler.html | 2 +- .../addInterceptor.html | 2 +- .../addMiddleware.html | 2 +- .../server/DefaultDartnessServer-class.html | 2 +- .../DefaultDartnessServer.html | 2 +- .../DefaultDartnessServer/addController.html | 2 +- .../addErrorHandler.html | 2 +- .../DefaultDartnessServer/addInterceptor.html | 2 +- .../DefaultDartnessServer/addMiddleware.html | 2 +- .../DefaultDartnessServer/controllers.html | 2 +- .../DefaultDartnessServer/getAddress.html | 2 +- .../server/DefaultDartnessServer/getPort.html | 2 +- .../DefaultDartnessServer/isStarted.html | 2 +- .../server/DefaultDartnessServer/start.html | 2 +- .../server/DefaultDartnessServer/stop.html | 2 +- .../server/LogRequestsInterceptor-class.html | 2 +- .../LogRequestsInterceptor.html | 2 +- .../server/LogRequestsInterceptor/method.html | 2 +- .../LogRequestsInterceptor/onError.html | 2 +- .../LogRequestsInterceptor/onRequest.html | 2 +- .../LogRequestsInterceptor/onResponse.html | 2 +- .../LogRequestsInterceptor/startTime.html | 2 +- .../server/LogRequestsInterceptor/uri.html | 2 +- .../server/LogRequestsInterceptor/watch.html | 2 +- .../doc/api/server/server-library.html | 2 +- 220 files changed, 1040 insertions(+), 232 deletions(-) create mode 100644 packages/dartness_server/doc/api/dartness/Inject-class-sidebar.html create mode 100644 packages/dartness_server/doc/api/dartness/Inject-class.html create mode 100644 packages/dartness_server/doc/api/dartness/Inject/Inject.html create mode 100644 packages/dartness_server/doc/api/dartness/Inject/name.html create mode 100644 packages/dartness_server/doc/api/modules/ProviderMetadata/name.html create mode 100644 packages/dartness_server/doc/api/modules/ProviderMetadata/useFactory.html diff --git a/packages/dartness_generator/doc/api/__404error.html b/packages/dartness_generator/doc/api/__404error.html index cc5f1874..dd25bf82 100644 --- a/packages/dartness_generator/doc/api/__404error.html +++ b/packages/dartness_generator/doc/api/__404error.html @@ -94,7 +94,7 @@
dartness_generator dartness_generator - 0.4.6-alpha + 0.5.0-alpha diff --git a/packages/dartness_generator/doc/api/builder/application.html b/packages/dartness_generator/doc/api/builder/application.html index a7810230..80731ade 100644 --- a/packages/dartness_generator/doc/api/builder/application.html +++ b/packages/dartness_generator/doc/api/builder/application.html @@ -109,7 +109,7 @@
builder library