Skip to content

Commit

Permalink
Fixing use case described in #79 (#80)
Browse files Browse the repository at this point in the history
* Fixing use case described in #79

* version bump

* test fix
  • Loading branch information
numen31337 authored Feb 27, 2023
1 parent 650b19c commit ab006c7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions copy_with_extension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.0.2
* [Fix](https://github.com/numen31337/copy_with_extension/issues/79) Allow having a nullable constructor parameter with a fallback for a non-nullable class field.

## 5.0.1
* [Fix](https://github.com/numen31337/copy_with_extension/issues/72) Warnings when using the `?` operator on a dynamic type.
* [Fix](https://github.com/numen31337/copy_with_extension/issues/75) Warnings when using the `!` operator on a non-nullable type.
Expand Down
2 changes: 1 addition & 1 deletion copy_with_extension/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: copy_with_extension
version: 5.0.1
version: 5.0.2
description: Annotation for generating `copyWith` extensions code using `copy_with_extension_gen`.
homepage: https://github.com/numen31337/copy_with_extension/tree/master/copy_with_extension
repository: https://github.com/numen31337/copy_with_extension
Expand Down
3 changes: 3 additions & 0 deletions copy_with_extension_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.0.2
* [Fix](https://github.com/numen31337/copy_with_extension/issues/79) Allow having a nullable constructor parameter with a fallback for a non-nullable class field.

## 5.0.1
* [Fix](https://github.com/numen31337/copy_with_extension/issues/72) Warnings when using the `?` operator on a dynamic type.
* [Fix](https://github.com/numen31337/copy_with_extension/issues/75) Warnings when using the `!` operator on a non-nullable type.
Expand Down
5 changes: 3 additions & 2 deletions copy_with_extension_gen/lib/src/copy_with_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {

for (final field in sortedFields) {
if (field.classFieldInfo != null &&
field.nullable != field.classFieldInfo?.nullable) {
field.nullable == false &&
field.classFieldInfo?.nullable == true) {
throw InvalidGenerationSourceError(
'The nullability of the constructor parameter "${field.name}" does not match the nullability of the corresponding field in the object.',
'The constructor parameter "${field.name}" is not nullable, whereas the corresponding class field is nullable. This use case is not supported.',
element: element,
);
}
Expand Down
2 changes: 1 addition & 1 deletion copy_with_extension_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: copy_with_extension_gen
version: 5.0.1
version: 5.0.2
description: Automatically generating `copyWith` extensions code for classes with `@CopyWith()` annotation.
repository: https://github.com/numen31337/copy_with_extension
homepage: https://github.com/numen31337/copy_with_extension/tree/master/copy_with_extension_gen
Expand Down
26 changes: 22 additions & 4 deletions copy_with_extension_gen/test/gen_nullability_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ part 'gen_nullability_test.g.dart';

@CopyWith()
class TestNullability {
TestNullability(
this.dynamicField,
this.integers,
);
TestNullability(this.dynamicField, this.integers, {int? constructorFallback})
: constructorFallback = constructorFallback ?? 0;

/// https://github.com/numen31337/copy_with_extension/issues/74
/// Test for crash on `instance.dynamicField!`.
Expand All @@ -18,6 +16,10 @@ class TestNullability {
/// Warnings during compilation when using `!` on non-nullable value.
/// Use `dart run copy_with_extension_gen/test/gen_nullability_test.dart` to reproduce the warning.
final List<int> integers;

/// https://github.com/numen31337/copy_with_extension/issues/79
/// Case when a class has non-nullable type, but the constructor accepts nullable and falls back.
final int constructorFallback;
}

void main() {
Expand All @@ -30,5 +32,21 @@ void main() {
expect(TestNullability(null, [1]).copyWith.dynamicField(1).dynamicField, 1);
expect(
TestNullability(null, [1]).copyWith.integers([2]).dynamicField, null);

// Test fallback is working
expect(
TestNullability(1, [1], constructorFallback: 1).constructorFallback, 1);
expect(
TestNullability(1, [1], constructorFallback: 1)
.copyWith
.constructorFallback(null)
.constructorFallback,
0);
expect(
TestNullability(1, [1], constructorFallback: 1)
.copyWith
.constructorFallback(2)
.constructorFallback,
2);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NoDefaultConstructor {
}

@ShouldThrow(
'The nullability of the constructor parameter "nullableWithNonNullableConstructor" does not match the nullability of the corresponding field in the object.',
'The constructor parameter "nullableWithNonNullableConstructor" is not nullable, whereas the corresponding class field is nullable. This use case is not supported.',
)
@CopyWith()
class TestNullability {
Expand Down

0 comments on commit ab006c7

Please sign in to comment.