atreeon / morphy

Provides a clean class definition with extra functionality including; copy with, json serializable, tostring, equals that supports inheritance and polymorphism
3 stars 3 forks source link

How to generate changeTo_Subclass method on superclass #3

Closed burhankhanzada closed 5 months ago

burhankhanzada commented 5 months ago

How to generate changeTo_Subclass method on superclass as described at https://pub.dev/packages/morphy#changeto

human.dart


import 'package:morphy_annotation/morphy_annotation.dart';

part 'human.morphy.dart';

@morphy abstract class $Human { String get name; int get age; }


> student.dart
```dart
import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:morphy_test/human.dart';

part 'student.morphy.dart';

@morphy
abstract class $Student implements $Human {
  String get rollNo;
}

human.morphy.dart


// ignore_for_file: UNNECESSARY_CAST
// ignore_for_file: unused_element

part of 'human.dart';

// ** // Generator: MorphyGenerator // **

/// class Human extends $Human { final String name; final int age;

/// Human({ required this.name, required this.age, }); Human._({ required this.name, required this.age, }); String toString() => "(Human-name:${name.toString()}|age:${age.toString()})"; int get hashCode => hashObjects([name.hashCode, age.hashCode]); bool operator ==(Object other) => identical(this, other) || other is Human && runtimeType == other.runtimeType && name == other.name && age == other.age; Human copyWithHuman({ String Function()? name, int Function()? age, }) { return Human.( name: name == null ? this.name as String : name() as String, age: age == null ? this.age as int : age() as int, ); } }

extension $Human_changeTo_E on $Human { Human changeToHuman({ String Function()? name, int Function()? age, }) { return Human.( name: name == null ? this.name as String : name() as String, age: age == null ? this.age as int : age() as int, ); } }

enum Human$ { name, age }



As your can in generated code there is no `changeTo_Student` method
burhankhanzada commented 5 months ago

Get it work after looking at source code example/read_me.dart file we need to add option explicitSubTypes in morphy annotation.

import 'package:morphy_annotation/morphy_annotation.dart';
import 'package:morphy_test/student.dart';

part 'human.g.dart';
part 'human.morphy.dart';

@Morphy(generateJson: true, explicitSubTypes: [$Student])
abstract class $Human {
  String get name;
  int get age;
}