Line data Source code
1 : import 'package:enum_assist/src/util/util.dart'; 2 : import 'package:source_gen/source_gen.dart'; 3 : 4 : /// {@template enum_assist.extension_value_config} 5 : /// The configuration for the additional extension. 6 : /// {@endtemplate} 7 : class ExtensionValueConfig { 8 : /// {@macro enum_assist.extension_value_config} 9 0 : const ExtensionValueConfig._({ 10 : required this.valueClassName, 11 : required this.valueType, 12 : required this.value, 13 : }); 14 : 15 : /// The name of the class that represents the value. 16 : final String valueClassName; 17 : 18 : /// The type of the value. 19 : final String valueType; 20 : 21 : /// The value. 22 : final String value; 23 : 24 : /// resolve the [ExtensionValueConfig] from a `ConstantReader`. 25 0 : static ExtensionValueConfig resolve(ConstantReader reader) { 26 : const valueClassNameKey = 'valueClassName'; 27 : const valueTypeKey = 'valueType'; 28 : const valueKey = 'value'; 29 : 30 0 : final valueClassName = reader.objectValue.type?.element?.displayName; 31 : 32 0 : final classDetails = '${reader.objectValue}'; 33 : 34 0 : final match = RegExp(r'(?<![\w])ExtensionValue<.*?>(?= \()') 35 0 : .allMatches(classDetails) 36 : // get the lowest match in class heirarchy 37 0 : .last 38 0 : .group(0); 39 0 : if (match == null) throw _missingValueException(valueTypeKey); 40 : 41 0 : final typeWithBrackets = match.replaceAll('ExtensionValue', ''); 42 : final valueType = 43 0 : typeWithBrackets.substring(1, typeWithBrackets.length - 1); 44 : 45 0 : final value = reader.peek(valueKey)?.stringValue; 46 : 47 0 : if (valueClassName == null) throw _missingValueException(valueClassNameKey); 48 0 : if (value == null) throw _missingValueException(valueKey); 49 : 50 0 : return ExtensionValueConfig._( 51 : valueClassName: valueClassName, 52 : valueType: valueType, 53 : value: value, 54 : ); 55 : } 56 : 57 0 : static MissingValueException<String> _missingValueException(String key) => 58 0 : MissingValueException('The value for the key "$key" is missing.'); 59 : 60 0 : @override 61 : String toString() => ''' 62 0 : valueClassName: $valueClassName, 63 0 : valueType: $valueType, 64 0 : value: $value) 65 0 : '''; 66 : }