|
| 1 | +//.title |
| 2 | +// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 3 | +// |
| 4 | +// Dart/Flutter (DF) Packages by DevCetra.com & contributors. The use of this |
| 5 | +// source code is governed by an MIT-style license described in the LICENSE |
| 6 | +// file located in this project's root directory. |
| 7 | +// |
| 8 | +// See: https://opensource.org/license/mit |
| 9 | +// |
| 10 | +// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ |
| 11 | +//.title~ |
| 12 | + |
| 13 | +import 'package:df_string/df_string.dart' show StringCaseConversionsOnStringX; |
| 14 | + |
| 15 | +import '../models/field_model/field_model.dart'; |
| 16 | + |
| 17 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 18 | + |
| 19 | +final class DartField extends Field { |
| 20 | + // |
| 21 | + // |
| 22 | + // |
| 23 | + |
| 24 | + const DartField({ |
| 25 | + required super.fieldPath, |
| 26 | + required super.fieldType, |
| 27 | + super.nullable, |
| 28 | + super.children, |
| 29 | + super.primaryKey, |
| 30 | + super.foreignKey, |
| 31 | + super.fallback, |
| 32 | + super.description, |
| 33 | + }); |
| 34 | + |
| 35 | + /// Derives an instance [DartField] from [source]. |
| 36 | + factory DartField.from(Field source) { |
| 37 | + return DartField( |
| 38 | + fieldPath: source.fieldPath, |
| 39 | + fieldType: source.fieldType, |
| 40 | + nullable: source.nullable, |
| 41 | + children: source.children, |
| 42 | + primaryKey: source.primaryKey, |
| 43 | + foreignKey: source.foreignKey, |
| 44 | + fallback: source.fallback, |
| 45 | + description: source.description, |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /// Derives an instance [DartField] from [record]. |
| 50 | + factory DartField.fromRecord(TFieldRecord record) { |
| 51 | + return DartField( |
| 52 | + fieldPath: record.fieldPath, |
| 53 | + fieldType: record.fieldType, |
| 54 | + nullable: record.nullable, |
| 55 | + children: record.children, |
| 56 | + primaryKey: record.primaryKey, |
| 57 | + foreignKey: record.foreignKey, |
| 58 | + fallback: record.fallback, |
| 59 | + description: record.description, |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + // |
| 64 | + // |
| 65 | + // |
| 66 | + |
| 67 | + /// The super.fieldPath stripped of '?'. |
| 68 | + @override |
| 69 | + List<String>? get fieldPath { |
| 70 | + return super.fieldPath?.map((e) => e.trim().replaceAll('?', '')).toList(); |
| 71 | + } |
| 72 | + |
| 73 | + /// The [fieldPath] joined and to camelCase. |
| 74 | + String? get fieldName { |
| 75 | + return fieldPath?.join('_').toCamelCase(); |
| 76 | + } |
| 77 | + |
| 78 | + /// The super.fieldType stripped of '?'. |
| 79 | + @override |
| 80 | + String? get fieldType { |
| 81 | + final temp = super.fieldType?.toString(); |
| 82 | + if (temp != null) { |
| 83 | + return _expandDynamicTypes( |
| 84 | + _isFieldTypeNullable! ? temp.substring(0, temp.length - 1) : temp, |
| 85 | + ); |
| 86 | + } else { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// The this.fieldPath with '?' if nullable. |
| 92 | + @override |
| 93 | + String? get fieldTypeCode { |
| 94 | + if (fieldType != null) { |
| 95 | + return '$fieldType${nullable ? '?' : ''}'; |
| 96 | + } else { |
| 97 | + return null; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Whether super.fieldPath or super.fieldType ends with '?' or super.nullable is true. |
| 102 | + @override |
| 103 | + bool get nullable { |
| 104 | + return [ |
| 105 | + super.nullable, |
| 106 | + _isFieldNameNullable, |
| 107 | + _isFieldTypeNullable, |
| 108 | + ].any((e) => e == true); |
| 109 | + } |
| 110 | + |
| 111 | + bool? get _isFieldNameNullable => super.fieldPath?.any((e) => e.contains('?')); |
| 112 | + |
| 113 | + bool? get _isFieldTypeNullable => super.fieldType?.endsWith('?') == true; |
| 114 | + |
| 115 | + // |
| 116 | + // |
| 117 | + // |
| 118 | + |
| 119 | + /// Assumes [unknown] is a [TFieldRecord] or [Field] or similar and tries to |
| 120 | + /// construct a [DartField], otherwise returns `null`. |
| 121 | + static DartField? ofOrNull(dynamic unknown) { |
| 122 | + try { |
| 123 | + return DartField.from(Field.ofOrNull(unknown as FieldModel?)!); |
| 124 | + } catch (_) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 131 | + |
| 132 | +/// Expands some non-generic Dart collection types to their generic forms |
| 133 | +/// (e.g. Map to Map\<dynamic, dynamic\>). It processes types separated by "|" |
| 134 | +/// and skips over collections that already specify types. |
| 135 | +/// |
| 136 | +/// This only works for the following types: |
| 137 | +/// |
| 138 | +/// - Map |
| 139 | +/// - List |
| 140 | +/// - Set |
| 141 | +/// - Iterable |
| 142 | +/// - Queue |
| 143 | +/// - LinkedList |
| 144 | +/// - HashSet |
| 145 | +/// - LinkedHashSet |
| 146 | +/// - HashMap |
| 147 | +/// - LinkedHashMap |
| 148 | +String _expandDynamicTypes(String fieldTypeCode) { |
| 149 | + for (final e in { |
| 150 | + 'Map': 'Map<dynamic, dynamic>', |
| 151 | + 'List': 'List<dynamic>', |
| 152 | + 'Set': 'Set<dynamic>', |
| 153 | + 'Iterable': 'Iterable<dynamic>', |
| 154 | + 'Queue': 'Queue<dynamic>', |
| 155 | + 'LinkedList': 'LinkedList<dynamic>', |
| 156 | + 'HashSet': 'HashSet<dynamic>', |
| 157 | + 'LinkedHashSet': 'LinkedHashSet<dynamic>', |
| 158 | + 'HashMap': 'HashMap<dynamic, dynamic>', |
| 159 | + 'LinkedHashMap': 'LinkedHashMap<dynamic, dynamic>', |
| 160 | + }.entries) { |
| 161 | + final key = e.key; |
| 162 | + final value = e.value; |
| 163 | + // This regex looks for the key (like "Map") that is not immediately |
| 164 | + // followed by a "<", but it will also match if the key is followed by "|" |
| 165 | + // and any text. |
| 166 | + final regex = RegExp(r'\b' + key + r'\b(?![<|])'); |
| 167 | + fieldTypeCode = fieldTypeCode.replaceAll(regex, value); |
| 168 | + } |
| 169 | + return fieldTypeCode; |
| 170 | +} |
0 commit comments