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
feat: Add JsonSerializable(createJsonMeta) #1164
Changes from 3 commits
e386254
dc749c1
105ab22
3f9bbad
604faa0
d0bda6a
108ab46
4b76cf8
5d00aad
06c1640
eda3fc7
d3aeb2d
6006a3e
4a0a053
a5c6b66
6bf0987
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,26 @@ import 'unsupported_type_error.dart'; | |
abstract class EncodeHelper implements HelperCore { | ||
String _fieldAccess(FieldElement field) => '$_toJsonParamName.${field.name}'; | ||
|
||
/// Generates an object containing metadatas related to the encoding, | ||
/// destined to be used by other code-generators. | ||
String createJsonMeta(Set<FieldElement> accessibleFieldSet) { | ||
assert(config.createJsonMeta); | ||
|
||
final buffer = | ||
StringBuffer('const _\$${element.name}JsonMeta = <String, String> {'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Careful using I have a helper around here somewhere for this" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I haven't seen another util for this |
||
|
||
for (final field in accessibleFieldSet) { | ||
buffer.writeln( | ||
'${escapeDartString(field.name)}: ' | ||
'${escapeDartString(nameAccess(field))},', | ||
); | ||
} | ||
|
||
buffer.write('};'); | ||
|
||
return buffer.toString(); | ||
} | ||
|
||
Iterable<String> createToJson(Set<FieldElement> accessibleFields) sync* { | ||
assert(config.createToJson); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'json_meta_example.g.dart'; | ||
|
||
@JsonSerializable(createJsonMeta: true, fieldRename: FieldRename.kebab) | ||
class Model { | ||
Model({ | ||
required this.firstName, | ||
required this.lastName, | ||
this.ignoredName, | ||
}); | ||
|
||
factory Model.fromJson(Map<String, Object?> json) => _$ModelFromJson(json); | ||
|
||
final String firstName; | ||
|
||
@JsonKey(name: 'LAST_NAME') | ||
final String lastName; | ||
|
||
@JsonKey(ignore: true) | ||
final String? ignoredName; | ||
|
||
String get fullName => '$firstName $lastName'; | ||
|
||
Map<String, Object?> toJson() => _$ModelToJson(this); | ||
} | ||
|
||
const modelMeta = _$ModelJsonMeta; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"meta" is pretty vague here. Maybe
createFieldMap
? Let's talk before you start rewriting everything...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xFieldMap sounds good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done