Skip to content

Commit

Permalink
Merge v2.3.4 - fix records with named parameters
Browse files Browse the repository at this point in the history
 - Fix handling og record types with named parameters.
 - Fix type argument resolution.
  • Loading branch information
gmpassos committed Apr 6, 2024
2 parents e9aedec + 3a6ee79 commit b7a2cde
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.3.4

- Fix handling og record types with named parameters.
- Fix type argument resolution.

## 2.3.3

- `ClassReflection`:
Expand Down
4 changes: 2 additions & 2 deletions example/reflection_factory_bridge_example.reflection.g.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GENERATED CODE - DO NOT MODIFY BY HAND!
// BUILDER: reflection_factory/2.3.3
// BUILDER: reflection_factory/2.3.4
// BUILD COMMAND: dart run build_runner build
//

Expand All @@ -20,7 +20,7 @@ typedef __TI<T> = TypeInfo<T>;
typedef __PR = ParameterReflection;

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.3.3');
static final Version _version = Version.parse('2.3.4');

Version get reflectionFactoryVersion => _version;

Expand Down
4 changes: 2 additions & 2 deletions example/reflection_factory_example.reflection.g.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GENERATED CODE - DO NOT MODIFY BY HAND!
// BUILDER: reflection_factory/2.3.3
// BUILDER: reflection_factory/2.3.4
// BUILD COMMAND: dart run build_runner build
//

Expand All @@ -20,7 +20,7 @@ typedef __TI<T> = TypeInfo<T>;
typedef __PR = ParameterReflection;

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.3.3');
static final Version _version = Version.parse('2.3.4');

Version get reflectionFactoryVersion => _version;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/reflection_factory_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'reflection_factory_utils.dart';
/// Class with all registered reflections ([ClassReflection]).
class ReflectionFactory {
// ignore: constant_identifier_names
static const String VERSION = '2.3.3';
static const String VERSION = '2.3.4';

static final ReflectionFactory _instance = ReflectionFactory._();

Expand Down
49 changes: 32 additions & 17 deletions lib/src/reflection_factory_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3243,16 +3243,26 @@ extension _DartTypeExtension on DartType {

String fullTypeNameResolvable(
{bool withNullability = true, Iterable<String>? typeParameters}) {
if (this is InvalidType) {
return 'dynamic';
}

var name = resolveTypeName(typeParameters: typeParameters);

if (!hasTypeArguments) {
return withNullability && isNullable ? '$name?' : name;
}

var args = resolvedTypeArguments
.map((e) => e.fullTypeNameResolvable(
withNullability: withNullability, typeParameters: typeParameters))
.join(',');
var argsList = resolvedTypeArguments.map((e) {
return e.fullTypeNameResolvable(
withNullability: withNullability, typeParameters: typeParameters);
}).toList();

if (argsList.isEmpty || argsList.every((a) => a == 'dynamic')) {
return name;
}

var args = argsList.join(',');

return withNullability && isNullable ? '$name<$args>?' : '$name<$args>';
}
Expand All @@ -3264,23 +3274,26 @@ extension _DartTypeExtension on DartType {

var recordType = this as RecordType;

var recordTypesNamesPos = recordType.positionalFields
.map((t) => t.type.resolveTypeName(typeParameters: typeParameters));
var recordTypesNamesPos = recordType.positionalFields.map((t) {
return t.type.fullTypeNameResolvable(typeParameters: typeParameters);
}).toList();

var recordTypesNamesNamed = recordType.namedFields.map((t) =>
'${t.name} ${t.type.resolveTypeName(typeParameters: typeParameters)}');
var recordTypesNamesNamed = recordType.namedFields.map((t) {
return '${t.type.fullTypeNameResolvable(typeParameters: typeParameters)} ${t.name}';
}).toList();

var recordDeclaration = [
var list = [
'(',
recordTypesNamesPos.join(', '),
if (recordTypesNamesNamed.isNotEmpty)
{
'{',
recordTypesNamesNamed.join(', '),
'}',
},
if (recordTypesNamesPos.isNotEmpty) recordTypesNamesPos.join(', '),
if (recordTypesNamesNamed.isNotEmpty) ...[
'{',
recordTypesNamesNamed.join(', '),
'}',
],
')',
].join();
];

var recordDeclaration = list.join();

return recordDeclaration;
}
Expand Down Expand Up @@ -3468,6 +3481,8 @@ extension _DartTypeExtension on DartType {
return '$tr.$constName';
} else {
if (isRecordType) {
typeNameResolvable;

var typeAlias = typeAliasTable.aliasForRecordType(name);
return '$tr<$typeAlias>($typeAlias)';
} else if (this is TypeParameterType) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: reflection_factory
description: Allows Dart reflection with an easy approach, even for third-party classes, using code generation portable for all Dart platforms.
version: 2.3.3
version: 2.3.4
homepage: https://github.com/gmpassos/reflection_factory

environment:
Expand Down
55 changes: 52 additions & 3 deletions test/reflection_factory_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void main() {
matches(RegExp(
r"__TR<List<Set<int\?>>>\(\s*List, <__TR>\[__TR.tSetInt\]\)")),
matches(RegExp(
r"__TR<Set<List<dynamic>>>\(\s*Set, <__TR>\[__TR.tListDynamic\]\)")),
r"__TR<Set<List>>\(\s*Set, <__TR>\[__TR.tListDynamic\]\)")),
),
)),
},
Expand Down Expand Up @@ -456,8 +456,9 @@ void main() {
matches(RegExp(
r"'ignoreCase':\s*__PR\(\s*__TR.tBool\s*,\s*'ignoreCase'\s*,\s*false\s*,\s*false\s*,\s*false\s*\)")),
allOf(
matches(RegExp(r"typedef __RCD1 = \(bool, String\);")),
matches(RegExp(r"MethodReflection<User,\s+\(bool, String\)>\(")),
matches(RegExp(r"typedef __RCD1 = \(bool, String\?\);")),
matches(
RegExp(r"MethodReflection<User,\s+\(bool, String\?\)>\(")),
matches(RegExp(r"'checkPassword',\s+__TR<__RCD1>\(__RCD1\),")),
),
)),
Expand Down Expand Up @@ -549,6 +550,54 @@ void main() {
);
});

test('EnableReflection: records (named)', () async {
var builder = ReflectionBuilder(verbose: true);

var sourceAssets = {
'$_pkgName|lib/foo.dart': '''
import 'package:reflection_factory/reflection_factory.dart';
part 'foo.reflection.g.dart';
@EnableReflection()
class Validator {
({bool ok, String? error}) validate() => (ok: true, error: null);
}
'''
};

await testBuilder(
builder,
sourceAssets,
reader: await PackageAssetReader.currentIsolate(),
generateFor: {'$_pkgName|lib/foo.dart'},
outputs: {
'$_pkgName|lib/foo.reflection.g.dart': decodedMatches(allOf(
allOf(
contains('GENERATED CODE - DO NOT MODIFY BY HAND'),
contains(
'BUILDER: reflection_factory/${ReflectionFactory.VERSION}'),
contains("part of 'foo.dart'"),
contains(
"Version _version = Version.parse('${ReflectionFactory.VERSION}')"),
),
allOf(
contains('Validator\$reflection'),
contains('Validator\$reflectionExtension'),
contains("typedef __RCD1 = ({String? error, bool ok});"),
),
)),
},
onLog: (msg) {
print(msg);
},
);
});

test('EnableReflection(reflectionClassName, reflectionExtensionName)',
() async {
var builder = ReflectionBuilder(verbose: true);
Expand Down
4 changes: 2 additions & 2 deletions test/src/reflection/user_with_reflection.g.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GENERATED CODE - DO NOT MODIFY BY HAND!
// BUILDER: reflection_factory/2.3.3
// BUILDER: reflection_factory/2.3.4
// BUILD COMMAND: dart run build_runner build
//

Expand All @@ -20,7 +20,7 @@ typedef __TI<T> = TypeInfo<T>;
typedef __PR = ParameterReflection;

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.3.3');
static final Version _version = Version.parse('2.3.4');

Version get reflectionFactoryVersion => _version;

Expand Down
4 changes: 2 additions & 2 deletions test/src/user_reflection_bridge.reflection.g.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// GENERATED CODE - DO NOT MODIFY BY HAND!
// BUILDER: reflection_factory/2.3.3
// BUILDER: reflection_factory/2.3.4
// BUILD COMMAND: dart run build_runner build
//

Expand All @@ -20,7 +20,7 @@ typedef __TI<T> = TypeInfo<T>;
typedef __PR = ParameterReflection;

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.3.3');
static final Version _version = Version.parse('2.3.4');

Version get reflectionFactoryVersion => _version;

Expand Down

0 comments on commit b7a2cde

Please sign in to comment.