Line data Source code
1 : import 'package:analyzer/dart/element/element.dart'; 2 : import 'package:enum_assist/src/configs/class_config.dart'; 3 : import 'package:enum_assist/src/configs/extension_value_config.dart'; 4 : import 'package:enum_assist/src/src.dart'; 5 : import 'package:enum_assist_annotation/enum_assist_annotation.dart'; 6 : import 'package:meta/meta.dart'; 7 : 8 : /// {@template enum_assist.enum_field} 9 : /// A field & all its associated metadata. 10 : /// {@endtemplate} 11 : class EnumField extends KeyConfig { 12 : /// {@macro enum_assist.enum_field} 13 0 : EnumField._( 14 : this.belongsToEnum, 15 : this.fieldName, { 16 : required this.serializedFormat, 17 : required this.docComment, 18 : required KeyConfig config, 19 0 : }) : super.fromSettings(config); 20 : 21 : /// {@macro enum_assist.enum_field} 22 0 : factory EnumField.config(FieldElement e, ClassConfig con) => EnumField._( 23 0 : e.type.element!.name!, 24 0 : e.name, 25 0 : serializedFormat: con.serializedFormat, 26 0 : docComment: e.documentationComment, 27 0 : config: KeyConfig.mergeConfigs(con, e), 28 : ); 29 : 30 : /// used for testing 31 1 : @protected 32 : @visibleForTesting 33 : EnumField.manual({ 34 : this.belongsToEnum = 'MyEnum', 35 : this.fieldName = 'myField', 36 : this.serializedFormat = SerializedFormat.none, 37 : this.docComment, 38 : KeyConfig config = KeyConfig.defaults, 39 1 : }) : super.fromSettings(config); 40 : 41 : /// the name of the enum without formatting 42 : final String fieldName; 43 : 44 : /// the documentation associated with this field 45 : final String? docComment; 46 : 47 : /// the name of the enum the field is in 48 : final String belongsToEnum; 49 : 50 : /// the name of the field as a private member 51 3 : String get privateName => '_${fieldName}Name'; 52 : 53 : /// return the name of the enum & field as a string 54 : /// 55 : /// e.g. `MyEnum.myField` 56 4 : String get wholeName => '$belongsToEnum.$fieldName'; 57 : 58 : /// {@macro enum_assist_annotation.serialized_format} 59 : final SerializedFormat serializedFormat; 60 : 61 : /// gets the serialized form of the field's name 62 : /// 63 : /// _prioritizes the [serializedValue]_ 64 1 : String get getSerializedName { 65 2 : if (serializedValue != null) return serializedValue!; 66 : 67 2 : return _format(fieldName); 68 : } 69 : 70 : /// get the description of the enum value 71 : /// 72 : /// _prioritizes by order:_ 73 : /// - [description] 74 : /// - [docComment] 75 1 : String? get getDescription { 76 2 : if (description != null) return description; 77 : 78 1 : if (useDocCommentAsDescription) { 79 3 : return docComment?.replaceAll(RegExp('///(?: *)'), ''); 80 : } 81 : return null; 82 : } 83 : 84 : /// retrieves the name 85 : /// 86 : /// _prioritizes name from [EnumKey]_ 87 : /// 88 : /// if name is not provided, returns the field name 89 : /// formatted to Capital case 90 1 : String get getName { 91 2 : if (name != null) return name!; 92 : 93 2 : return fieldName.toCapitalCase(); 94 : } 95 : 96 : /// returns the config for the extension 97 1 : ExtensionValueConfig? getExtensionValue(String valueClassName) { 98 2 : if (extensionValues.isEmpty) return null; 99 : 100 : final index = 101 5 : extensionValues.indexWhere((e) => e.valueClassName == valueClassName); 102 2 : if (index == -1) return null; 103 : 104 2 : return extensionValues[index]; 105 : } 106 : 107 1 : @override 108 1 : String toString() => fieldName; 109 : 110 1 : String _format(String s) { 111 2 : return serializedFormat.map( 112 1 : kebab: s.toKebabCase(), 113 1 : snake: s.toSnakeCase(), 114 1 : pascal: s.toPascalCase(), 115 1 : camel: s.toCamelCase(), 116 1 : dot: s.toDotCase(), 117 1 : capital: s.toCapitalCase(), 118 1 : constant: s.toConstantCase(), 119 1 : header: s.toHeaderCase(), 120 1 : no: s.toNoCase(), 121 1 : path: s.toPathCase(), 122 1 : sentence: s.toSentenceCase(), 123 1 : swap: s.toSwapCase(), 124 : none: s, 125 : ); 126 : } 127 : }