|
| 1 | +import 'package:artemis/generator/data_printer.dart'; |
| 2 | +import 'package:artemis/generator/data/definition.dart'; |
| 3 | +import 'package:equatable/equatable.dart'; |
| 4 | +import 'package:json_annotation/json_annotation.dart'; |
| 5 | + |
| 6 | +abstract class Annotation extends Equatable with DataPrinter { |
| 7 | + String toAnnotation(); |
| 8 | +} |
| 9 | + |
| 10 | +class JsonKeyItem extends Equatable with DataPrinter { |
| 11 | + final String? defaultValue; |
| 12 | + |
| 13 | + final String? disallowNullValue; |
| 14 | + |
| 15 | + final String? fromJson; |
| 16 | + |
| 17 | + final String? ignore; |
| 18 | + |
| 19 | + final String? includeIfNull; |
| 20 | + |
| 21 | + final String? name; |
| 22 | + |
| 23 | + final String? required; |
| 24 | + |
| 25 | + final String? toJson; |
| 26 | + |
| 27 | + final String? unknownEnumValue; |
| 28 | + |
| 29 | + const JsonKeyItem({ |
| 30 | + this.defaultValue, |
| 31 | + this.disallowNullValue, |
| 32 | + this.fromJson, |
| 33 | + this.ignore, |
| 34 | + this.includeIfNull, |
| 35 | + this.name, |
| 36 | + this.required, |
| 37 | + this.toJson, |
| 38 | + this.unknownEnumValue, |
| 39 | + }); |
| 40 | + |
| 41 | + @override |
| 42 | + Map<String, Object?> get namedProps { |
| 43 | + return { |
| 44 | + 'defaultValue': defaultValue, |
| 45 | + 'disallowNullValue': disallowNullValue, |
| 46 | + 'fromJson': fromJson, |
| 47 | + 'ignore': ignore, |
| 48 | + 'includeIfNull': includeIfNull, |
| 49 | + 'name': name, |
| 50 | + 'required': required, |
| 51 | + 'toJson': toJson, |
| 52 | + 'unknownEnumValue': unknownEnumValue, |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + JsonKeyItem copyWith({ |
| 57 | + String? defaultValue, |
| 58 | + String? disallowNullValue, |
| 59 | + String? fromJson, |
| 60 | + String? ignore, |
| 61 | + String? includeIfNull, |
| 62 | + String? name, |
| 63 | + String? required, |
| 64 | + String? toJson, |
| 65 | + String? unknownEnumValue, |
| 66 | + }) { |
| 67 | + return JsonKeyItem( |
| 68 | + defaultValue: defaultValue ?? this.defaultValue, |
| 69 | + disallowNullValue: disallowNullValue ?? this.disallowNullValue, |
| 70 | + fromJson: fromJson ?? this.fromJson, |
| 71 | + ignore: ignore ?? this.ignore, |
| 72 | + includeIfNull: includeIfNull ?? this.includeIfNull, |
| 73 | + name: name ?? this.name, |
| 74 | + required: required ?? this.required, |
| 75 | + toJson: toJson ?? this.toJson, |
| 76 | + unknownEnumValue: unknownEnumValue ?? this.unknownEnumValue, |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class StringAnnotation extends Annotation { |
| 82 | + final String name; |
| 83 | + |
| 84 | + StringAnnotation({required this.name}); |
| 85 | + |
| 86 | + @override |
| 87 | + String toAnnotation() => name; |
| 88 | + |
| 89 | + @override |
| 90 | + Map<String, Object?> get namedProps => { |
| 91 | + 'name': name, |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +class OverrideAnnotation extends StringAnnotation { |
| 96 | + OverrideAnnotation() : super(name: 'override'); |
| 97 | +} |
| 98 | + |
| 99 | +class JsonKeyAnnotation extends Annotation { |
| 100 | + final JsonKeyItem jsonKey; |
| 101 | + |
| 102 | + JsonKeyAnnotation({required this.jsonKey}); |
| 103 | + |
| 104 | + factory JsonKeyAnnotation.fromMap(Map<String, dynamic> jsonKeyAnnotation) { |
| 105 | + return JsonKeyAnnotation( |
| 106 | + jsonKey: JsonKeyItem( |
| 107 | + defaultValue: jsonKeyAnnotation['defaultValue'] as String?, |
| 108 | + disallowNullValue: jsonKeyAnnotation['disallowNullValue'] as String?, |
| 109 | + fromJson: jsonKeyAnnotation['fromJson'] as String?, |
| 110 | + ignore: jsonKeyAnnotation['ignore'] as String?, |
| 111 | + includeIfNull: jsonKeyAnnotation['includeIfNull'] as String?, |
| 112 | + name: jsonKeyAnnotation['name'] as String?, |
| 113 | + required: jsonKeyAnnotation['required'] as String?, |
| 114 | + toJson: jsonKeyAnnotation['toJson'] as String?, |
| 115 | + unknownEnumValue: jsonKeyAnnotation['unknownEnumValue'] as String?, |
| 116 | + ), |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + @override |
| 121 | + String toAnnotation() { |
| 122 | + final jsonKeyAnnotation = <String, dynamic>{ |
| 123 | + 'disallowNullValue': jsonKey.disallowNullValue, |
| 124 | + 'fromJson': jsonKey.fromJson, |
| 125 | + 'ignore': jsonKey.ignore, |
| 126 | + 'includeIfNull': jsonKey.includeIfNull, |
| 127 | + 'name': jsonKey.name, |
| 128 | + 'required': jsonKey.required, |
| 129 | + 'toJson': jsonKey.toJson, |
| 130 | + 'unknownEnumValue': jsonKey.unknownEnumValue |
| 131 | + }; |
| 132 | + |
| 133 | + final key = jsonKeyAnnotation.entries |
| 134 | + .map<String?>((e) { |
| 135 | + if (e.value != null) { |
| 136 | + switch (e.key) { |
| 137 | + case 'name': |
| 138 | + return '${e.key}: \'${e.value}\''; |
| 139 | + default: |
| 140 | + return '${e.key}: ${e.value}'; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + }) |
| 146 | + .whereType<String>() |
| 147 | + .join(', '); |
| 148 | + |
| 149 | + return 'JsonKey($key)'; |
| 150 | + } |
| 151 | + |
| 152 | + @override |
| 153 | + Map<String, Object?> get namedProps { |
| 154 | + return { |
| 155 | + 'jsonKey': jsonKey, |
| 156 | + }; |
| 157 | + } |
| 158 | +} |
0 commit comments