Skip to content

Commit

Permalink
fix for copyWith null for a non-nullable value (#46)
Browse files Browse the repository at this point in the history
* fix for copyWith null for a non-nullable value

* version bump

* minor ci correction

* minor ci correction
  • Loading branch information
numen31337 committed Feb 3, 2022
1 parent 7907433 commit 274f369
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ jobs:
run: |
cmp -s example/example.dart ../copy_with_extension/example/example.dart || (echo "::error ::File example.dart is out of sync" && exit 1)
cmp -s LICENSE ../copy_with_extension/LICENSE || (echo "::error ::File LICENSE is out of sync" && exit 1)
cmp -s CHANGELOG.md ../copy_with_extension/CHANGELOG.md || (echo "::error ::File CHANGELOG.md is out of sync" && exit 1)
cmp -s CHANGELOG.md ../copy_with_extension/CHANGELOG.md || (echo "::warning ::File CHANGELOG.md is out of sync")
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 @@
## 4.0.1
* [Fix](https://github.com/numen31337/copy_with_extension/issues/45) for passing `null` into `copyWith` function for non-nullable values.

## 4.0.0
* **BREAKING** `copyWith` function now correctly supports nullification of nullable fields like so `copyWith(id: null)`.
* **BREAKING** `CopyWith` annotation for named constructor `namedConstructor` is renamed to `constructor` to be in sync with [json_serializable](https://pub.dev/packages/json_serializable).
Expand Down
4 changes: 3 additions & 1 deletion copy_with_extension_gen/lib/src/copy_with_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {
'',
(r, v) {
if (v.immutable) return '$r ${v.name}: _value.${v.name},';
final nullCheckForNonNullable =
v.nullable ? "" : "|| ${v.name} == null";

return '''
$r ${v.name}:
${v.name} == const \$CopyWithPlaceholder()
${v.name} == const \$CopyWithPlaceholder() $nullCheckForNonNullable
? _value.${v.name}
// ignore: cast_nullable_to_non_nullable
: ${v.name} as ${v.type},''';
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: 4.0.0
version: 4.0.1
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class CopyWithValues {
class CopyWithValuesOptional {
final String? id;

const CopyWithValuesOptional({
this.id,
});
const CopyWithValuesOptional({this.id});
}

@CopyWith()
Expand Down Expand Up @@ -50,6 +48,8 @@ void main() {
const CopyWithValues(id: '').copyWith(id: "test").id,
"test",
);

expect(const CopyWithValues(id: '').copyWith(id: null).id, '');
});

test('CopyWithValuesOptional', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _$BasicClassCWProxyImpl<T extends Iterable<int>>
Object? optional = const $CopyWithPlaceholder(),
}) {
return BasicClass<T>(
id: id == const $CopyWithPlaceholder()
id: id == const $CopyWithPlaceholder() || id == null
? _value.id
// ignore: cast_nullable_to_non_nullable
: id as String,
Expand Down

0 comments on commit 274f369

Please sign in to comment.