-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[go_router_builder] Proposal: add json support, custom string encoder/decoder #8665
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
base: main
Are you sure you want to change the base?
Conversation
@@ -6,6 +6,9 @@ | |||
/// deep linking, data-driven routes and more. | |||
library go_router; | |||
|
|||
// used for json decoder/encoder |
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.
just import 'dart:convert';
in the file where you use it
class JsonExample { | ||
const JsonExample({required this.id}); | ||
|
||
// mismach toJson |
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.
typo: mismatch
const JsonExample({required this.id}); | ||
|
||
// mismach toJson | ||
factory JsonExample.toJson(dynamic json) { |
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.
do you mean fromJson here ?
|
||
import 'package:go_router/go_router.dart'; | ||
|
||
@TypedGoRoute<BadJson>(path: '/') |
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.
BadJson-> rename it?
you also need to bump versions in packages/go_router_builder/CHANGELOG.md and packages/go_router_builder/pubspec.yaml |
}'''; | ||
} | ||
|
||
bool _isTemplate(InterfaceType type) { |
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.
_isTemplate : maybe rename to _isNestedTemplate?
thank you for contributing the PR! This looks good to me over all with some comments |
|
||
import 'package:go_router/go_router.dart'; | ||
// used for json decoder/encoder | ||
export 'dart:convert' show jsonDecode, jsonEncode; |
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.
i think you can just import it instead of export it?
'`${type.getDisplayString(withNullability: false)}` not have a supported fromJson definition.', | ||
element: type.element, | ||
); | ||
// return false; |
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.
// return false;
is this a typo?
String? decoder; | ||
|
||
final ElementAnnotation? annotation = | ||
metadata?.firstWhereOrNull((ElementAnnotation annotation) { | ||
return annotation.computeConstantValue()?.type?.getDisplayString() == | ||
'CustomParameterCodec'; | ||
}); | ||
if (annotation != null) { | ||
final String? decode = annotation | ||
.computeConstantValue() | ||
?.getField('decode') | ||
?.toFunctionValue() | ||
?.displayName; | ||
final String? encode = annotation | ||
.computeConstantValue() | ||
?.getField('encode') | ||
?.toFunctionValue() | ||
?.displayName; | ||
if (decode != null && encode != null) { | ||
decoder = decode; | ||
} | ||
} |
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.
nit: code here is same as L110-L129, you can extract them into a function to reuse.
String _encode(String fieldName, DartType type) => | ||
'$fieldName${type.ensureNotNull}.toString()'; | ||
String _encode(String fieldName, DartType type, String? customEncoder) { | ||
String encode = '$fieldName${type.ensureNotNull}.toString()'; |
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.
nit: this could be:
if (customEncoder != null) {
return '$customEncoder($encode)';
}
return '$fieldName${type.ensureNotNull}.toString()';
String _encode(String fieldName, DartType type) => | ||
'$fieldName${type.ensureNotNull}.toString()'; | ||
String _encode(String fieldName, DartType type, String? customEncoder) { | ||
String encode = '$fieldName${type.ensureNotNull}.toString()'; |
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.
nit: ditto, and all other _encode
PR looks good to me over all, just left some nit comments :) |
I made a rebase in order to take in account latest changes from main branch |
looks like the ci is not happy |
I think it’s because the annotations requires the changes that I made in go_router_builder. |
they will have to be separate into two prs. I think we try to avoid having one pr that bump the two package at once |
Hi @NearTox , is this PR still on your radar? this pr has to be separated into 2 PRs for go_router and go_router_builder |
Hi @hannah-hyj , sorry for the delayed reply. |
Thanks for the updates, and for contributing @NearTox! It looks like this is currently failing a lot of tests, can you take a look? |
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.
Sorry for the late review, I think the overall approach looks fine, but can you add a example usage for custom codec?
@@ -191,3 +191,46 @@ class Repository { | |||
return FamilyPerson(family: family, person: family.person(pid)); | |||
} | |||
} | |||
|
|||
class JsonExample { |
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.
let's move this to json_example.dart
final ElementAnnotation? annotation = | ||
metadata?.firstWhereOrNull((ElementAnnotation annotation) { | ||
final DartObject? constant = annotation.computeConstantValue(); | ||
return constant?.type?.getDisplayString() == 'CustomParameterCodec'; |
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.
can you add an example for using custom codec?
Add initial json support for use in go_router_builder
Adds annotation that enable custom string encoder/decoder, its enable conversion for base64
This allow custom type conversion for parameters, like mentionated in #117261 and #110781
Pre-launch Checklist
dart format
.)[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style, or this PR is exempt from CHANGELOG changes.///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.