Skip to content

Commit b5b7afc

Browse files
committed
+chore: Improve generated output and templates
1 parent 78fbda0 commit b5b7afc

File tree

6 files changed

+609
-438
lines changed

6 files changed

+609
-438
lines changed

example/example.dart renamed to example/example.md

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
```dart
12
//.title
23
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
34
//
@@ -15,16 +16,12 @@
1516
// ignore_for_file: unnecessary_null_comparison
1617
// ignore_for_file: unnecessary_question_mark
1718
18-
// This line is not actually in the generated code.
19-
import 'package:df_generate_dart_models_core/df_generate_dart_models_core.dart';
20-
21-
// This line is in the generated code.
22-
//part of 'field_model.dart';
19+
part of 'example.md';
2320
2421
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
2522
2623
/// Generated class for [_FieldModel].
27-
class FieldModel extends Model {
24+
class FieldModel extends _FieldModel {
2825
//
2926
//
3027
//
@@ -337,65 +334,6 @@ class FieldModel extends Model {
337334
}
338335
}
339336
340-
@override
341-
T mergeWith<T extends BaseModel>(
342-
BaseModel? other, {
343-
bool deepMerge = false,
344-
}) {
345-
final a = toJson();
346-
final b = other?.toJson() ?? {};
347-
final data = (deepMerge ? mergeDataDeep(a, b) : {...a, ...b}) as Map;
348-
return FieldModel.fromJson(data.cast()) as T;
349-
}
350-
351-
/// Creates a copy of this instance, replacing the specified fields.
352-
static FieldModel copyWith(
353-
FieldModel src, {
354-
List<String>? fieldPath,
355-
dynamic? fieldType,
356-
bool? nullable,
357-
List<Map<String, dynamic>>? children,
358-
bool? primaryKey,
359-
bool? foreignKey,
360-
Object? fallback,
361-
String? description,
362-
}) {
363-
return FieldModel.assertRequired(
364-
fieldPath: fieldPath ?? src.fieldPath,
365-
fieldType: fieldType ?? src.fieldType,
366-
nullable: nullable ?? src.nullable,
367-
children: children ?? src.children,
368-
primaryKey: primaryKey ?? src.primaryKey,
369-
foreignKey: foreignKey ?? src.foreignKey,
370-
fallback: fallback ?? src.fallback,
371-
description: description ?? src.description,
372-
);
373-
}
374-
375-
/// Creates a copy of this instance, removing the specified fields.
376-
static FieldModel copyWithout(
377-
FieldModel src, {
378-
bool fieldPath = true,
379-
bool fieldType = true,
380-
bool nullable = true,
381-
bool children = true,
382-
bool primaryKey = true,
383-
bool foreignKey = true,
384-
bool fallback = true,
385-
bool description = true,
386-
}) {
387-
return FieldModel.assertRequired(
388-
fieldPath: fieldPath ? src.fieldPath : null,
389-
fieldType: fieldType ? src.fieldType : null,
390-
nullable: nullable ? src.nullable : null,
391-
children: children ? src.children : null,
392-
primaryKey: primaryKey ? src.primaryKey : null,
393-
foreignKey: foreignKey ? src.foreignKey : null,
394-
fallback: fallback ? src.fallback : null,
395-
description: description ? src.description : null,
396-
);
397-
}
398-
399337
/// Returns the value of the [fieldPath] field.
400338
/// If the field is nullable, the return value may be null; otherwise, it
401339
/// will always return a non-null value.
@@ -472,3 +410,66 @@ abstract final class FieldModelFieldNames {
472410
/// The field name of [FieldModel.description].
473411
static const description = 'description';
474412
}
413+
414+
extension FieldModelX on FieldModel {
415+
/// Creates a copy of this instance, merging another model's fields into
416+
/// this model's fields.
417+
FieldModel mergeWith(
418+
BaseModel? other, {
419+
bool deepMerge = false,
420+
}) {
421+
final a = toJson();
422+
final b = other?.toJson() ?? {};
423+
final data = (deepMerge ? mergeDataDeep(a, b) : {...a, ...b}) as Map;
424+
return FieldModel.fromJson(data.cast());
425+
}
426+
427+
/// Creates a copy of this instance, replacing the specified fields.
428+
FieldModel copyWith(
429+
FieldModel src, {
430+
List<String>? fieldPath,
431+
dynamic? fieldType,
432+
bool? nullable,
433+
List<Map<String, dynamic>>? children,
434+
bool? primaryKey,
435+
bool? foreignKey,
436+
Object? fallback,
437+
String? description,
438+
}) {
439+
return FieldModel.assertRequired(
440+
fieldPath: fieldPath ?? this.fieldPath,
441+
fieldType: fieldType ?? this.fieldType,
442+
nullable: nullable ?? this.nullable,
443+
children: children ?? this.children,
444+
primaryKey: primaryKey ?? this.primaryKey,
445+
foreignKey: foreignKey ?? this.foreignKey,
446+
fallback: fallback ?? this.fallback,
447+
description: description ?? this.description,
448+
);
449+
}
450+
451+
/// Creates a copy of this instance, removing the specified fields.
452+
FieldModel copyWithout(
453+
FieldModel src, {
454+
bool fieldPath = true,
455+
bool fieldType = true,
456+
bool nullable = true,
457+
bool children = true,
458+
bool primaryKey = true,
459+
bool foreignKey = true,
460+
bool fallback = true,
461+
bool description = true,
462+
}) {
463+
return FieldModel.assertRequired(
464+
fieldPath: fieldPath ? this.fieldPath : null,
465+
fieldType: fieldType ? this.fieldType : null,
466+
nullable: nullable ? this.nullable : null,
467+
children: children ? this.children : null,
468+
primaryKey: primaryKey ? this.primaryKey : null,
469+
foreignKey: foreignKey ? this.foreignKey : null,
470+
fallback: fallback ? this.fallback : null,
471+
description: description ? this.description : null,
472+
);
473+
}
474+
}
475+
```

lib/src/models/data_ref/_data_ref_model.g.dart

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ part of 'data_ref_model.dart';
2020
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
2121

2222
/// Generated class for [_DataRefModel].
23-
class DataRefModel extends Model {
23+
class DataRefModel extends _DataRefModel {
2424
//
2525
//
2626
//
@@ -31,29 +31,36 @@ class DataRefModel extends Model {
3131
@override
3232
String get $className => CLASS_NAME;
3333

34-
34+
/// The document ID within the database. If null, the reference points to a collection instead of a specific document.
35+
final String? id;
36+
37+
/// A list representing the hierarchical path where the document or collection resides.
38+
final List<String>? collection;
3539

3640
/// Constructs a new instance of [DataRefModel]
3741
/// from optional and required parameters.
3842
const DataRefModel({
39-
40-
}) ;
43+
this.id,
44+
required this.collection,
45+
});
4146

4247
/// Construcs a new instance of [DataRefModel],
4348
/// forcing all parameters to be optional.
4449
const DataRefModel.optional({
45-
46-
}) ;
47-
50+
this.id,
51+
this.collection,
52+
});
4853

4954
/// Constructs a new instance of [DataRefModel],
5055
/// and asserts that all required parameters are not null.
5156
factory DataRefModel.assertRequired({
52-
57+
String? id,
58+
List<String>? collection,
5359
}) {
54-
55-
return const DataRefModel(
56-
60+
assert(collection != null);
61+
return DataRefModel(
62+
id: id,
63+
collection: collection,
5764
);
5865
}
5966

@@ -80,7 +87,6 @@ class DataRefModel extends Model {
8087
return fromJsonOrNull(another?.toJson())!;
8188
}
8289

83-
8490
/// Constructs a new instance of [DataRefModel],
8591
/// from the fields of [another] instance. Throws if the conversion fails.
8692
factory DataRefModel.of(
@@ -113,7 +119,7 @@ class DataRefModel extends Model {
113119
try {
114120
return fromJsonStringOrNull(jsonString)!;
115121
} catch (e) {
116-
assert(false, '$DataRefModel.fromJsonString: $e');
122+
assert(false, '$DataRefModel.fromJsonString: $e');
117123
rethrow;
118124
}
119125
}
@@ -157,9 +163,18 @@ class DataRefModel extends Model {
157163
Map<String, dynamic>? json,
158164
) {
159165
try {
160-
161-
return const DataRefModel(
162-
166+
final id = json?['id']?.toString().trim().nullIfEmpty;
167+
final collection = letListOrNull<dynamic>(json?['collection'])
168+
?.map(
169+
(p0) => p0?.toString().trim().nullIfEmpty,
170+
)
171+
.nonNulls
172+
.nullIfEmpty
173+
?.toList()
174+
.unmodifiable;
175+
return DataRefModel(
176+
id: id,
177+
collection: collection,
163178
);
164179
} catch (e) {
165180
return null;
@@ -202,9 +217,17 @@ class DataRefModel extends Model {
202217
bool includeNulls = false,
203218
}) {
204219
try {
205-
220+
final id0 = id?.trim().nullIfEmpty;
221+
final collection0 = collection
222+
?.map(
223+
(p0) => p0?.trim().nullIfEmpty,
224+
)
225+
.nonNulls
226+
.nullIfEmpty
227+
?.toList();
206228
final withNulls = {
207-
229+
'id': id0,
230+
'collection': collection0,
208231
};
209232
return includeNulls ? withNulls : withNulls.nonNulls;
210233
} catch (e) {
@@ -213,13 +236,27 @@ class DataRefModel extends Model {
213236
}
214237
}
215238

216-
239+
/// Returns the value of the [id] field.
240+
/// If the field is nullable, the return value may be null; otherwise, it
241+
/// will always return a non-null value.
242+
@pragma('vm:prefer-inline')
243+
String? get id$ => id;
244+
245+
/// Returns the value of the [collection] field.
246+
/// If the field is nullable, the return value may be null; otherwise, it
247+
/// will always return a non-null value.
248+
@pragma('vm:prefer-inline')
249+
List<String> get collection$ => collection!;
217250
}
218251

219252
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
220253

221254
abstract final class DataRefModelFieldNames {
222-
255+
/// The field name of [DataRefModel.id].
256+
static const id = 'id';
257+
258+
/// The field name of [DataRefModel.collection].
259+
static const collection = 'collection';
223260
}
224261

225262
extension DataRefModelX on DataRefModel {
@@ -236,22 +273,26 @@ extension DataRefModelX on DataRefModel {
236273
}
237274

238275
/// Creates a copy of this instance, replacing the specified fields.
239-
DataRefModel copyWith(DataRefModel src, {
240-
276+
DataRefModel copyWith(
277+
DataRefModel src, {
278+
String? id,
279+
List<String>? collection,
241280
}) {
242-
final src = this;
243281
return DataRefModel.assertRequired(
244-
282+
id: id ?? this.id,
283+
collection: collection ?? this.collection,
245284
);
246285
}
247286

248287
/// Creates a copy of this instance, removing the specified fields.
249-
DataRefModel copyWithout(DataRefModel src, {
250-
288+
DataRefModel copyWithout(
289+
DataRefModel src, {
290+
bool id = true,
291+
bool collection = true,
251292
}) {
252-
final src = this;
253293
return DataRefModel.assertRequired(
254-
294+
id: id ? this.id : null,
295+
collection: collection ? this.collection : null,
255296
);
256297
}
257-
}
298+
}

0 commit comments

Comments
 (0)