Skip to content

Commit ec4d0a3

Browse files
Merge pull request #114 from icapps/feature/SimpleType
Support inline type definitions
2 parents 7a0e3db + 4db4f11 commit ec4d0a3

16 files changed

+383
-106
lines changed

.fvm/fvm_config.json

-4
This file was deleted.

CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
# Changelog
2+
## [6.0.0] - 2022-10-04
3+
- Added the ability to specify simple fields in a more compact way, inline.
4+
5+
Example: id and id2, name and name2 are equivalent notations
6+
```yaml
7+
ExampleModel:
8+
properties:
9+
id: string
10+
id2:
11+
type: string
12+
required: true
13+
name: string?
14+
name2:
15+
type: string
16+
required: false
17+
```
18+
219
## [5.9.0] - 2022-03-29
320
- *POTENTIALLY BREAKING CHANGE*: By default, fields with a default value will now accept 'null' to use their default value
421
- Add options to control whether fields with default value accepts 'null' or not

README.md

+45-13
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ UserModel:
8585
type: int
8686
ignore_equality: true
8787
include:
88-
type: string
88+
type: String
8989
```
9090

9191
## explicit_to_json
@@ -174,7 +174,7 @@ UserModel:
174174
type: int
175175
default_value: 1
176176
name:
177-
type: string
177+
type: String
178178
required: true
179179
default_value: "'an example quoted string'"
180180
```
@@ -204,7 +204,7 @@ UserModel:
204204
type: int
205205
default_value: 1
206206
name:
207-
type: string
207+
type: String
208208
required: true
209209
default_value: "'an example quoted string'"
210210
```
@@ -219,7 +219,7 @@ UserModel:
219219
type: int
220220
default_value: 1
221221
name:
222-
type: string
222+
type: String
223223
required: true
224224
default_value: "'an example quoted string'"
225225
disallow_null: true
@@ -259,7 +259,20 @@ UserDetails:
259259
extends: UserModel
260260
properties:
261261
name:
262-
type: string
262+
type: String
263+
```
264+
265+
## Builtin types
266+
The following framework types descriptors are known out of the box:
267+
```
268+
string/String
269+
int/integer
270+
bool/boolean
271+
double
272+
date/datetime
273+
dynamic/object/any
274+
array
275+
map
263276
```
264277

265278
## Default setup
@@ -275,7 +288,7 @@ UserModel:
275288
id:
276289
type: int
277290
name:
278-
type: string
291+
type: String
279292
salary:
280293
type: double
281294
something:
@@ -286,7 +299,7 @@ UserModel:
286299
roles:
287300
type: array
288301
items:
289-
type: string
302+
type: String
290303
birthday:
291304
type: date
292305
addresses:
@@ -299,19 +312,19 @@ UserModel:
299312
key: String
300313
value: Address
301314
securityRole:
302-
type: string
315+
type: String
303316
jsonKey: securityIndicator
304317
dynamicField:
305318
type: dynamic
306319
includeIfNullField:
307320
include_if_null: false #If this field is null, this field will not be added to your json object (used for PATCH models)
308-
type: string
321+
type: String
309322
ignoreField:
310323
ignore: false #this field will not be final, and not be used in the json parsing
311-
type: string
324+
type: String
312325
mutableField:
313326
non_final: true #Field will not be marked final
314-
type: string
327+
type: String
315328
changedAt:
316329
type: datetime
317330
idToAddressList:
@@ -324,15 +337,15 @@ Address:
324337
path: webservice/user #Can also be package:... and/or end with the actual file (.dart)
325338
properties:
326339
street:
327-
type: string
340+
type: String
328341
329342
#Custom base_directory
330343
CustomBaseDirectoryObject:
331344
base_directory: custom_models
332345
path: webservice
333346
properties:
334347
path:
335-
type: string
348+
type: String
336349
337350
#Custom json converter. Use with converters property on models
338351
DateTimeConverter:
@@ -341,6 +354,25 @@ DateTimeConverter:
341354
342355
```
343356

357+
## Inline types (since 6.0.0)
358+
359+
In some cases, writing the full specification for simple fields is very verbose. Since 6.0.0 it is possible to write simple fields inline, without nesting below the field name:
360+
361+
```yaml
362+
UserModel:
363+
properties:
364+
id: int
365+
name: String
366+
age: int
367+
is_active: bool?
368+
created_at: DateTime
369+
roles: List<string>
370+
customProperties: Map<String, Property>?
371+
```
372+
373+
Currently all basic types are supported, simple Lists and Maps (no nested types, no nullable generic parameters) as well as references to other objects.
374+
Items post-fixed with `?` will be marked optional.
375+
344376
## Enum support
345377

346378
Add enums with custom values

example/.fvm/fvm_config.json

-4
This file was deleted.

example/lib/model/ogm.dart

+21-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ part 'ogm.g.dart';
77
@JsonSerializable(explicitToJson: true)
88
@DateTimeConverter()
99
class OGM {
10-
@JsonKey(name: 'structuredMessage', required: true, includeIfNull: false)
11-
final String structuredMessage;
1210
@JsonKey(name: 'beneficiary', required: true, includeIfNull: false)
1311
final String beneficiary;
1412
@JsonKey(name: 'beneficiaryIBAN', required: true, includeIfNull: false)
@@ -19,6 +17,10 @@ class OGM {
1917
final String someThing;
2018
@JsonKey(name: 'some_ThinG_huGE', required: true, includeIfNull: false)
2119
final String someThinGHuGE;
20+
@JsonKey(name: 'simpleFields', required: true)
21+
final List<Testing> simpleFields;
22+
@JsonKey(name: 'structuredMessage')
23+
final String? structuredMessage;
2224
@JsonKey(name: 'securityIndicator', includeIfNull: false)
2325
final String? securityRole;
2426
@JsonKey(name: 'mutableProperty', includeIfNull: false)
@@ -27,18 +29,22 @@ class OGM {
2729
final DateTime? dateChange;
2830
@JsonKey(name: 'fields', includeIfNull: false)
2931
final List<List<Testing>>? fields;
32+
@JsonKey(name: 'simpleMap')
33+
final Map<String, Testing>? simpleMap;
3034

3135
OGM({
32-
required this.structuredMessage,
3336
required this.beneficiary,
3437
required this.beneficiaryIBAN,
3538
required this.testTest,
3639
required this.someThing,
3740
required this.someThinGHuGE,
41+
required this.simpleFields,
42+
this.structuredMessage,
3843
this.securityRole,
3944
this.mutableProperty,
4045
this.dateChange,
4146
this.fields,
47+
this.simpleMap,
4248
});
4349

4450
factory OGM.fromJson(Map<String, dynamic> json) => _$OGMFromJson(json);
@@ -50,42 +56,48 @@ class OGM {
5056
identical(this, other) ||
5157
other is OGM &&
5258
runtimeType == other.runtimeType &&
53-
structuredMessage == other.structuredMessage &&
5459
beneficiary == other.beneficiary &&
5560
beneficiaryIBAN == other.beneficiaryIBAN &&
5661
testTest == other.testTest &&
5762
someThing == other.someThing &&
5863
someThinGHuGE == other.someThinGHuGE &&
64+
simpleFields == other.simpleFields &&
65+
structuredMessage == other.structuredMessage &&
5966
securityRole == other.securityRole &&
6067
mutableProperty == other.mutableProperty &&
6168
dateChange == other.dateChange &&
62-
fields == other.fields;
69+
fields == other.fields &&
70+
simpleMap == other.simpleMap;
6371

6472
@override
6573
int get hashCode =>
66-
structuredMessage.hashCode ^
6774
beneficiary.hashCode ^
6875
beneficiaryIBAN.hashCode ^
6976
testTest.hashCode ^
7077
someThing.hashCode ^
7178
someThinGHuGE.hashCode ^
79+
simpleFields.hashCode ^
80+
structuredMessage.hashCode ^
7281
securityRole.hashCode ^
7382
mutableProperty.hashCode ^
7483
dateChange.hashCode ^
75-
fields.hashCode;
84+
fields.hashCode ^
85+
simpleMap.hashCode;
7686

7787
@override
7888
String toString() => 'OGM{'
79-
'structuredMessage: $structuredMessage, '
8089
'beneficiary: $beneficiary, '
8190
'beneficiaryIBAN: $beneficiaryIBAN, '
8291
'testTest: $testTest, '
8392
'someThing: $someThing, '
8493
'someThinGHuGE: $someThinGHuGE, '
94+
'simpleFields: $simpleFields, '
95+
'structuredMessage: $structuredMessage, '
8596
'securityRole: $securityRole, '
8697
'mutableProperty: $mutableProperty, '
8798
'dateChange: $dateChange, '
88-
'fields: $fields'
99+
'fields: $fields, '
100+
'simpleMap: $simpleMap'
89101
'}';
90102
}
91103

example/lib/model/ogm.g.dart

+12-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/model/user/profile/admin_profile_data.dart

+16-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import 'package:model_generator_example/model/user/testing.dart';
77

88
part 'admin_profile_data.g.dart';
99

10-
@JsonSerializable()
10+
@JsonSerializable(explicitToJson: true)
1111
@immutable
1212
class AdminProfileData extends UserProfileDataExtended {
13-
@JsonKey(name: 'privileges', required: true)
13+
@JsonKey(name: 'privileges', required: true, includeIfNull: false)
1414
final String privileges;
1515

1616
const AdminProfileData({
@@ -76,3 +76,17 @@ class AdminProfileData extends UserProfileDataExtended {
7676
'personsById: $personsById'
7777
'}';
7878
}
79+
80+
AdminProfileData deserializeAdminProfileData(Map<String, dynamic> json) =>
81+
AdminProfileData.fromJson(json);
82+
83+
Map<String, dynamic> serializeAdminProfileData(AdminProfileData object) =>
84+
object.toJson();
85+
86+
List<AdminProfileData> deserializeAdminProfileDataList(
87+
List<Map<String, dynamic>> jsonList) =>
88+
jsonList.map((json) => AdminProfileData.fromJson(json)).toList();
89+
90+
List<Map<String, dynamic>> serializeAdminProfileDataList(
91+
List<AdminProfileData> objects) =>
92+
objects.map((object) => object.toJson()).toList();

0 commit comments

Comments
 (0)