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 all commits
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
58 changes: 45 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ UserModel:
type: int
ignore_equality: true
include:
type: string
type: String
```

## explicit_to_json
Expand Down Expand Up @@ -174,7 +174,7 @@ UserModel:
type: int
default_value: 1
name:
type: string
type: String
required: true
default_value: "'an example quoted string'"
```
Expand Down Expand Up @@ -204,7 +204,7 @@ UserModel:
type: int
default_value: 1
name:
type: string
type: String
required: true
default_value: "'an example quoted string'"
```
Expand All @@ -219,7 +219,7 @@ UserModel:
type: int
default_value: 1
name:
type: string
type: String
required: true
default_value: "'an example quoted string'"
disallow_null: true
Expand Down Expand Up @@ -259,7 +259,20 @@ UserDetails:
extends: UserModel
properties:
name:
type: string
type: String
```

## Builtin types
The following framework types descriptors are known out of the box:
```
string/String
int/integer
bool/boolean
double
date/datetime
dynamic/object/any
array
map
```

## Default setup
Expand All @@ -275,7 +288,7 @@ UserModel:
id:
type: int
name:
type: string
type: String
salary:
type: double
something:
Expand All @@ -286,7 +299,7 @@ UserModel:
roles:
type: array
items:
type: string
type: String
birthday:
type: date
addresses:
Expand All @@ -299,19 +312,19 @@ UserModel:
key: String
value: Address
securityRole:
type: string
type: String
jsonKey: securityIndicator
dynamicField:
type: dynamic
includeIfNullField:
include_if_null: false #If this field is null, this field will not be added to your json object (used for PATCH models)
type: string
type: String
ignoreField:
ignore: false #this field will not be final, and not be used in the json parsing
type: string
type: String
mutableField:
non_final: true #Field will not be marked final
type: string
type: String
changedAt:
type: datetime
idToAddressList:
Expand All @@ -324,15 +337,15 @@ Address:
path: webservice/user #Can also be package:... and/or end with the actual file (.dart)
properties:
street:
type: string
type: String

#Custom base_directory
CustomBaseDirectoryObject:
base_directory: custom_models
path: webservice
properties:
path:
type: string
type: String

#Custom json converter. Use with converters property on models
DateTimeConverter:
Expand All @@ -341,6 +354,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();
Loading