mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
implement inject generator
This commit is contained in:
@@ -11,6 +11,9 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
@experimental
|
||||||
// ignore: camel_case_types
|
// ignore: camel_case_types
|
||||||
final class inject {
|
final class inject {
|
||||||
const inject();
|
const inject();
|
||||||
|
|||||||
@@ -11,6 +11,9 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
@experimental
|
||||||
// ignore: camel_case_types
|
// ignore: camel_case_types
|
||||||
final class injectable {
|
final class injectable {
|
||||||
const injectable();
|
const injectable();
|
||||||
|
|||||||
@@ -11,6 +11,9 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
@experimental
|
||||||
// ignore: camel_case_types
|
// ignore: camel_case_types
|
||||||
final class scope {
|
final class scope {
|
||||||
final String? name;
|
final String? name;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:analyzer/dart/constant/value.dart';
|
import 'package:analyzer/dart/constant/value.dart';
|
||||||
|
import 'package:analyzer/dart/element/type.dart';
|
||||||
import 'package:build/build.dart';
|
import 'package:build/build.dart';
|
||||||
import 'package:source_gen/source_gen.dart';
|
import 'package:source_gen/source_gen.dart';
|
||||||
import 'package:analyzer/dart/element/element.dart';
|
import 'package:analyzer/dart/element/element.dart';
|
||||||
@@ -44,18 +45,32 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final String fieldType = field.type.getDisplayString();
|
// --- Фикс: определяем resolveAsync для Future<T> ---
|
||||||
|
final DartType type = field.type;
|
||||||
|
String genericType;
|
||||||
|
String resolveMethod;
|
||||||
|
|
||||||
|
if (type.isDartAsyncFuture) {
|
||||||
|
// Если поле Future<T>
|
||||||
|
final typeArg = (type as ParameterizedType).typeArguments.first;
|
||||||
|
genericType = typeArg.getDisplayString();
|
||||||
|
resolveMethod = 'resolveAsync<$genericType>';
|
||||||
|
} else {
|
||||||
|
genericType = type.getDisplayString();
|
||||||
|
resolveMethod = 'resolve<$genericType>';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Вызываем openScope или openRootScope
|
||||||
String accessor = (scopeName != null && scopeName.isNotEmpty)
|
String accessor = (scopeName != null && scopeName.isNotEmpty)
|
||||||
? "CherryPick.openScope(scopeName: '$scopeName').resolve"
|
? "CherryPick.openScope(scopeName: '$scopeName').$resolveMethod"
|
||||||
: "CherryPick.openRootScope().resolve";
|
: "CherryPick.openRootScope().$resolveMethod";
|
||||||
|
|
||||||
String generic = fieldType != 'dynamic' ? '<$fieldType>' : '';
|
// Аргументы resolve
|
||||||
String params = (namedValue != null && namedValue.isNotEmpty)
|
String params = (namedValue != null && namedValue.isNotEmpty)
|
||||||
? "(named: '$namedValue')"
|
? "(named: '$namedValue')"
|
||||||
: '()';
|
: '()';
|
||||||
|
|
||||||
buffer.writeln(" instance.${field.name} = $accessor$generic$params;");
|
buffer.writeln(" instance.${field.name} = $accessor$params;");
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.writeln(' }');
|
buffer.writeln(' }');
|
||||||
|
|||||||
@@ -13,24 +13,16 @@ part 'app.inject.cherrypick.g.dart';
|
|||||||
class MyApp extends StatelessWidget with _$MyApp {
|
class MyApp extends StatelessWidget with _$MyApp {
|
||||||
final _appRouter = AppRouter();
|
final _appRouter = AppRouter();
|
||||||
|
|
||||||
@scope('authZone')
|
|
||||||
@inject()
|
|
||||||
late final String text;
|
|
||||||
|
|
||||||
@scope('authZone')
|
|
||||||
@named('timeout')
|
|
||||||
@inject()
|
|
||||||
late final int timeout;
|
|
||||||
|
|
||||||
@named('repo')
|
@named('repo')
|
||||||
@inject()
|
@inject()
|
||||||
late final PostRepository repository;
|
late final PostRepository repository;
|
||||||
|
|
||||||
MyApp({super.key});
|
MyApp({super.key}) {
|
||||||
|
_inject(this);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
_inject(this);
|
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (_) => PostBloc(repository),
|
create: (_) => PostBloc(repository),
|
||||||
child: MaterialApp.router(
|
child: MaterialApp.router(
|
||||||
|
|||||||
Reference in New Issue
Block a user