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
37 changes: 15 additions & 22 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ packages:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "34.0.0"
version: "47.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "3.2.0"
version: "4.7.0"
args:
dependency: transitive
description:
Expand All @@ -35,7 +35,7 @@ packages:
name: build
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
version: "2.3.1"
build_config:
dependency: transitive
description:
Expand All @@ -49,21 +49,21 @@ packages:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
version: "3.1.0"
build_resolvers:
dependency: transitive
description:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
version: "2.0.10"
build_runner:
dependency: "direct dev"
description:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.7"
version: "2.1.11"
build_runner_core:
dependency: transitive
description:
Expand All @@ -84,7 +84,7 @@ packages:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "8.0.6"
version: "8.4.1"
charcode:
dependency: transitive
description:
Expand All @@ -99,13 +99,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
cli_util:
dependency: transitive
description:
name: cli_util
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
code_builder:
dependency: transitive
description:
Expand Down Expand Up @@ -140,7 +133,7 @@ packages:
name: dart_style
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
version: "2.2.4"
file:
dependency: transitive
description:
Expand All @@ -161,14 +154,14 @@ packages:
name: freezed
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
version: "2.1.0+1"
freezed_annotation:
dependency: "direct main"
description:
name: freezed_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "2.1.0"
frontend_server_client:
dependency: transitive
description:
Expand Down Expand Up @@ -224,7 +217,7 @@ packages:
name: injectable_generator
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.3"
version: "1.5.4"
io:
dependency: transitive
description:
Expand Down Expand Up @@ -336,21 +329,21 @@ packages:
path: "../smartstruct"
relative: true
source: path
version: "1.2.6"
version: "1.2.7"
smartstruct_generator:
dependency: "direct dev"
description:
path: "../generator"
relative: true
source: path
version: "1.2.6"
version: "1.2.7"
source_gen:
dependency: transitive
description:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.1"
version: "1.2.5"
source_span:
dependency: transitive
description:
Expand Down Expand Up @@ -429,4 +422,4 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.14.0 <3.0.0"
dart: ">=2.17.0 <3.0.0"
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ environment:

publish_to: none
dependencies:
freezed_annotation: ^1.1.0
freezed_annotation: ^2.1.0
injectable: ^1.5.3
smartstruct: ^1.2.6
dev_dependencies:
build_runner: ^2.1.7
freezed: ^1.1.1
freezed: ^2.1.0+1
injectable_generator: ^1.5.3
#smartstruct_generator: ^1.2.6
smartstruct_generator:
Expand Down
12 changes: 6 additions & 6 deletions generator/lib/code_builders/assignment_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Expression generateSourceFieldAssignment(SourceAssignment sourceAssignment,
.map((sourceParam) => refer(sourceParam.displayName));
Expression expr = refer(sourceFunction.name);
if (sourceFunction.isStatic &&
sourceFunction.enclosingElement.name != null) {
expr = refer(sourceFunction.enclosingElement.name!)
sourceFunction.enclosingElement3.name != null) {
expr = refer(sourceFunction.enclosingElement3.name!)
.property(sourceFunction.name);
}
sourceFieldAssignment = expr.call([...references], makeNamedArgumentForStaticFunction(sourceFunction));
Expand Down Expand Up @@ -183,11 +183,11 @@ Iterable<MethodElement> _findMatchingMappingMethod(ClassElement classElement,
if(met.parameters.isEmpty) {
return false;
}
final metReturnElement = met.returnType.element;
final metParameterElement = met.parameters.first.type.element;
final metReturnElement = met.returnType.element2;
final metParameterElement = met.parameters.first.type.element2;

final targetReturnElement = targetReturnType.element;
final srcParameterElement = sourceParameterType.element;
final targetReturnElement = targetReturnType.element2;
final srcParameterElement = sourceParameterType.element2;

return metReturnElement == targetReturnElement &&
(metParameterElement == srcParameterElement);
Expand Down
58 changes: 34 additions & 24 deletions generator/lib/code_builders/class_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:code_builder/code_builder.dart';
import 'package:smartstruct_generator/code_builders/method_builder.dart';
import 'package:smartstruct_generator/code_builders/static_proxy_builder.dart';
import 'package:smartstruct_generator/mapper_config.dart';
import 'parameter_copy.dart';

Library buildMapperClass(
Expand All @@ -20,36 +21,46 @@ Library buildMapperClass(

List<Class> _generateStaticProxy(
ClassElement abstractClass, Map<String, dynamic> config) {
if(config['generateStaticProxy']) {
if (config['generateStaticProxy']) {
return [generateStaticProxy(abstractClass)];
}
return [];
}

List<Method> _generateStaticMethods(
ClassElement abstractClass, Map<String, dynamic> config) {
// I have no idea why the static methods needed to be generated.
// But if the return type is abstract class like List, the _generateBody in "method_builder.dart" can not work.
// So we need to filter these function.
var staticMethods = abstractClass.methods
.where((method) => method.isStatic
&& !_isPrimitive(method.returnType)
&& !isAbstractType(method.returnType))
.map((method) => buildStaticMapperImplementation(config, method, abstractClass))
.where(
(method) =>
method.isStatic &&
shouldGenerateStaticMethod(method) &&
!_isPrimitive(method.returnType) &&
!isAbstractType(method.returnType),
)
.map((method) =>
buildStaticMapperImplementation(config, method, abstractClass))
.toList();
return staticMethods;
}

bool shouldGenerateStaticMethod(MethodElement method) {
return !MapperConfig.isIgnoreMapping(method);
}

bool isAbstractType(DartType type) {
final element = type.element;
if(element is! ClassElement) {
return false;
}
return element.isAbstract;
final element = type.element2;
if (element is! ClassElement) {
return false;
}
return element.isAbstract;
}

bool _isPrimitive(DartType type) {
return type.isDartCoreBool || type.isDartCoreDouble || type.isDartCoreInt || type.isDartCoreNum || type.isDartCoreString;
return type.isDartCoreBool ||
type.isDartCoreDouble ||
type.isDartCoreInt ||
type.isDartCoreNum ||
type.isDartCoreString;
}

Class _generateMapperImplementationClass(
Expand All @@ -69,16 +80,15 @@ Class _generateMapperImplementationClass(
}

List<MethodElement> _getAllMethods(InterfaceType interfaceType) {

final set = LinkedHashSet<MethodElement>(
equals: (p0, p1) => p0.name == p1.name,
hashCode: (p0) => p0.name.hashCode,
);
// The methods of subclass has the priority.
// So it should be added before the methods of superclass.
set.addAll(interfaceType.methods);
set.addAll(interfaceType.allSupertypes.expand(_getAllMethods));
return set.toList();
final set = LinkedHashSet<MethodElement>(
equals: (p0, p1) => p0.name == p1.name,
hashCode: (p0) => p0.name.hashCode,
);
// The methods of subclass has the priority.
// So it should be added before the methods of superclass.
set.addAll(interfaceType.methods);
set.addAll(interfaceType.allSupertypes.expand(_getAllMethods));
return set.toList();
}

/// Generates a [Constructor] by copying the given [ConstructorElement] c
Expand Down
26 changes: 5 additions & 21 deletions generator/lib/code_builders/method_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'assignment_builder.dart';
/// Generates the implemented mapper method by the given abstract [MethodElement].
Method buildMapperImplementation(Map<String, dynamic> config,
MethodElement method, ClassElement abstractMapper) {
if (method.returnType.element == null) {
if (method.returnType.element2 == null) {
throw InvalidGenerationSourceError(
'${method.returnType} is not a valid return type',
element: method,
Expand Down Expand Up @@ -51,7 +51,7 @@ Code _generateBody(Map<String, dynamic> config, MethodElement method,
ClassElement abstractMapper) {
final blockBuilder = BlockBuilder();

final targetClass = method.returnType.element as ClassElement;
final targetClass = method.returnType.element2 as ClassElement;

final sourceParams = method.parameters;

Expand Down Expand Up @@ -161,7 +161,7 @@ List<HashMap<String, SourceAssignment>> _targetToSource(
ClassElement target,
MethodElement method,
Map<String, dynamic> config) {
final sourceMap = {for (var e in sources) e.type.element as ClassElement: e};
final sourceMap = {for (var e in sources) e.type.element2 as ClassElement: e};

final caseSensitiveFields = config['caseSensitiveFields'];
final fieldMapper = caseSensitiveFields ? (a) => a : (a) => a.toUpperCase();
Expand Down Expand Up @@ -202,30 +202,14 @@ List<HashMap<String, SourceAssignment>> _targetToSource(
for (var matchedTarget in matchedSourceClazzInSourceMapping.keys) {
final sourceValueList =
matchedSourceClazzInSourceMapping[matchedTarget]!;
final fieldClazz = f.type.element as ClassElement;
final foundFields = _findFields(fieldClazz);
final fieldClazz = f.type.element2 as ClassElement;

final refChain = RefChain.byPropNames(sourceEntry.value, sourceValueList.sublist(1));
targetToSource[matchedTarget] = SourceAssignment.fromRefChain(refChain);

// final matchingFieldForSourceValues =
// _findMatchingField(sourceValueList.sublist(1), foundFields);
// if (matchingFieldForSourceValues != null) {
// final sourceRefer = sourceValueList
// .sublist(0, sourceValueList.length - 1)
// .join(".");
// targetToSource[matchedTarget] = SourceAssignment.fromField(
// matchingFieldForSourceValues, sourceRefer);
// } else {
// targetToSource[f.name] =
// SourceAssignment.fromField(f, sourceEntry.value.displayName);
// }
}
} else {
targetToSource[f.name] =
SourceAssignment.fromRefChain(RefChain([sourceEntry.value, f]));
// targetToSource[f.name] =
// SourceAssignment.fromField(f, sourceEntry.value.displayName);
}
}
}
Expand Down Expand Up @@ -318,7 +302,7 @@ FieldElement? _findMatchingField(
final foundField = potentielFinds.first;
// foundField is not string
if (_shouldSearchMoreFields(foundField)) {
final searchClazz = foundField.type.element as ClassElement;
final searchClazz = foundField.type.element2 as ClassElement;
return _findMatchingField(
sources.skip(1).toList(), _findFields(searchClazz));
} else {
Expand Down
7 changes: 6 additions & 1 deletion generator/lib/mapper_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MapperConfig {
static Map<String, dynamic> readMapperConfig(
ConstantReader annotation, ClassElement mappingClass) {
var mapper =
mappingClass.metadata[0].element!.enclosingElement as ClassElement;
mappingClass.metadata[0].element!.enclosingElement3 as ClassElement;
final config = <String, dynamic>{};

for (final field in mapper.fields) {
Expand All @@ -35,6 +35,11 @@ class MapperConfig {
}
return config;
}

static bool isIgnoreMapping(MethodElement method) {
final annotations = TypeChecker.fromRuntime(IgnoreMapping).annotationsOf(method);
return annotations.isNotEmpty;
}
}

class MappingConfig {
Expand Down
Loading