-
Notifications
You must be signed in to change notification settings - Fork 455
generic support #1552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
generic support #1552
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import 'package:analyzer/dart/constant/value.dart'; | ||
| import 'package:analyzer/dart/element/element.dart'; | ||
| import 'package:analyzer/src/dart/constant/value.dart'; | ||
| import 'package:analyzer/dart/element/type.dart'; | ||
| import 'package:source_helper/source_helper.dart'; | ||
|
|
||
|
|
@@ -122,12 +123,19 @@ ConvertData? _convertData(DartObject obj, FieldElement element, bool isFrom) { | |
| if (objectValue == null || objectValue.isNull) { | ||
| return null; | ||
| } | ||
|
|
||
| final executableElement = objectValue.toFunctionValue()!; | ||
| var executableType = executableElement.type; | ||
| var qualifiedName = executableElement.qualifiedName; | ||
| final state = (objectValue as DartObjectImpl).state; | ||
| if (((state as FunctionState).typeArguments?.length ?? 0) > 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| final generics = state.typeArguments!.cast<DartType>(); | ||
| executableType = executableType.instantiate(generics); | ||
| qualifiedName += "<${generics.join(',')}>"; | ||
| } | ||
|
Comment on lines
+129
to
+134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes rely on internal APIs from the Could you investigate if there's an alternative approach using only public APIs to retrieve the generic type arguments for the function? If this is the only way, the dependency on |
||
|
|
||
| if (executableElement.formalParameters.isEmpty || | ||
| executableElement.formalParameters.first.isNamed || | ||
| executableElement.formalParameters.where((pe) => !pe.isOptional).length > | ||
| if (executableType.formalParameters.isEmpty || | ||
| executableType.formalParameters.first.isNamed || | ||
| executableType.formalParameters.where((pe) => !pe.isOptional).length > | ||
| 1) { | ||
| throwUnsupported( | ||
| element, | ||
|
|
@@ -136,8 +144,8 @@ ConvertData? _convertData(DartObject obj, FieldElement element, bool isFrom) { | |
| ); | ||
| } | ||
|
|
||
| final returnType = executableElement.returnType; | ||
| final argType = executableElement.formalParameters.first.type; | ||
| final returnType = executableType.returnType; | ||
| final argType = executableType.formalParameters.first.type; | ||
| if (isFrom) { | ||
| final hasDefaultValue = !jsonKeyAnnotation( | ||
| element, | ||
|
|
@@ -179,5 +187,5 @@ ConvertData? _convertData(DartObject obj, FieldElement element, bool isFrom) { | |
| } | ||
| } | ||
|
|
||
| return ConvertData(executableElement.qualifiedName, argType, returnType); | ||
| return ConvertData(qualifiedName, argType, returnType); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||
|
|
||||||
| import 'package:analyzer/dart/constant/value.dart'; | ||||||
| import 'package:analyzer/dart/element/element.dart'; | ||||||
| import 'package:analyzer/dart/element/nullability_suffix.dart'; | ||||||
| import 'package:analyzer/dart/element/type.dart'; | ||||||
| import 'package:json_annotation/json_annotation.dart'; | ||||||
| import 'package:source_gen/source_gen.dart'; | ||||||
|
|
@@ -281,9 +282,20 @@ _ConverterMatch? _compatibleMatch( | |||||
| ElementAnnotation? annotation, | ||||||
| DartObject constantValue, | ||||||
| ) { | ||||||
| final converterClassElement = constantValue.type!.element as ClassElement; | ||||||
| var converterClassType = constantValue.type! as InterfaceType; | ||||||
| final converterClassElement = converterClassType.element as ClassElement; | ||||||
| String? genericTypeArg; | ||||||
|
|
||||||
| final generics = (constantValue.type as InterfaceType).typeArguments; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| if (generics.isNotEmpty) { | ||||||
| converterClassType = converterClassElement.instantiate( | ||||||
| typeArguments: generics, | ||||||
| nullabilitySuffix: NullabilitySuffix.none, | ||||||
| ); | ||||||
| genericTypeArg = generics.join(", "); | ||||||
| } | ||||||
|
|
||||||
| final jsonConverterSuper = converterClassElement.allSupertypes | ||||||
| final jsonConverterSuper = converterClassType.allSupertypes | ||||||
| .where((e) => _jsonConverterChecker.isExactly(e.element)) | ||||||
| .singleOrNull; | ||||||
|
|
||||||
|
|
@@ -302,7 +314,7 @@ _ConverterMatch? _compatibleMatch( | |||||
| annotation, | ||||||
| constantValue, | ||||||
| jsonConverterSuper.typeArguments[1], | ||||||
| null, | ||||||
| genericTypeArg, | ||||||
| fieldType, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is NOT ideal...