Skip to content
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

Type info is valid generic type #39

Merged
merged 5 commits into from
Nov 17, 2024
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.4.6

- `TypeInfo`:
- `isValidGenericType`: fix for `Map`, `MapEntry`, `List` and `Set`.
- Added `toMapType`, `toMapEntryValueType`, `toMapEntryKeyType`, `toMapEntryType` and `isCastedMapEntry`.

## 2.4.5

- `_TypeWrapperList`, `_TypeWrapperMap`, `_TypeWrapperSet`, `_TypeWrapperIterable`:
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.4.5
// BUILDER: reflection_factory/2.4.6
// BUILD COMMAND: dart run build_runner build
//

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

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.4.5');
static final Version _version = Version.parse('2.4.6');

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.4.5
// BUILDER: reflection_factory/2.4.6
// BUILD COMMAND: dart run build_runner build
//

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

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.4.5');
static final Version _version = Version.parse('2.4.6');

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 @@ -20,7 +20,7 @@ import 'reflection_factory_utils.dart';
/// Class with all registered reflections ([ClassReflection]).
class ReflectionFactory {
// ignore: constant_identifier_names
static const String VERSION = '2.4.5';
static const String VERSION = '2.4.6';

static final ReflectionFactory _instance = ReflectionFactory._();

Expand Down
94 changes: 86 additions & 8 deletions lib/src/reflection_factory_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1361,13 +1361,38 @@
return true;
}

if (this.isIterable && hasArguments) {
var arg = arguments[0];
var valid = arg.isValidGenericType;
if (valid) {
var genericType2 = arg.toListType().genericType;
if (genericType2 == genericType) {
return true;
if (hasArguments) {
if (this.isIterable) {
var arg = arguments[0];
var valid = arg.isValidGenericType;
if (valid) {
TypeInfo<Iterable> iterableTypeInfo;
if (this.isSet) {
iterableTypeInfo = arg.toSetType();

Check warning on line 1371 in lib/src/reflection_factory_type.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/reflection_factory_type.dart#L1371

Added line #L1371 was not covered by tests
} else if (this.isList) {
iterableTypeInfo = arg.toListType();
} else {
iterableTypeInfo = arg.toIterableType();
}

var genericType2 = iterableTypeInfo.genericType;
if (genericType2 == genericType) {
return true;
}
}
} else if (this.isMap || this.isMapEntry) {
var arg0 = arguments[0];
var arg1 = arguments[1];

var valid = arg0.isValidGenericType && arg1.isValidGenericType;
if (valid) {
var typeInfo = this.isMapEntry
? arg0.toMapEntryType(arg1)
: arg0.toMapType(arg1);
var genericType2 = typeInfo.genericType;
if (genericType2 == genericType) {
return true;
}
}
}
}
Expand Down Expand Up @@ -1595,12 +1620,39 @@
return TypeInfo.fromType(Map, [keyType, this]);
}

/// Returns `this` as a [TypeInfo] for `Map<K,T>`.
/// Returns `this` as a [TypeInfo] for `Map<T,V>`.
TypeInfo<Map<T, V>> toMapKeyType<V>({TypeInfo? valueType}) {
valueType ??= TypeInfo.fromType(V);
return TypeInfo.fromType(Map, [this, valueType]);
}

/// Returns `this` as a [TypeInfo] for `Map<T,V>` ensuring that `V` is the same as [valueType].
TypeInfo<Map<T, V>> toMapType<V>(TypeInfo valueType) {
return valueType.callCasted(<E>() {
return toMapKeyType<E>(valueType: valueType) as TypeInfo<Map<T, V>>;
});
}

/// Returns `this` as a [TypeInfo] for `MapEntry<K,T>`.
TypeInfo<MapEntry<K, T>> toMapEntryValueType<K>({TypeInfo? keyType}) {
keyType ??= TypeInfo.fromType(K);

Check warning on line 1638 in lib/src/reflection_factory_type.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/reflection_factory_type.dart#L1638

Added line #L1638 was not covered by tests
return TypeInfo.fromType(MapEntry, [keyType, this]);
}

/// Returns `this` as a [TypeInfo] for `MapEntry<T,V>`.
TypeInfo<MapEntry<T, V>> toMapEntryKeyType<V>({TypeInfo? valueType}) {
valueType ??= TypeInfo.fromType(V);

Check warning on line 1644 in lib/src/reflection_factory_type.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/reflection_factory_type.dart#L1644

Added line #L1644 was not covered by tests
return TypeInfo.fromType(MapEntry, [this, valueType]);
}

/// Returns `this` as a [TypeInfo] for `MapEntry<T,V>` ensuring that `V` is the same as [valueType].
TypeInfo<MapEntry<T, V>> toMapEntryType<V>(TypeInfo valueType) {
return valueType.callCasted(<E>() {
return toMapEntryKeyType<E>(valueType: valueType)
as TypeInfo<MapEntry<T, V>>;
});
}

/// Returns `true` if `this`.[type] equals to [other].[type].
///
/// Resolves some [Type] singleton issues with [List], [Set] and [Map].
Expand Down Expand Up @@ -2148,6 +2200,32 @@
return false;
}
}

/// Returns `true` if [o] is a `MapEntry<K,V>`
/// where `K` is [arguments0] `T` and `V` is [arguments1] `T`.
/// - `K` or `V` should be valid. See [isValidGenericType].
/// - This [TypeInfo] should be a [MapEntry]. See [isMapEntry].
bool isCastedMapEntry(Object? o) {
if (!isMapEntry || o is! MapEntry) return false;

var arg0 = arguments0;
var arg1 = arguments1;

var arg0Ok = arg0 != null && arg0.isValidGenericType;
var arg1Ok = arg1 != null && arg1.isValidGenericType;

if (arg0Ok && arg1Ok) {
return arg0.callCasted(<K>() {
return arg1.callCasted(<V>() => o is MapEntry<K, V>);
});
} else if (arg0Ok) {
return arg0.callCasted(<K>() => o is MapEntry<K, dynamic>);

Check warning on line 2222 in lib/src/reflection_factory_type.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/reflection_factory_type.dart#L2222

Added line #L2222 was not covered by tests
} else if (arg1Ok) {
return arg1.callCasted(<V>() => o is MapEntry<dynamic, V>);

Check warning on line 2224 in lib/src/reflection_factory_type.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/reflection_factory_type.dart#L2224

Added line #L2224 was not covered by tests
} else {
return false;
}
}
}

final TypeInfoListEquality _listEqualityTypeInfo = TypeInfoListEquality();
Expand Down
4 changes: 2 additions & 2 deletions 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.4.5
version: 2.4.6
homepage: https://github.com/gmpassos/reflection_factory

environment:
Expand All @@ -27,5 +27,5 @@ dev_dependencies:
data_serializer: ^1.2.1
dependency_validator: ^3.2.3
test: ^1.25.8
coverage: ^1.10.0
coverage: ^1.11.0
benchmark: ^0.3.0
77 changes: 77 additions & 0 deletions test/reflection_factory_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,22 @@ void main() {
expect(t10.hasArguments, isFalse);
expect(t10.equivalentArgumentsTypes([]), isTrue);
expect(t10.equivalentArgumentsTypes([int]), isFalse);

var t11 = TypeInfo.fromType(BigInt);
expect(t11.isBigInt, isTrue);
expect(t11.isInt, isFalse);
expect(t11.isDateTime, isFalse);
expect(t11.hasArguments, isFalse);
expect(t11.equivalentArgumentsTypes([]), isTrue);
expect(t11.equivalentArgumentsTypes([int]), isFalse);

var t12 = TypeInfo.fromType(Uint8List);
expect(t12.isUInt8List, isTrue);
expect(t12.isList, isFalse);
expect(t11.isInt, isFalse);
expect(t12.hasArguments, isFalse);
expect(t12.equivalentArgumentsTypes([]), isTrue);
expect(t12.equivalentArgumentsTypes([int]), isFalse);
});

test('from', () async {
Expand Down Expand Up @@ -889,7 +905,9 @@ void main() {
{
var t = TypeInfo<Iterable<int>>.fromType(Iterable, [int]);

expect(t.isValidGenericType, isTrue);
expect(t.genericType, equals(Iterable<int>));
expect(t.arguments0?.isValidGenericType, isTrue);
expect(t.arguments0?.genericType, equals(int));

var list = <int>[1, 2, 3];
Expand All @@ -916,6 +934,7 @@ void main() {
{
var t = TypeInfo<Map<String, int>>.fromType(Map, [String, int]);

expect(t.isValidGenericType, isTrue);
expect(t.genericType, equals(Map<String, int>));
expect(t.arguments0?.genericType, equals(String));
expect(t.arguments1?.genericType, equals(int));
Expand Down Expand Up @@ -989,6 +1008,61 @@ void main() {
expect(t.isCastedMap(map2), isTrue);

expect(t.isCastedMap(mapObj), isFalse);
expect(t.isCastedMapEntry(mapObj), isFalse);
}
});

test('isCastedMapEntry<K,V>', () async {
{
var t =
TypeInfo<MapEntry<String, int>>.fromType(MapEntry, [String, int]);

expect(t.isValidGenericType, isTrue);
expect(t.genericType, equals(MapEntry<String, int>));
expect(t.arguments0?.genericType, equals(String));
expect(t.arguments1?.genericType, equals(int));

expect(t.arguments0!.toMapEntryType(t.arguments1!).genericType,
equals(MapEntry<String, int>));

expect(
t.arguments0!
.toMapEntryKeyType<int>(valueType: t.arguments1!)
.genericType,
equals(MapEntry<String, int>));

expect(
t.arguments0!
.toMapEntryKeyType(valueType: t.arguments1!)
.genericType,
equals(MapEntry<String, dynamic>));

expect(
t.arguments1!
.toMapEntryValueType<String>(keyType: t.arguments0!)
.genericType,
equals(MapEntry<String, int>));

expect(
t.arguments1!
.toMapEntryValueType(keyType: t.arguments0!)
.genericType,
equals(MapEntry<dynamic, int>));

var mapEntry = MapEntry<String, int>('a', 1);
var mapEntryObjKV = MapEntry<Object, Object>('a', 1);
var mapEntryObjV = MapEntry<String, Object>('a', 1);
var mapEntryObjK = MapEntry<Object, int>('a', 1);

expect(t.isCastedMapEntry(mapEntry), isTrue);
expect(t.isCastedMapEntry(mapEntryObjKV), isFalse);
expect(t.isCastedMapEntry(mapEntryObjV), isFalse);
expect(t.isCastedMapEntry(mapEntryObjK), isFalse);

expect(t.isCastedList(mapEntry), isFalse);
expect(t.isCastedSet(mapEntry), isFalse);
expect(t.isCastedIterable(mapEntry), isFalse);
expect(t.isCastedMap(mapEntry), isFalse);
}
});

Expand Down Expand Up @@ -2114,6 +2188,9 @@ void main() {

expect(TypeParser.parseBigInt(DateTime.utc(2020, 10, 2, 3, 4, 5, 0, 0)),
equals(BigInt.from(1601607845000)));

expect(TypeParser.parseBigInt(Duration(hours: 3)),
equals(BigInt.from(1000 * 60 * 60 * 3)));
});
});
}
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.4.5
// BUILDER: reflection_factory/2.4.6
// BUILD COMMAND: dart run build_runner build
//

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

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.4.5');
static final Version _version = Version.parse('2.4.6');

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.4.5
// BUILDER: reflection_factory/2.4.6
// BUILD COMMAND: dart run build_runner build
//

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

mixin __ReflectionMixin {
static final Version _version = Version.parse('2.4.5');
static final Version _version = Version.parse('2.4.6');

Version get reflectionFactoryVersion => _version;

Expand Down
Loading