-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloud_firestore_odm): add support for json_serializable's field …
…rename/property ignore (#9030)
- Loading branch information
1 parent
f3a6bdc
commit 81ec08f
Showing
15 changed files
with
2,498 additions
and
87 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/cloud_firestore_odm/cloud_firestore_odm/example/build.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
targets: | ||
$default: | ||
builders: | ||
json_serializable: | ||
options: | ||
create_field_map: true |
91 changes: 91 additions & 0 deletions
91
packages/cloud_firestore_odm/cloud_firestore_odm/example/lib/integration.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'integration.g.dart'; | ||
|
||
@JsonSerializable() | ||
class EmptyModel { | ||
EmptyModel(); | ||
|
||
factory EmptyModel.fromJson(Map<String, dynamic> json) => | ||
_$EmptyModelFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$EmptyModelToJson(this); | ||
} | ||
|
||
@Collection<EmptyModel>('firestore-example-app/test/config') | ||
final emptyModelRef = EmptyModelCollectionReference(); | ||
|
||
@Collection<ManualJson>('root') | ||
class ManualJson { | ||
ManualJson(this.value); | ||
|
||
factory ManualJson.fromJson(Map<String, Object?> json) { | ||
return ManualJson(json['value']! as String); | ||
} | ||
|
||
final String value; | ||
|
||
Map<String, Object?> toJson() => {'value': value}; | ||
} | ||
|
||
@Collection<AdvancedJson>('firestore-example-app/test/advanced') | ||
@JsonSerializable(fieldRename: FieldRename.snake) | ||
class AdvancedJson { | ||
AdvancedJson({this.firstName, this.lastName, this.ignored}); | ||
|
||
final String? firstName; | ||
|
||
@JsonKey(name: 'LAST_NAME') | ||
final String? lastName; | ||
|
||
@JsonKey(ignore: true) | ||
final String? ignored; | ||
|
||
Map<String, Object?> toJson() => _$AdvancedJsonToJson(this); | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
return other is AdvancedJson && | ||
other.lastName == lastName && | ||
other.firstName == firstName && | ||
other.ignored == ignored; | ||
} | ||
|
||
@override | ||
int get hashCode => Object.hashAll([firstName, lastName, ignored]); | ||
} | ||
|
||
// This tests that the generated code compiles | ||
@Collection<_PrivateAdvancedJson>('firestore-example-app/test/private-advanced') | ||
@JsonSerializable(fieldRename: FieldRename.snake) | ||
class _PrivateAdvancedJson { | ||
_PrivateAdvancedJson({ | ||
this.firstName, | ||
this.lastName, | ||
// ignore: unused_element | ||
this.ignored, | ||
}); | ||
|
||
final String? firstName; | ||
|
||
@JsonKey(name: 'LAST_NAME') | ||
final String? lastName; | ||
|
||
@JsonKey(ignore: true) | ||
final String? ignored; | ||
|
||
Map<String, Object?> toJson() => _$PrivateAdvancedJsonToJson(this); | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
return other is AdvancedJson && | ||
other.lastName == lastName && | ||
other.firstName == firstName && | ||
other.ignored == ignored; | ||
} | ||
|
||
@override | ||
int get hashCode => Object.hashAll([firstName, lastName, ignored]); | ||
} |
Oops, something went wrong.