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

@JsonCodable() supports super toJson and fromJson. #89

Merged
merged 5 commits into from
Oct 10, 2024

Conversation

davidmorgan
Copy link
Contributor

@davidmorgan davidmorgan commented Oct 8, 2024

Add checking of the super type for fromJson and toJson, which requires adding query support for constructors and methods.

I just added them flat in Member, there is still the TODO to add more types there, this gives something useful for new types to do, i.e. differentiate constructors, methods and fields :)

This also needs to look up the supertype, @simolus3 I wasn't quite sure where that should go, added to StaticTypeSystem here, or did you have something else in mind? :)

@davidmorgan davidmorgan requested a review from jakemac53 October 8, 2024 12:07
@davidmorgan davidmorgan marked this pull request as ready for review October 8, 2024 12:19
///
/// TODO(davidmorgan): is this the right place to provide access to supertypes?
QualifiedName supertypeOf(QualifiedName name) {
return _constructSuperTypes(_lookupNamed(name.asString), []).first.name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For types, I think this is the right place to put it. A QualifiedName alone is not really a type though, interface types would also need the instantiation. And this method answers class hierarchy queries, which are not exactly the same as type hierarchy queries:

  1. There is no designated "primary" supertype in the type hierarchy since it's not relevant when checking for subtype relationships. Using .first here essentially relies on an implementation detail.
  2. This generates an intermediate fully-resolved parent type when we only care about the name (so at the very least has to run in the none scope I think?)

I absolutely think that they're should be methods to resolve super types of interface types in TypeSystem. I'm not sure if the QualifiedName is close enough to an interface type to also belong here though.
Especially if we care about the type of subclass relationship, maybe it's cleaner to add extends, implements and mixesIn properties on Interface as well?

In my mind, the kinds of question a type system should answer are "is this class, through whatever how many intermediates, a subtype of JsonConverter?". IMHO, "does this class extend from a class defining a fromJson constructor" is a different kind of question that is more related to the AST instead of a type model. But that might just be my opinion, so feel free to disagree or ignore this if it doesn't make sense :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good points, thanks! I agree re: "what does this extend" being more like an AST question than a type model, I think moving this functionality to Interface is the right direction.

There's already a lot going on in this PR so added a TODO pointing to this comment + an item to the short term planning issue.

},
"namedParameters": {
"type": "array",
"description": "The named positional parameters of this member, if it has them.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"description": "The named positional parameters of this member, if it has them.",
"description": "The named parameters of this member, if it has them.",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"$ref": "#/$defs/NamedFunctionTypeParameter"
}
},
"parameters": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused by this one, what does it represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems to be a mistake, removed :)

extension ExecutableElementExtension on ExecutableElement {
List<StaticTypeDesc> requiredPositionalParameters(
AnalyzerTypesToMacros translator, TypeTranslationContext context) {
return parameters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, I would use list literals for this. But, I don't know that we have any specific guideline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

pkgs/_test_macros/lib/json_codable.dart Outdated Show resolved Hide resolved
pkgs/_test_macros/lib/json_codable.dart Show resolved Hide resolved
pkgs/_test_macros/lib/json_codable.dart Show resolved Hide resolved
pkgs/_test_macros/lib/json_codable.dart Show resolved Hide resolved
@davidmorgan davidmorgan mentioned this pull request Oct 8, 2024
@davidmorgan davidmorgan requested a review from jakemac53 October 9, 2024 07:37
@davidmorgan davidmorgan merged commit 87ce3dc into dart-lang:main Oct 10, 2024
45 checks passed
@davidmorgan davidmorgan deleted the json-superclasses branch October 10, 2024 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants