Skip to content

Commit

Permalink
1.3.0 see CHANGELOG.md for changes
Browse files Browse the repository at this point in the history
  • Loading branch information
atreeon committed Aug 22, 2024
1 parent 635aaf1 commit 29c6a8a
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 100 deletions.
7 changes: 4 additions & 3 deletions example/test/ex11_generic_subclass_manual_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import 'package:test/test.dart';

abstract class A<T1, T2> {
T1 get x;

T2 get y;

A copyWith_A<T1, T2>({
A<T1, T2> copyWith_A<T1, T2>({
Opt<T1>? x,
Opt<T2>? y,
});
Expand All @@ -26,15 +27,15 @@ class B implements A<int, String> {

String toString() => "(B-x:$x|y:$y|z:$z)";

B copyWith_A<T1, T2>({
A<T1, T2> copyWith_A<T1, T2>({
Opt<T1>? x,
Opt<T2>? y,
}) {
return B(
x: x == null ? this.x as int : x.value as int,
y: y == null ? this.y as String : y.value as String,
z: (this as B).z,
);
) as A<T1, T2>;
}

B copyWith_B({
Expand Down
2 changes: 1 addition & 1 deletion example/test/ex29_copywith_subclasses_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ main() {

test("db", () {
D db = D(b: 5, a: "A");
var db_copy = db.copyWith_B(a: () => "a", b: () => 6);
var db_copy = db.copyWith_B(a: () => "a", b: () => 6) as D;
expect(db_copy.toString(), "(D-a:a|b:6)");
});

Expand Down
12 changes: 4 additions & 8 deletions example/test/ex60_copywith_generic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ part 'ex60_copywith_generic_test.morphy.dart';

main() {
test("1", () {
// var a = A<int>(x:1, y:2);
//
// var aAsInt = a.copyWith_A<int>(x: () => 2);
//
// expect(aAsInt.runtimeType, A<int>);
var a = A<int>(x:1, y:2);

// var aAsDouble = a.copyWith_A(x: () => 2.1);
//
// expect(aAsDouble.runtimeType, 2.1);
var aAsInt = a.copyWith_A<int>(x: () => 2);

expect(aAsInt.runtimeType, A<int>);
});
}

Expand Down
30 changes: 30 additions & 0 deletions example/test/ex63_class_with_no_members_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:test/test.dart';

part 'ex63_class_with_no_members_test.morphy.dart';

@Morphy(
explicitSubTypes: [
$AgreedEulaState,
]
)
abstract class $EulaState
{
// lack of members
}

@Morphy()
abstract class $AgreedEulaState implements $EulaState
{
bool get test;
}

main() {
test("1", () {
var a = EulaState();
var b = AgreedEulaState(test: true);

expect(a == null, false);
expect(b.test, true);
});
}
7 changes: 7 additions & 0 deletions morphy/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.3.0
- bug fixes, subclass with no members now works, generic copy with bug now fixed where type is incorrect
- in order to fix the above problem the type returned from the copywith is now the type of the named copywith function, eg b.copyWith_A now returns an A type
- this is a BREAKING change.
- if you specify D newD = d.copyWith_A(); then newD will be of type D not A and you'll receive an error
- the thing is that if you know it is a d type you'd more likely do a d.copyWith_D() so this should not be a problem

## 1.2.0
- New functionality - private getters are now allowed!

Expand Down
58 changes: 44 additions & 14 deletions morphy/lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,43 @@ String getCopyWith({
var classNameTrimmed = className.replaceAll("\$", "");
var interfaceNameTrimmed = interfaceName.replaceAll("\$", "");

isExplicitSubType //
? sb.write("$interfaceNameTrimmed changeTo_${interfaceNameTrimmed}")
: sb.write("$classNameTrimmed copyWith_$interfaceNameTrimmed");

if (interfaceGenerics.isNotEmpty) {
var generic = interfaceGenerics //
.map((e) => e.type == null //
? e.name
: "${e.name} extends ${e.type}")
.joinToString(separator: ", ");
sb.write("<$generic>");
// var interfaceGenericString = interfaceGenerics //
// .map((e) => e.type == null //
// ? e.name
// : "${e.name} extends ${e.type}")
// .joinToString(separator: ", ");

var interfaceGenericStringWithExtends = interfaceGenerics //
.map((e) => e.type == null //
? e.name
: "${e.name} extends ${e.type}")
.joinToString(separator: ", ");

if (interfaceGenericStringWithExtends.length > 0) {
interfaceGenericStringWithExtends = "<$interfaceGenericStringWithExtends>";
}

var interfaceGenericStringNoExtends = interfaceGenerics //
.map((e) => e.name)
.joinToString(separator: ", ");

if (interfaceGenericStringNoExtends.length > 0) {
interfaceGenericStringNoExtends = "<$interfaceGenericStringNoExtends>";
}

isExplicitSubType //
? sb.write("$interfaceNameTrimmed$interfaceGenericStringNoExtends changeTo_$interfaceNameTrimmed$interfaceGenericStringWithExtends")
: sb.write("$interfaceNameTrimmed$interfaceGenericStringNoExtends copyWith_$interfaceNameTrimmed$interfaceGenericStringWithExtends");

// if (interfaceGenerics.isNotEmpty) {
// var generic = interfaceGenerics //
// .map((e) => e.type == null //
// ? e.name
// : "${e.name} extends ${e.type}")
// .joinToString(separator: ", ");
// sb.write("<$generic>");
// }

sb.write("(");

//where property name of interface is the same as the one in the class
Expand Down Expand Up @@ -315,7 +339,7 @@ String getCopyWith({
return "${getDataTypeWithoutDollars(interfaceType!)} Function()? $name,\n";
}).join());

if (fieldsForSignature.isNotEmpty) //
if (fieldsForSignature.isNotEmpty|| requiredFields.isNotEmpty) //
sb.write("}");

if (isClassAbstract && !isExplicitSubType) {
Expand Down Expand Up @@ -350,10 +374,16 @@ String getCopyWith({
.where((element) => !interfaceFields.map((e) => e.name).contains(element.name));

sb.write(fieldsNotInSignature //
.map((e) => "${e.name.startsWith('_') ? e.name.substring(1) : e.name }: (this as $classNameTrimmed).${e.name},\n")
.map((e) => "${e.name.startsWith('_') ? e.name.substring(1) : e.name}: (this as $classNameTrimmed).${e.name},\n")
.join());

sb.writeln(");");
sb.write(") as $interfaceNameTrimmed$interfaceGenericStringNoExtends;");

// if (isExplicitSubType) {
// sb.write(") as $interfaceNameTrimmed;");
// } else {
// sb.write(") as $interfaceNameTrimmed$interfaceGenericStringNoExtends;");
// }
sb.write("}");

return sb.toString();
Expand Down
2 changes: 1 addition & 1 deletion morphy/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: morphy
description: Provides a clean class definition with extra functionality including; copy with, json serializable, tostring, equals that supports inheritance and polymorphism
version: 1.2.0
version: 1.3.0
homepage: https://github.com/atreeon/morphy

environment:
Expand Down
Loading

0 comments on commit 29c6a8a

Please sign in to comment.