Skip to content

Commit 5cf17a4

Browse files
Merge pull request #146 from icapps/feature/#135-map-with-list
#135: added support for lists in map
2 parents a444c7e + 60a251a commit 5cf17a4

File tree

115 files changed

+2443
-1053
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+2443
-1053
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Changelog
22
## [7.0.0] - 2023-08-14
33
- *BREAKING CHANGE*: Every type is now defined inline, this means that 'required' is no longer supported, if a field isn't nullable it is automatically required. This also means that the 'array' type is no longer supported and is instead just defined like 'List<T>'.
4+
- *BREAKING CHANGE*: The way enums are defined has changed, see readme for more information. You can now add properties to enums, optional and default values are supported.
5+
- *BREAKING CHANGE*: Enums are now by default not uppercase anymore, you can still enable this my adding 'uppercase_enums: true' to your pubspec or enum configuration
46
- Logs of build runner now get shown in real time.
7+
- You are now allowed to have no properties configured for a class.
58

69
## [6.3.0] - 2023-06-05
710
- Fixed the deprecated `ignore` field. Added

README.md

+141-27
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ DateTimeConverter:
333333

334334
## Inline types (since 6.0.0)
335335

336-
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:
336+
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, since 7.0.0 nested lists and list in maps is also supported:
337337

338338
```yaml
339339
UserModel:
@@ -345,6 +345,7 @@ UserModel:
345345
created_at: DateTime
346346
roles: List<string>
347347
customProperties: Map<String, Property>?
348+
customPropertiesList: Map<String, List<Property>>?
348349
```
349350
since 7.0.0 inline types are supported now even when adding extra configuration:
350351

@@ -373,67 +374,161 @@ BookCase:
373374
include_if_null: false
374375
```
375376

376-
Currently all basic types are supported, simple Lists and Maps (no nested types, no nullable generic parameters) as well as references to other objects.
377+
Currently all basic types are supported, simple Lists and Maps (no nullable generic parameters) as well as references to other objects.
377378
Items post-fixed with `?` will be marked optional.
378379

379-
## Enum support
380+
## Enum support (as of v7.0.0 enums now support properties)
380381

381-
Add enums with custom values (can be mapped to String,double,int)
382+
Add simple enums, the name of the enum value (MALE, FEMALE, X, Y) will be used when parsing from json
382383

383384
```yaml
384385
Gender:
385386
path: webservice/user
386387
type: enum
387-
properties:
388+
values:
388389
MALE:
389-
value: _mAl3
390390
FEMALE:
391-
value: femAle
392391
X:
393-
value: X
394392
Y:
395393
```
396394

397-
### Generate mapping
395+
By default enums will be generated with a property called jsonValue. this is the value of the enum used when parsing from json. This will only be used when there isn't already a custom jsonValue defined using 'is_json_value: true' in the properties of the enum. To turn this behavior of you can use 'use_default_json_value: false'.
396+
397+
```yaml
398+
Gender:
399+
path: webservice/user
400+
use_default_json_value: false
401+
type: enum
402+
values:
403+
MALE:
404+
FEMALE:
405+
X:
406+
Y:
407+
```
398408

399-
For enums, it is also possible to have a map generated that maps from the enum value to its string representation and reverse. To enable this, use `generate_map: true`
409+
Add enums with custom properties (currently supported types are int, double, bool and String)
400410

401411
```yaml
402412
Gender:
403413
path: webservice/user
404414
type: enum
405-
generate_map: true
406415
properties:
416+
abbreviation: String
417+
values:
407418
MALE:
408-
value: _mAl3
419+
properties:
420+
abbreviation: m
409421
FEMALE:
410-
value: femAle
422+
properties:
423+
abbreviation: f
411424
X:
412-
value: X
425+
properties:
426+
abbreviation: x
413427
Y:
428+
properties:
429+
abbreviation: y
414430
```
415431

416-
### Generate mapping extensions
432+
Define custom json key using is_json_value, the value of this property will then be used to parse from json
433+
434+
```yaml
435+
Gender:
436+
path: webservice/user
437+
type: enum
438+
properties:
439+
key:
440+
type: String
441+
is_json_value: true
442+
abbreviation: String
443+
values:
444+
MALE:
445+
properties:
446+
key: male
447+
abbreviation: m
448+
FEMALE:
449+
properties:
450+
key: female
451+
abbreviation: f
452+
X:
453+
properties:
454+
key: x
455+
abbreviation: x
456+
Y:
457+
properties:
458+
key: y
459+
abbreviation: y
460+
```
417461

418-
When generating maps, it is also possible to specify that special extension functions should be added that return either the string value or that takes a string value and tries to
419-
convert it to the enum value. To enable this, use `generate_map: true` **AND** `generate_extensions: true`
462+
Optional and default values are supported. If value isn't defined for a property then it will use the defaultValue. If a property is optional and no value is given it is null.
420463

421464
```yaml
422465
Gender:
423466
path: webservice/user
424467
type: enum
425-
generate_map: true
426-
generate_extensions: true
427468
properties:
469+
key:
470+
type: String
471+
is_json_value: true
472+
abbreviation:
473+
type: String
474+
default_value: m
475+
lastName: String?
476+
values:
428477
MALE:
429-
value: _mAl3
478+
properties:
479+
key: male
430480
FEMALE:
431-
value: femAle
481+
properties:
482+
key: female
483+
abbreviation: f
432484
X:
433-
value: X
485+
properties:
486+
key: x
434487
Y:
488+
properties:
489+
key: y
490+
lastName: lastName
435491
```
436492

493+
### Generate mapping extensions
494+
495+
It is possible to generate an extension for the enum that can turn the enum into it's corresponding jsonValue and the reverse.
496+
497+
```yaml
498+
Person:
499+
path: test/enum/
500+
type: enum
501+
generate_extension: true
502+
properties:
503+
jsonValue:
504+
is_json_value: true
505+
type: int
506+
firstName: String
507+
lastName: String
508+
values:
509+
MAN:
510+
properties:
511+
jsonKey: 1
512+
firstName: firstName1
513+
lastName: lastName1
514+
WOMAN:
515+
properties:
516+
jsonKey: 2
517+
firstName: firstName2
518+
lastName: lastName2
519+
520+
```
521+
The above configuration will generate an enum with this extension.
522+
523+
```dart
524+
extension PersonExtension on Person {
525+
static Person? fromJsonValue(int value) => Person.values.firstWhereOrNull((enumValue) => enumValue.jsonKey == value);
526+
527+
int toJsonValue() => jsonKey;
528+
}
529+
```
530+
531+
### Generate mapping is no longer supported as of V7.0.0, use properties instead
437532
### Use unknownEnumValue
438533

439534
```yaml
@@ -445,22 +540,22 @@ UnknownEnumTestObject:
445540
type: Gender
446541
```
447542

448-
### Automatic case conversion
543+
### Automatic case conversion(v7.0.0)
449544

450-
By default all fields will be converted into uppercase. You can control this behavior globally for all enums or per-enum by setting the `uppercase_enums` property to `true` (
451-
default) or `false`
545+
As of v7.0.0 by default all fields will be converted into lowercase camelcase instead of uppercase like before. You can control this behavior globally for all enums or per-enum by setting the `uppercase_enums` property to `false` (
546+
default) or `true`. This only affects the name of the enum when using it in dart code. the jsonValue will still be the name you type in the config.
452547

453548
```yaml
454549
model_generator:
455-
uppercase_enums: false
550+
uppercase_enums: true
456551
```
457552

458553
or
459554

460555
```yaml
461556
UnknownEnumTestObject:
462557
path: webservice
463-
uppercase_enums: false
558+
uppercase_enums: true
464559
properties:
465560
path:
466561
```
@@ -546,6 +641,8 @@ DateTimeConverter:
546641
547642
You can specify `description` on models, enum, fields and on enum entries. This description will be used verbatim to generate a code comment for that class/enum/field
548643

644+
Example for a class:
645+
549646
```yaml
550647
UserModel:
551648
path: webservice/user
@@ -557,6 +654,23 @@ UserModel:
557654
changedAt: DateTime
558655
```
559656

657+
Example for a enum:
658+
659+
```yaml
660+
Person:
661+
path: test/enum/
662+
type: enum
663+
description: This is a enum of a person
664+
values:
665+
MAN:
666+
description: enum of a man
667+
WOMAN:
668+
description: enum of a woman
669+
OTHER:
670+
description: enum of a other
671+
```
672+
673+
560674
## Static creator support
561675

562676
You can specify `static_create` on objects or globally in the `pubspec.yaml` file. If this is specified, a static creator method called `create` will be generated referencing the

assets/example.gif

-688 KB
Binary file not shown.

0 commit comments

Comments
 (0)