Skip to content

Commit b2689d4

Browse files
committed
+breaking: Improve generated model
1 parent f5e0c9c commit b2689d4

File tree

7 files changed

+122
-95
lines changed

7 files changed

+122
-95
lines changed

example/example.dart

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Example of a generated model:
2-
31
//.title
42
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
53
//
@@ -17,12 +15,16 @@
1715
// ignore_for_file: unnecessary_null_comparison
1816
// ignore_for_file: unnecessary_question_mark
1917

18+
// This line is not actually in the generated code.
2019
import 'package:df_generate_dart_models_core/df_generate_dart_models_core.dart';
2120

21+
// This line is in the generated code.
22+
//part of 'field_model.dart';
23+
2224
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
2325

2426
/// Generated class for [_FieldModel].
25-
class FieldModel extends Model /* _FieldModel */ {
27+
class FieldModel extends Model {
2628
//
2729
//
2830
//
@@ -42,16 +44,16 @@ class FieldModel extends Model /* _FieldModel */ {
4244
/// Whether the field can hold a null value.
4345
final bool? nullable;
4446

45-
/// [Unimplemented] Children of this field, allowing for nested fields or complex structures.
46-
final List<FieldModel>? children;
47+
/// Children of this field, allowing for nested fields or complex structures.
48+
final List<Map<String, dynamic>>? children;
4749

48-
/// [Unimplemented] Whether this field serves as a primary key.
50+
/// Whether this field serves as a primary key.
4951
final bool? primaryKey;
5052

51-
/// [Unimplemented] Whether this field serves as a foreign key.
53+
/// Whether this field serves as a foreign key.
5254
final bool? foreignKey;
5355

54-
/// [Unimplemented] The default/fallback value for the field, to use in cases where the value is null
56+
/// The default/fallback value for the field, to use in cases where the value is null.
5557
final Object? fallback;
5658

5759
/// A brief comment or explanation for the field's purpose.
@@ -89,7 +91,7 @@ class FieldModel extends Model /* _FieldModel */ {
8991
List<String>? fieldPath,
9092
dynamic? fieldType,
9193
bool? nullable,
92-
List<FieldModel>? children,
94+
List<Map<String, dynamic>>? children,
9395
bool? primaryKey,
9496
bool? foreignKey,
9597
Object? fallback,
@@ -214,19 +216,27 @@ class FieldModel extends Model /* _FieldModel */ {
214216
)
215217
.nonNulls
216218
.nullIfEmpty
217-
?.toList();
219+
?.toList()
220+
.unmodifiable;
218221
final fieldType = json?['fieldType'];
219222
final nullable = letAsOrNull<bool>(json?['nullable']);
220223
final children = letListOrNull<dynamic>(json?['children'])
221224
?.map(
222-
(p0) => () {
223-
final a = letMapOrNull<String, dynamic>(p0);
224-
return a != null ? FieldModel.fromJson(a) : null;
225-
}(),
225+
(p0) => letMapOrNull<dynamic, dynamic>(p0)
226+
?.map(
227+
(p0, p1) => MapEntry(
228+
p0?.toString().trim().nullIfEmpty,
229+
p1,
230+
),
231+
)
232+
.nonNulls
233+
.nullIfEmpty
234+
?.unmodifiable,
226235
)
227236
.nonNulls
228237
.nullIfEmpty
229-
?.toList();
238+
?.toList()
239+
.unmodifiable;
230240
final primaryKey = letAsOrNull<bool>(json?['primaryKey']);
231241
final foreignKey = letAsOrNull<bool>(json?['foreignKey']);
232242
final fallback = json?['fallback'];
@@ -293,7 +303,15 @@ class FieldModel extends Model /* _FieldModel */ {
293303
final nullable0 = nullable;
294304
final children0 = children
295305
?.map(
296-
(p0) => p0?.toJson(),
306+
(p0) => p0
307+
?.map(
308+
(p0, p1) => MapEntry(
309+
p0?.trim().nullIfEmpty,
310+
p1,
311+
),
312+
)
313+
.nonNulls
314+
.nullIfEmpty,
297315
)
298316
.nonNulls
299317
.nullIfEmpty
@@ -320,41 +338,43 @@ class FieldModel extends Model /* _FieldModel */ {
320338
}
321339

322340
@override
323-
BaseModel mergeWith(
341+
T mergeWith<T extends BaseModel>(
324342
BaseModel? other, {
325343
bool deepMerge = false,
326344
}) {
327345
final a = toJson();
328346
final b = other?.toJson() ?? {};
329347
final data = (deepMerge ? mergeDataDeep(a, b) : {...a, ...b}) as Map;
330-
return FieldModel.fromJson(data.cast());
348+
return FieldModel.fromJson(data.cast()) as T;
331349
}
332350

333351
/// Creates a copy of this instance, replacing the specified fields.
334-
FieldModel copyWithFieldModelProperties({
352+
static FieldModel copyWith(
353+
FieldModel src, {
335354
List<String>? fieldPath,
336355
dynamic? fieldType,
337356
bool? nullable,
338-
List<FieldModel>? children,
357+
List<Map<String, dynamic>>? children,
339358
bool? primaryKey,
340359
bool? foreignKey,
341360
Object? fallback,
342361
String? description,
343362
}) {
344363
return FieldModel.assertRequired(
345-
fieldPath: fieldPath ?? this.fieldPath,
346-
fieldType: fieldType ?? this.fieldType,
347-
nullable: nullable ?? this.nullable,
348-
children: children ?? this.children,
349-
primaryKey: primaryKey ?? this.primaryKey,
350-
foreignKey: foreignKey ?? this.foreignKey,
351-
fallback: fallback ?? this.fallback,
352-
description: description ?? this.description,
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,
353372
);
354373
}
355374

356375
/// Creates a copy of this instance, removing the specified fields.
357-
FieldModel copyWithoutFieldModelProperties({
376+
static FieldModel copyWithout(
377+
FieldModel src, {
358378
bool fieldPath = true,
359379
bool fieldType = true,
360380
bool nullable = true,
@@ -365,14 +385,14 @@ class FieldModel extends Model /* _FieldModel */ {
365385
bool description = true,
366386
}) {
367387
return FieldModel.assertRequired(
368-
fieldPath: fieldPath ? this.fieldPath : null,
369-
fieldType: fieldType ? this.fieldType : null,
370-
nullable: nullable ? this.nullable : null,
371-
children: children ? this.children : null,
372-
primaryKey: primaryKey ? this.primaryKey : null,
373-
foreignKey: foreignKey ? this.foreignKey : null,
374-
fallback: fallback ? this.fallback : null,
375-
description: description ? this.description : null,
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,
376396
);
377397
}
378398

@@ -398,7 +418,7 @@ class FieldModel extends Model /* _FieldModel */ {
398418
/// If the field is nullable, the return value may be null; otherwise, it
399419
/// will always return a non-null value.
400420
@pragma('vm:prefer-inline')
401-
List<FieldModel>? get children$ => children;
421+
List<Map<String, dynamic>>? get children$ => children;
402422

403423
/// Returns the value of the [primaryKey] field.
404424
/// If the field is nullable, the return value may be null; otherwise, it

lib/src/models/base_model.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ abstract mixin class BaseModel {
5454
bool includeNulls = false,
5555
}) {
5656
final a = toJson(includeNulls: includeNulls);
57-
final b = a.keys.toList(growable: false)
58-
..sort((k1, k2) => k1.compareTo(k2));
57+
final b = a.keys.toList(growable: false)..sort((k1, k2) => k1.compareTo(k2));
5958
final c = {for (var k in b) k: a[k] as dynamic};
6059
return c;
6160
}
@@ -66,7 +65,7 @@ abstract mixin class BaseModel {
6665

6766
/// Returns a new copy of the BaseModel with the fields updated from the
6867
/// [other] BaseModel. Set [deepMerge] to `true` to merge nested objects.
69-
BaseModel mergeWith(BaseModel? other, {bool deepMerge = false});
68+
T mergeWith<T extends BaseModel>(BaseModel? other, {bool deepMerge = false});
7069

7170
//
7271
//

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,35 +237,37 @@ class DataRefModel extends _DataRefModel {
237237
}
238238

239239
@override
240-
BaseModel mergeWith(
240+
T mergeWith<T extends BaseModel>(
241241
BaseModel? other, {
242242
bool deepMerge = false,
243243
}) {
244244
final a = toJson();
245245
final b = other?.toJson() ?? {};
246246
final data = (deepMerge ? mergeDataDeep(a, b) : {...a, ...b}) as Map;
247-
return DataRefModel.fromJson(data.cast());
247+
return DataRefModel.fromJson(data.cast()) as T;
248248
}
249249

250250
/// Creates a copy of this instance, replacing the specified fields.
251-
DataRefModel copyWithDataRefModelProperties({
251+
static DataRefModel copyWith(
252+
DataRefModel src, {
252253
String? id,
253254
List<String>? collection,
254255
}) {
255256
return DataRefModel.assertRequired(
256-
id: id ?? this.id,
257-
collection: collection ?? this.collection,
257+
id: id ?? src.id,
258+
collection: collection ?? src.collection,
258259
);
259260
}
260261

261262
/// Creates a copy of this instance, removing the specified fields.
262-
DataRefModel copyWithoutDataRefModelProperties({
263+
static DataRefModel copyWithout(
264+
DataRefModel src, {
263265
bool id = true,
264266
bool collection = true,
265267
}) {
266268
return DataRefModel.assertRequired(
267-
id: id ? this.id : null,
268-
collection: collection ? this.collection : null,
269+
id: id ? src.id : null,
270+
collection: collection ? src.collection : null,
269271
);
270272
}
271273

lib/src/models/field_model/_field_model.g.dart

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,19 @@ class FieldModel extends _FieldModel {
334334
}
335335

336336
@override
337-
BaseModel mergeWith(
337+
T mergeWith<T extends BaseModel>(
338338
BaseModel? other, {
339339
bool deepMerge = false,
340340
}) {
341341
final a = toJson();
342342
final b = other?.toJson() ?? {};
343343
final data = (deepMerge ? mergeDataDeep(a, b) : {...a, ...b}) as Map;
344-
return FieldModel.fromJson(data.cast());
344+
return FieldModel.fromJson(data.cast()) as T;
345345
}
346346

347347
/// Creates a copy of this instance, replacing the specified fields.
348-
FieldModel copyWithFieldModelProperties({
348+
static FieldModel copyWith(
349+
FieldModel src, {
349350
List<String>? fieldPath,
350351
dynamic? fieldType,
351352
bool? nullable,
@@ -356,19 +357,20 @@ class FieldModel extends _FieldModel {
356357
String? description,
357358
}) {
358359
return FieldModel.assertRequired(
359-
fieldPath: fieldPath ?? this.fieldPath,
360-
fieldType: fieldType ?? this.fieldType,
361-
nullable: nullable ?? this.nullable,
362-
children: children ?? this.children,
363-
primaryKey: primaryKey ?? this.primaryKey,
364-
foreignKey: foreignKey ?? this.foreignKey,
365-
fallback: fallback ?? this.fallback,
366-
description: description ?? this.description,
360+
fieldPath: fieldPath ?? src.fieldPath,
361+
fieldType: fieldType ?? src.fieldType,
362+
nullable: nullable ?? src.nullable,
363+
children: children ?? src.children,
364+
primaryKey: primaryKey ?? src.primaryKey,
365+
foreignKey: foreignKey ?? src.foreignKey,
366+
fallback: fallback ?? src.fallback,
367+
description: description ?? src.description,
367368
);
368369
}
369370

370371
/// Creates a copy of this instance, removing the specified fields.
371-
FieldModel copyWithoutFieldModelProperties({
372+
static FieldModel copyWithout(
373+
FieldModel src, {
372374
bool fieldPath = true,
373375
bool fieldType = true,
374376
bool nullable = true,
@@ -379,14 +381,14 @@ class FieldModel extends _FieldModel {
379381
bool description = true,
380382
}) {
381383
return FieldModel.assertRequired(
382-
fieldPath: fieldPath ? this.fieldPath : null,
383-
fieldType: fieldType ? this.fieldType : null,
384-
nullable: nullable ? this.nullable : null,
385-
children: children ? this.children : null,
386-
primaryKey: primaryKey ? this.primaryKey : null,
387-
foreignKey: foreignKey ? this.foreignKey : null,
388-
fallback: fallback ? this.fallback : null,
389-
description: description ? this.description : null,
384+
fieldPath: fieldPath ? src.fieldPath : null,
385+
fieldType: fieldType ? src.fieldType : null,
386+
nullable: nullable ? src.nullable : null,
387+
children: children ? src.children : null,
388+
primaryKey: primaryKey ? src.primaryKey : null,
389+
foreignKey: foreignKey ? src.foreignKey : null,
390+
fallback: fallback ? src.fallback : null,
391+
description: description ? src.description : null,
390392
);
391393
}
392394

0 commit comments

Comments
 (0)