|
| 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 | + |
| 57 | +class StringAnnotation extends Annotation { |
| 58 | + final String name; |
| 59 | + |
| 60 | + StringAnnotation({required this.name}); |
| 61 | + |
| 62 | + @override |
| 63 | + String toAnnotation() => name; |
| 64 | + |
| 65 | + @override |
| 66 | + Map<String, Object?> get namedProps => { |
| 67 | + 'name': name, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +class OverrideAnnotation extends StringAnnotation { |
| 72 | + OverrideAnnotation() : super(name: 'override'); |
| 73 | +} |
| 74 | + |
| 75 | +class JsonKeyAnnotation extends Annotation { |
| 76 | + final JsonKeyItem jsonKey; |
| 77 | + |
| 78 | + JsonKeyAnnotation({required this.jsonKey}); |
| 79 | + |
| 80 | + factory JsonKeyAnnotation.fromMap(Map<String, dynamic> jsonKeyAnnotation) { |
| 81 | + return JsonKeyAnnotation( |
| 82 | + jsonKey: JsonKeyItem( |
| 83 | + defaultValue: jsonKeyAnnotation['defaultValue'] as String?, |
| 84 | + disallowNullValue: jsonKeyAnnotation['disallowNullValue'] as String?, |
| 85 | + fromJson: jsonKeyAnnotation['fromJson'] as String?, |
| 86 | + ignore: jsonKeyAnnotation['ignore'] as String?, |
| 87 | + includeIfNull: jsonKeyAnnotation['includeIfNull'] as String?, |
| 88 | + name: jsonKeyAnnotation['name'] as String?, |
| 89 | + required: jsonKeyAnnotation['required'] as String?, |
| 90 | + toJson: jsonKeyAnnotation['toJson'] as String?, |
| 91 | + unknownEnumValue: jsonKeyAnnotation['unknownEnumValue'] as String?, |
| 92 | + ), |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + @override |
| 97 | + String toAnnotation() { |
| 98 | + final jsonKeyAnnotation = <String, dynamic>{ |
| 99 | + 'disallowNullValue': jsonKey.disallowNullValue, |
| 100 | + 'fromJson': jsonKey.fromJson, |
| 101 | + 'ignore': jsonKey.ignore, |
| 102 | + 'includeIfNull': jsonKey.includeIfNull, |
| 103 | + 'name': jsonKey.name, |
| 104 | + 'required': jsonKey.required, |
| 105 | + 'toJson': jsonKey.toJson, |
| 106 | + 'unknownEnumValue': jsonKey.unknownEnumValue |
| 107 | + }; |
| 108 | + |
| 109 | + final key = jsonKeyAnnotation.entries |
| 110 | + .map<String?>((e) { |
| 111 | + if (e.value != null) { |
| 112 | + switch (e.key) { |
| 113 | + case 'name': |
| 114 | + return '${e.key}: \'${e.value}\''; |
| 115 | + default: |
| 116 | + return '${e.key}: ${e.value}'; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return null; |
| 121 | + }) |
| 122 | + .whereType<String>() |
| 123 | + .join(', '); |
| 124 | + |
| 125 | + return 'JsonKey($key)'; |
| 126 | + } |
| 127 | + |
| 128 | + @override |
| 129 | + Map<String, Object?> get namedProps { |
| 130 | + return { |
| 131 | + 'jsonKey': jsonKey, |
| 132 | + }; |
| 133 | + } |
| 134 | +} |
0 commit comments