diff --git a/README.md b/README.md index e96e6a2..b3346eb 100644 --- a/README.md +++ b/README.md @@ -24,23 +24,22 @@ import 'package:struct_annotation/struct_annotation.dart'; @Struct() class Person { final String name; - final int age; } void main() { // 🪨 Create a const instance with required, name parameters. - const jane = Person(name: 'Jane', age: 42); + const dash = Person(name: 'Dash'); // 🖨️ Create copies of your object. - final john = jane.copyWith(name: 'John'); + final sparky = dash.copyWith(name: () => 'Sparky'); // ✨ Human-readable string representation. - print(jane); // Person(name: Jane, age: 42) - print(john); // Person(name: John, age: 42) + print(dash); // Person(name: Dash) + print(sparky); // Person(name: Sparky) // ☯️ Value equality comparisons. - print(jane == jane.copyWith()); // true - print(john == john.copyWith(age: 21)); // false + print(dash == dash.copyWith()); // true + print(dash == sparky); // false } ``` diff --git a/assets/demo.mp4 b/assets/demo.mp4 deleted file mode 100644 index 34893d2..0000000 Binary files a/assets/demo.mp4 and /dev/null differ diff --git a/example/main.dart b/example/main.dart index 7c0d96a..3eb96cd 100644 --- a/example/main.dart +++ b/example/main.dart @@ -6,21 +6,20 @@ import 'package:struct_annotation/struct_annotation.dart'; @Struct() class Person { final String name; - final int age; } void main() { // 🪨 Create a const instance with required, name parameters. - const jane = Person(name: 'Jane', age: 42); - + const dash = Person(name: 'Dash'); + // 🖨️ Create copies of your object. - final john = jane.copyWith(name: 'John'); + final sparky = dash.copyWith(name: () => 'Sparky'); // ✨ Human-readable string representation. - print(jane); // Person(name: Jane, age: 42) - print(john); // Person(name: John, age: 42) + print(dash); // Person(name: Dash) + print(sparky); // Person(name: Sparky) // ☯️ Value equality comparisons. - print(jane == jane.copyWith()); // true - print(john == john.copyWith(age: 21)); // false + print(dash == dash.copyWith()); // true + print(dash == sparky); // false } diff --git a/lib/src/struct_annotation.dart b/lib/src/struct_annotation.dart index 2eb70ca..7ca465c 100644 --- a/lib/src/struct_annotation.dart +++ b/lib/src/struct_annotation.dart @@ -24,7 +24,7 @@ import 'package:macros/macros.dart'; /// print(dash); // Person(name: Dash) /// /// // Generated `copyWith` -/// print(dash.copyWith(name: 'Sparky')); // Person(name: Sparky) +/// print(dash.copyWith(name: () => 'Sparky')); // Person(name: Sparky) /// /// // Generated `hashCode` and `operator==` for value based equality. /// print(dash == Person(name: 'Dash')); // true @@ -153,7 +153,7 @@ macro class Struct with _Shared implements ClassDeclarationsMacro, ClassDefiniti [ 'external ${clazz.identifier.name} copyWith({', for (final field in fields) - ...[field.type!.identifier.name, '? ', field.identifier.name, ','] + ...[field.type!.identifier.name, if(field.type!.isNullable) '?', ' Function()? ', field.identifier.name, ','] ,'});', ], ), @@ -330,7 +330,7 @@ macro class Struct with _Shared implements ClassDeclarationsMacro, ClassDefiniti clazzName, '(', for (final field in fields) - ...[field.identifier.name,': ', field.identifier.name, ' ?? ', 'this.',field.identifier.name, ','], + ...[field.identifier.name, ': ', field.identifier.name, '!= null ? ',field.identifier.name, '.call()', ' : this.',field.identifier.name, ','], ');' ], ), diff --git a/test/src/struct_annotation_test.dart b/test/src/struct_annotation_test.dart index da75a0b..e1158c9 100644 --- a/test/src/struct_annotation_test.dart +++ b/test/src/struct_annotation_test.dart @@ -25,7 +25,10 @@ void main() { }); test('hashCode', () { - expect(const EmptyClass().hashCode, equals(const EmptyClass().hashCode)); + expect( + const EmptyClass().hashCode, + equals(const EmptyClass().hashCode), + ); }); test('operator==', () { @@ -53,7 +56,7 @@ void main() { test('copyWith', () { expect(instance.copyWith(), equals(instance)); - final copy = instance.copyWith(value: 'bye'); + final copy = instance.copyWith(value: () => 'bye'); expect(copy, isNot(equals(instance))); expect(copy.value, equals('bye')); }); @@ -102,7 +105,21 @@ void main() { test('copyWith', () { expect(nullInstance.copyWith(), equals(nullInstance)); expect(nonNullInstance.copyWith(), equals(nonNullInstance)); - final copy = nonNullInstance.copyWith(value: 'bye'); + expect(nullInstance.copyWith(value: null), equals(nullInstance)); + expect(nonNullInstance.copyWith(value: null), equals(nonNullInstance)); + expect( + nullInstance.copyWith(value: () => 'hello'), + isA().having( + (e) => e.value, + 'value', + 'hello', + ), + ); + expect( + nonNullInstance.copyWith(value: () => null), + isA().having((e) => e.value, 'value', isNull), + ); + final copy = nonNullInstance.copyWith(value: () => 'bye'); expect(copy, isNot(equals(nonNullInstance))); expect(copy.value, equals('bye')); }); @@ -112,8 +129,11 @@ void main() { nonNullInstance.hashCode, equals(Object.hashAll([nonNullInstance.value])), ); - - expect(nullInstance.hashCode, equals(Object.hashAll([nullInstance.value]))); + + expect( + nullInstance.hashCode, + equals(Object.hashAll([nullInstance.value])), + ); }); test('operator==', () {