Line data Source code
1 : import 'package:enum_assist/src/enum_field.dart'; 2 : import 'package:enum_assist/src/templates/template_core.dart'; 3 : import 'package:enum_assist/src/util/util.dart'; 4 : import 'package:enum_assist_annotation/enum_assist_annotation.dart'; 5 : import 'package:meta/meta.dart'; 6 : 7 : class _Item extends FieldTemplate<EnumField> { 8 0 : const _Item(String enumName, EnumField field) : super(enumName, field); 9 : 10 0 : String returnString<T>(T value) => '$field: $value,'; 11 : } 12 : 13 : /// {@template enum_assist.additional_template} 14 : /// Helper class to create [ExtensionTemplate]s 15 : /// {@endtemplate} 16 : abstract class ExtensionTemplate extends TemplateCoreDetailed<_Item> { 17 : /// {@macro enum_assist.additional_template} 18 0 : ExtensionTemplate( 19 : String enumName, 20 : Iterable<EnumField> fields, { 21 : required this.defaultValue, 22 : required this.methodName, 23 : required this.returnValue, 24 : required this.methodType, 25 : required this.typeAsString, 26 : required this.docComment, 27 0 : }) : super(enumName, fields); 28 : 29 : /// The name of the method to call 30 : @protected 31 : final String methodName; 32 : 33 : /// the value to be returned by the enum value 34 : @protected 35 : final String? Function(EnumField) returnValue; 36 : 37 : /// the access method that will be used 38 : /// 39 : /// Options: 40 : /// - `map` 41 : /// - `maybeMap` 42 : @protected 43 : final MethodType methodType; 44 : 45 : /// the default value to be returned by the enum value 46 : @protected 47 : final String? defaultValue; 48 : 49 : /// the return type of the method 50 : /// 51 : /// (e.g. `'$String'` or `'String'`) 52 : @protected 53 : final String typeAsString; 54 : 55 : /// the doc comment for the method 56 : @protected 57 : final String docComment; 58 : 59 0 : String get _getAccess => methodType.map(maybeMap: 'maybeMap', map: 'map'); 60 : 61 0 : bool get _useOrElse => methodType.map(maybeMap: true, map: false); 62 : 63 0 : @protected 64 : @nonVirtual 65 : @override 66 : StringBuffer writeTemplate(StringBuffer buffer) { 67 0 : String? _checkValueAndPrepare(String? value, EnumField field) { 68 0 : return prepValueForGen(value); 69 : } 70 : 71 : buffer 72 0 : ..writeln(docComment) 73 0 : ..writeobj( 74 0 : '$typeAsString get $methodName', 75 0 : body: (nameBuffer, tab) { 76 0 : nameBuffer.writeobj( 77 0 : 'return $_getAccess', 78 : tab: tab, 79 : open: '(', 80 : close: ');', 81 0 : body: (mapBuffer, mapTab) { 82 0 : if (_useOrElse) { 83 0 : mapBuffer.writeln('orElse: $defaultValue,'); 84 : } 85 0 : mapBuffer.writelnTab( 86 0 : map((i) { 87 0 : final value = returnValue(i.field); 88 0 : final preparedValue = _checkValueAndPrepare(value, i.field); 89 : 90 0 : if (!_isTypeNullable && preparedValue == null) { 91 0 : throw NullValueException(i.field.fieldName); 92 : } 93 : 94 0 : return tabn(i.returnString(preparedValue), tab); 95 : }), 96 : mapTab, 97 : ); 98 : }, 99 : ); 100 : }, 101 : ); 102 : 103 : return buffer; 104 : } 105 : 106 0 : @override 107 : @protected 108 0 : _Item convert(EnumField e) => _Item(enumName, e); 109 : 110 : /// prepares the value for the generation 111 : /// 112 : /// if type is `String`, it will be wrapped in quotes 113 0 : String? prepValueForGen(String? value) { 114 0 : final formattedValue = value?.replaceAll(RegExp('\n'), r'\n'); 115 : 116 0 : if (typeAsString.startsWith('String') && formattedValue != null) { 117 0 : return "'$formattedValue'"; 118 : } 119 : return formattedValue; 120 : } 121 : 122 0 : bool get _isTypeNullable => isTypeAsStringNullable(typeAsString); 123 : }