Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inline type definitions #114

Merged
merged 4 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .fvm/fvm_config.json

This file was deleted.

17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# Changelog
## [6.0.0] - 2022-10-04
- Added the ability to specify simple fields in a more compact way, inline.

Example: id and id2, name and name2 are equivalent notations
```yaml
ExampleModel:
properties:
id: string
id2:
type: string
NicolaVerbeeck marked this conversation as resolved.
Show resolved Hide resolved
required: true
name: string?
name2:
type: string
required: false
```

## [5.9.0] - 2022-03-29
- *POTENTIALLY BREAKING CHANGE*: By default, fields with a default value will now accept 'null' to use their default value
- Add options to control whether fields with default value accepts 'null' or not
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,25 @@ DateTimeConverter:

```

## Inline types (since 6.0.0)
NicolaVerbeeck marked this conversation as resolved.
Show resolved Hide resolved

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:

```yaml
UserModel:
properties:
id: int
name: string
age: int
is_active: bool?
created_at: DateTime
roles: List<string>
customProperties: Map<String, Property>?
```

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

## Enum support

Add enums with custom values
Expand Down
4 changes: 0 additions & 4 deletions example/.fvm/fvm_config.json

This file was deleted.

30 changes: 21 additions & 9 deletions example/lib/model/ogm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ part 'ogm.g.dart';
@JsonSerializable(explicitToJson: true)
@DateTimeConverter()
class OGM {
@JsonKey(name: 'structuredMessage', required: true, includeIfNull: false)
final String structuredMessage;
@JsonKey(name: 'beneficiary', required: true, includeIfNull: false)
final String beneficiary;
@JsonKey(name: 'beneficiaryIBAN', required: true, includeIfNull: false)
Expand All @@ -19,6 +17,10 @@ class OGM {
final String someThing;
@JsonKey(name: 'some_ThinG_huGE', required: true, includeIfNull: false)
final String someThinGHuGE;
@JsonKey(name: 'simpleFields', required: true)
final List<Testing> simpleFields;
@JsonKey(name: 'structuredMessage')
final String? structuredMessage;
@JsonKey(name: 'securityIndicator', includeIfNull: false)
final String? securityRole;
@JsonKey(name: 'mutableProperty', includeIfNull: false)
Expand All @@ -27,18 +29,22 @@ class OGM {
final DateTime? dateChange;
@JsonKey(name: 'fields', includeIfNull: false)
final List<List<Testing>>? fields;
@JsonKey(name: 'simpleMap')
final Map<String, Testing>? simpleMap;

OGM({
required this.structuredMessage,
required this.beneficiary,
required this.beneficiaryIBAN,
required this.testTest,
required this.someThing,
required this.someThinGHuGE,
required this.simpleFields,
this.structuredMessage,
this.securityRole,
this.mutableProperty,
this.dateChange,
this.fields,
this.simpleMap,
});

factory OGM.fromJson(Map<String, dynamic> json) => _$OGMFromJson(json);
Expand All @@ -50,42 +56,48 @@ class OGM {
identical(this, other) ||
other is OGM &&
runtimeType == other.runtimeType &&
structuredMessage == other.structuredMessage &&
beneficiary == other.beneficiary &&
beneficiaryIBAN == other.beneficiaryIBAN &&
testTest == other.testTest &&
someThing == other.someThing &&
someThinGHuGE == other.someThinGHuGE &&
simpleFields == other.simpleFields &&
structuredMessage == other.structuredMessage &&
securityRole == other.securityRole &&
mutableProperty == other.mutableProperty &&
dateChange == other.dateChange &&
fields == other.fields;
fields == other.fields &&
simpleMap == other.simpleMap;

@override
int get hashCode =>
structuredMessage.hashCode ^
beneficiary.hashCode ^
beneficiaryIBAN.hashCode ^
testTest.hashCode ^
someThing.hashCode ^
someThinGHuGE.hashCode ^
simpleFields.hashCode ^
structuredMessage.hashCode ^
securityRole.hashCode ^
mutableProperty.hashCode ^
dateChange.hashCode ^
fields.hashCode;
fields.hashCode ^
simpleMap.hashCode;

@override
String toString() => 'OGM{'
'structuredMessage: $structuredMessage, '
'beneficiary: $beneficiary, '
'beneficiaryIBAN: $beneficiaryIBAN, '
'testTest: $testTest, '
'someThing: $someThing, '
'someThinGHuGE: $someThinGHuGE, '
'simpleFields: $simpleFields, '
'structuredMessage: $structuredMessage, '
'securityRole: $securityRole, '
'mutableProperty: $mutableProperty, '
'dateChange: $dateChange, '
'fields: $fields'
'fields: $fields, '
'simpleMap: $simpleMap'
'}';
}

Expand Down
16 changes: 12 additions & 4 deletions example/lib/model/ogm.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions example/lib/model/user/profile/admin_profile_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import 'package:model_generator_example/model/user/testing.dart';

part 'admin_profile_data.g.dart';

@JsonSerializable()
@JsonSerializable(explicitToJson: true)
@immutable
class AdminProfileData extends UserProfileDataExtended {
@JsonKey(name: 'privileges', required: true)
@JsonKey(name: 'privileges', required: true, includeIfNull: false)
final String privileges;

const AdminProfileData({
Expand Down Expand Up @@ -76,3 +76,17 @@ class AdminProfileData extends UserProfileDataExtended {
'personsById: $personsById'
'}';
}

AdminProfileData deserializeAdminProfileData(Map<String, dynamic> json) =>
AdminProfileData.fromJson(json);

Map<String, dynamic> serializeAdminProfileData(AdminProfileData object) =>
object.toJson();

List<AdminProfileData> deserializeAdminProfileDataList(
List<Map<String, dynamic>> jsonList) =>
jsonList.map((json) => AdminProfileData.fromJson(json)).toList();

List<Map<String, dynamic>> serializeAdminProfileDataList(
List<AdminProfileData> objects) =>
objects.map((object) => object.toJson()).toList();
36 changes: 20 additions & 16 deletions example/lib/model/user/profile/admin_profile_data.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions example/lib/model/user/profile/user_profile_data_extended.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import 'package:model_generator_example/model/user/testing.dart';

part 'user_profile_data_extended.g.dart';

@JsonSerializable()
@JsonSerializable(explicitToJson: true)
@immutable
class UserProfileDataExtended extends UserProfileData {
@JsonKey(name: 'additionalField', required: true)
@JsonKey(name: 'additionalField', required: true, includeIfNull: false)
final String additionalField;

const UserProfileDataExtended({
Expand Down Expand Up @@ -73,3 +73,19 @@ class UserProfileDataExtended extends UserProfileData {
'personsById: $personsById'
'}';
}

UserProfileDataExtended deserializeUserProfileDataExtended(
Map<String, dynamic> json) =>
UserProfileDataExtended.fromJson(json);

Map<String, dynamic> serializeUserProfileDataExtended(
UserProfileDataExtended object) =>
object.toJson();

List<UserProfileDataExtended> deserializeUserProfileDataExtendedList(
List<Map<String, dynamic>> jsonList) =>
jsonList.map((json) => UserProfileDataExtended.fromJson(json)).toList();

List<Map<String, dynamic>> serializeUserProfileDataExtendedList(
List<UserProfileDataExtended> objects) =>
objects.map((object) => object.toJson()).toList();
34 changes: 19 additions & 15 deletions example/lib/model/user/profile/user_profile_data_extended.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading