Skip to content

Commit 4bf598a

Browse files
committed
+feat: Add new library: df_generate_dart_models_utils
1 parent 0e55cad commit 4bf598a

11 files changed

+804
-4
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
library;
14+
15+
export 'src/_utils/_index.g.dart';

lib/src/_utils/_index.g.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//.title
2+
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
3+
//
4+
// GENERATED - DO NOT MODIFY BY HAND
5+
// See: https://github.com/DevCetra/df_generate_dart_indexes
6+
//
7+
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
8+
//.title~
9+
10+
export 'dart_type_code_mapper.dart';
11+
export 'dart_obj_to_string_list.dart';
12+
export 'dart_field.dart';
13+
export 'extract_insights_from_file.dart';
14+
export 'dart_obj_to_object.dart';
15+
export 'decompose_dart_collection_type.dart';
16+
export 'dart_from_record_on_dart_object_x.dart';
17+
export 'strip_special_syntax_from_field_type.dart';

lib/src/_utils/dart_field.dart

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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:analyzer/dart/constant/value.dart';
14+
15+
import 'dart_obj_to_string_list.dart';
16+
import 'dart_obj_to_object.dart';
17+
import '../models/field_model/field_model.dart';
18+
19+
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
20+
21+
extension DartFromRecordOnDartObjectX on DartObject {
22+
//
23+
//
24+
//
25+
26+
/// Returns `fieldName` property from `this` [DartObject] record if it matches
27+
/// the structure of [TFieldRecord] or `null`.
28+
List<String>? fieldPathFromRecord() {
29+
return _rawFieldPathFromRecord()?.map((e) => e.replaceAll('?', '')).toList();
30+
}
31+
32+
List<String>? _rawFieldPathFromRecord() {
33+
final a = dartObjToStringList(getField('\$1'));
34+
final b = dartObjToStringList(getField(FieldModelFieldNames.fieldPath));
35+
return (a ?? b)?.toList();
36+
}
37+
38+
/// Returns the `fieldType` property from `this` DartObject record if it
39+
/// matches the structure of [TFieldRecord] or `null`.
40+
String? fieldTypeFromRecord() {
41+
final raw = _rawFieldTypeFromRecord();
42+
if (raw != null) {
43+
return raw.endsWith('?') ? raw.substring(0, raw.length - 1) : raw;
44+
}
45+
return null;
46+
}
47+
48+
String? _rawFieldTypeFromRecord() {
49+
final a = getField('\$2')?.toStringValue();
50+
final b = getField('\$2')?.toTypeValue()?.getDisplayString();
51+
final c = getField(FieldModelFieldNames.fieldType)?.toStringValue();
52+
final d = getField(FieldModelFieldNames.fieldType)?.toTypeValue()?.getDisplayString();
53+
return a ?? b ?? c ?? d;
54+
}
55+
56+
/// Returns the `nullable` property from `this` [DartObject] record if it
57+
/// matches the structure of [TFieldRecord] or `null`.
58+
bool? nullableFromRecord() {
59+
if (fieldTypeFromRecord() == 'dynamic') {
60+
return false;
61+
}
62+
63+
final a = getField(FieldModelFieldNames.nullable)?.toBoolValue();
64+
final b = getField('\$3')?.toBoolValue();
65+
final c = _rawFieldPathFromRecord()?.any((e) => e.contains('?'));
66+
final d = _rawFieldTypeFromRecord()?.endsWith('?');
67+
return a ?? b ?? ((c ?? false) || (d ?? false));
68+
}
69+
70+
/// Returns the `children` property from `this` [DartObject] record if it
71+
/// matches the structure of [TFieldRecord] or `null`.
72+
List<Map<String, dynamic>>? childrenFromRecord() {
73+
return getField(FieldModelFieldNames.children)
74+
?.toListValue()
75+
?.map(
76+
(e) => e.toMapValue()!.map((k, v) => MapEntry(k!.toStringValue()!, dartObjToObject(v))),
77+
)
78+
.toList();
79+
}
80+
81+
/// Returns the `primaryKey` property from `this` [DartObject] record if it
82+
/// matches the structure of [TFieldRecord] or `null`.
83+
bool? primaryKeyFromRecord() {
84+
return getField(FieldModelFieldNames.primaryKey)?.toBoolValue();
85+
}
86+
87+
/// Returns the `foreignKey` property from `this` [DartObject] record if it
88+
/// matches the structure of [TFieldRecord] or `null`.
89+
bool? foreignKeyFromRecord() {
90+
return getField(FieldModelFieldNames.foreignKey)?.toBoolValue();
91+
}
92+
93+
/// Retrieves the `fallback` property from this [DartObject] record if it
94+
/// matches the structure of [TFieldRecord] or returns `null`.
95+
Object? fallbackFromRecord() {
96+
final fallbackField = getField(FieldModelFieldNames.fallback);
97+
return dartObjToObject(fallbackField);
98+
}
99+
100+
/// Returns the `description` property from `this` [DartObject] record if it
101+
/// matches the structure of [TFieldRecord] or `null`.
102+
String? descriptionFromRecord() {
103+
return getField(FieldModelFieldNames.description)?.toStringValue();
104+
}
105+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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:analyzer/dart/constant/value.dart';
14+
15+
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
16+
17+
Object? dartObjToObject(DartObject? dartObject) {
18+
if (dartObject != null) {
19+
try {
20+
return dartObject.toStringValue()!;
21+
} catch (_) {}
22+
try {
23+
return dartObject.toIntValue()!;
24+
} catch (_) {}
25+
26+
try {
27+
return dartObject.toBoolValue()!;
28+
} catch (_) {}
29+
try {
30+
return dartObject.toDoubleValue()!;
31+
} catch (_) {}
32+
try {
33+
return dartObject.toListValue()!.map((e) => dartObjToObject(e));
34+
} catch (_) {}
35+
try {
36+
return dartObject.toSetValue()!.map((e) => dartObjToObject(e));
37+
} catch (_) {}
38+
try {
39+
return dartObject
40+
.toMapValue()!
41+
.map((k, v) => MapEntry(dartObjToObject(k), dartObjToObject(v)));
42+
} catch (_) {}
43+
}
44+
return null;
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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:analyzer/dart/constant/value.dart';
14+
15+
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
16+
17+
List<String>? dartObjToStringList(DartObject? obj) {
18+
final a = obj?.toStringValue();
19+
if (a != null) {
20+
return [a];
21+
}
22+
final b = obj?.toListValue()?.map((e) => e.toStringValue()).nonNulls.toList();
23+
return b;
24+
}

0 commit comments

Comments
 (0)