Line data Source code
1 : import 'package:analyzer/dart/constant/value.dart'; 2 : import 'package:analyzer/dart/element/element.dart'; 3 : import 'package:enum_assist/src/configs/class_config.dart'; 4 : import 'package:enum_assist/src/configs/configs.dart'; 5 : import 'package:enum_assist/src/configs/extension_value_config.dart'; 6 : import 'package:enum_assist_annotation/enum_assist_annotation.dart'; 7 : import 'package:source_gen/source_gen.dart'; 8 : 9 : /// {@template enum_assist.key_config} 10 : /// Represents values from the [EnumKey] annotation. 11 : /// {@endtemplate} 12 : class KeyConfig { 13 : /// {@macro enum_assist.key_config} 14 4 : const KeyConfig({ 15 : required this.description, 16 : required this.name, 17 : required this.serializedValue, 18 : required this.useDocCommentAsDescription, 19 : required this.extensionValues, 20 : }); 21 : 22 : /// {@macro enum_assist.key_config} 23 1 : KeyConfig.fromSettings(KeyConfig settings) 24 1 : : this( 25 1 : description: settings.description, 26 1 : name: settings.name, 27 1 : serializedValue: settings.serializedValue, 28 1 : useDocCommentAsDescription: settings.useDocCommentAsDescription, 29 1 : extensionValues: settings.extensionValues, 30 : ); 31 : 32 : /// Constructs the key configs from the [EnumKey] annotation. 33 : /// and the class annotation [ClassConfig]. 34 : /// 35 : /// {@macro enum_assist.key_config} 36 0 : factory KeyConfig.mergeConfigs( 37 : ClassConfig classConfig, 38 : FieldElement element, 39 : ) { 40 : const nameKey = 'name'; 41 : const descriptionKey = 'description'; 42 : const serializedValueKey = 'serializedValue'; 43 : const useDocCommentAsDescriptionKey = 'useDocCommentAsDescription'; 44 : const extensionValuesKey = 'extensionValues'; 45 : 46 : DartObject? object; 47 0 : object = _enumKeyChecker.firstAnnotationOf(element); 48 : 49 : if (object == null) { 50 0 : if (element.getter != null) { 51 0 : object = _enumKeyChecker.firstAnnotationOf(element.getter!); 52 : } 53 : } 54 : 55 : if (object == null) { 56 0 : return defaults.copyWith( 57 0 : useDocCommentAsDescription: classConfig.useDocCommentAsDescription, 58 : ); 59 : } 60 0 : final reader = ConstantReader(object); 61 : 62 0 : final name = reader.peek(nameKey)?.stringValue; 63 : 64 0 : final description = reader.peek(descriptionKey)?.stringValue; 65 : 66 0 : final serializedValue = reader.peek(serializedValueKey)?.stringValue; 67 : 68 : final useDocCommentAsDescription = 69 0 : reader.peek(useDocCommentAsDescriptionKey)?.boolValue; 70 : 71 0 : final extensionValuesRaw = reader.peek(extensionValuesKey)?.listValue; 72 : 73 0 : final extensionValues = <ExtensionValueConfig>[]; 74 : 75 : if (extensionValuesRaw != null) { 76 0 : for (final entry in extensionValuesRaw) { 77 0 : final reader = ConstantReader(entry); 78 0 : final config = ExtensionValueConfig.resolve(reader); 79 0 : extensionValues.add(config); 80 : } 81 : } 82 : 83 : // ignore: avoid_print 84 0 : print('class config uses doc comments? ' 85 0 : '${classConfig.useDocCommentAsDescription}'); 86 0 : return KeyConfig( 87 : name: name, 88 : description: description, 89 : serializedValue: serializedValue, 90 : useDocCommentAsDescription: 91 0 : useDocCommentAsDescription ?? classConfig.useDocCommentAsDescription, 92 : extensionValues: extensionValues, 93 : ); 94 : } 95 : 96 : /// {@macro enum_assist_annotation.enum_key.description} 97 : final String? description; 98 : 99 : /// {@macro enum_assist_annotation.enum_key.name} 100 : final String? name; 101 : 102 : /// {@macro enum_assist_annotation.enum_key.serialized_value} 103 : final String? serializedValue; 104 : 105 : /// {@macro enum_assist_annotation.enum_key.use_doc_comment_as_description} 106 : final bool useDocCommentAsDescription; 107 : 108 : /// {@macro enum_assist_annotation.enum_key.extension_values} 109 : final List<ExtensionValueConfig> extensionValues; 110 : 111 : /// all the default values of [EnumKey] 112 : static const defaults = KeyConfig( 113 : name: null, 114 : description: null, 115 : serializedValue: null, 116 : useDocCommentAsDescription: true, 117 : extensionValues: [], 118 : ); 119 : 120 : static const _enumKeyChecker = TypeChecker.fromRuntime(EnumKey); 121 : 122 : /// @nodoc 123 1 : KeyConfig copyWith({ 124 : String? description, 125 : String? name, 126 : String? serializedValue, 127 : bool? useDocCommentAsDescription, 128 : List<ExtensionValueConfig>? extensionValues, 129 : }) { 130 1 : return KeyConfig( 131 1 : description: description ?? this.description, 132 1 : name: name ?? this.name, 133 1 : serializedValue: serializedValue ?? this.serializedValue, 134 : useDocCommentAsDescription: 135 1 : useDocCommentAsDescription ?? this.useDocCommentAsDescription, 136 1 : extensionValues: extensionValues ?? this.extensionValues, 137 : ); 138 : } 139 : }