patch.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:json_annotation/json_annotation.dart';
  2. part 'patch.g.dart';
  3. @JsonSerializable()
  4. class Patch {
  5. Patch({
  6. required this.name,
  7. required this.description,
  8. required this.excluded,
  9. required this.compatiblePackages,
  10. required this.options,
  11. });
  12. factory Patch.fromJson(Map<String, dynamic> json) {
  13. _migrateV16ToV17(json);
  14. return _$PatchFromJson(json);
  15. }
  16. static void _migrateV16ToV17(Map<String, dynamic> json) {
  17. if (json['options'] == null) {
  18. json['options'] = [];
  19. }
  20. }
  21. final String name;
  22. final String? description;
  23. final bool excluded;
  24. final List<Package> compatiblePackages;
  25. final List<Option> options;
  26. Map<String, dynamic> toJson() => _$PatchToJson(this);
  27. String getSimpleName() {
  28. return name;
  29. }
  30. }
  31. @JsonSerializable()
  32. class Package {
  33. Package({
  34. required this.name,
  35. required this.versions,
  36. });
  37. factory Package.fromJson(Map<String, dynamic> json) =>
  38. _$PackageFromJson(json);
  39. final String name;
  40. final List<String> versions;
  41. Map toJson() => _$PackageToJson(this);
  42. }
  43. @JsonSerializable()
  44. class Option {
  45. Option({
  46. required this.key,
  47. required this.title,
  48. required this.description,
  49. required this.value,
  50. required this.values,
  51. required this.required,
  52. required this.type,
  53. });
  54. factory Option.fromJson(Map<String, dynamic> json) {
  55. _migrateV17ToV19(json);
  56. _migrateV19ToV20(json);
  57. return _$OptionFromJson(json);
  58. }
  59. static void _migrateV17ToV19(Map<String, dynamic> json) {
  60. if (json['valueType'] == null) {
  61. final type = json['optionClassType'];
  62. if (type is String) {
  63. json['valueType'] =
  64. type.replaceAll('PatchOption', '').replaceAll('List', 'Array');
  65. json['optionClassType'] = null;
  66. }
  67. }
  68. }
  69. static void _migrateV19ToV20(Map<String, dynamic> json) {
  70. if (json['valueType'] != null) {
  71. final String type = json['valueType'];
  72. json['type'] = type.endsWith('Array')
  73. ? 'kotlin.collections.List<kotlin.${type.replaceAll('Array', '')}>'
  74. : 'kotlin.$type';
  75. json['valueType'] = null;
  76. }
  77. }
  78. final String key;
  79. final String title;
  80. final String description;
  81. final dynamic value;
  82. final Map<String, dynamic>? values;
  83. final bool required;
  84. final String type;
  85. Map toJson() => _$OptionToJson(this);
  86. }