Skip to content

Add support for generic classes #169

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

Merged
merged 2 commits into from
May 22, 2018
Merged

Add support for generic classes #169

merged 2 commits into from
May 22, 2018

Conversation

kevmoo
Copy link
Collaborator

@kevmoo kevmoo commented May 22, 2018

...and some name cleanup.

I will land these are separate commits, @natebosch 😄

@kevmoo kevmoo requested review from jakemac53 and natebosch May 22, 2018 04:28
@kevmoo kevmoo force-pushed the i116_generic_fun branch from 4b0b24b to 0184ef8 Compare May 22, 2018 04:29
@natebosch
Copy link
Member

I will land these are separate commits,

Why couldn't they be separate PRs?

@natebosch
Copy link
Member

Also, why does a commit which adds over 400 lines have a 5 word commit message?

@JsonSerializable()
class GenericClass<T extends num, S> extends Object
with _$GenericClassSerializerMixin<T, S> {
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson)
Copy link
Collaborator

Choose a reason for hiding this comment

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

are _dataFromJson and _dataToJson required for this to work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup. Will document as such.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

String _wrapperClassName([bool withConstraints]) =>
'${_prefix}JsonMapWrapper${_genericClassArguments(withConstraints)}';

String _genericClassArguments(bool withConstraints) {
Copy link
Member

Choose a reason for hiding this comment

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

Doc comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack

@@ -39,8 +39,20 @@ class _GeneratorHelper {
final StringBuffer _buffer = new StringBuffer();

String get _prefix => '_\$${_element.name}';
String get _mixClassName => '${_prefix}SerializerMixin';
String get _helpClassName => '${_prefix}JsonMapWrapper';
String _mixinClassName(bool withConstraints) =>
Copy link
Member

Choose a reason for hiding this comment

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

consider making this 2 separate methods (or getters) rather than using a flag argument. The names will be more clear about what is happening than a cryptic "true" or "false" for which you need to see the argument name to understand.

final $_mixClassName _v;
$_helpClassName(this._v);
_buffer
.writeln('''class ${_wrapperClassName(true)} extends \$JsonMapWrapper {
Copy link
Member

Choose a reason for hiding this comment

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

with writeln you're going to get an extra newline and this already ends in a newline. Do we need to?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Honestly, dartfmt handles most of this. Not going to modify this in this CL

@@ -175,7 +175,19 @@ int _sortByLocation(FieldElement a, FieldElement b) {

final _dartCoreObjectChecker = const TypeChecker.fromRuntime(Object);

/// If a parameter is required to invoke the constructor,
String genericClassArguments(ClassElement element, bool withConstraints) {
Copy link
Member

Choose a reason for hiding this comment

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

we seem to be relying on withConstraints being non-null here because of another method with the same name handling nullability. Maybe handle them both here and skip the second identically named wrapper method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ack. Going to leave both. Having the helper in generator_helper is nice.

Did forward the null check to the inner method

@@ -175,7 +175,19 @@ int _sortByLocation(FieldElement a, FieldElement b) {

final _dartCoreObjectChecker = const TypeChecker.fromRuntime(Object);

/// If a parameter is required to invoke the constructor,
String genericClassArguments(ClassElement element, bool withConstraints) {
Copy link
Member

Choose a reason for hiding this comment

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

Doc comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack

}
var values = element.typeParameters.map((t) {
if (withConstraints) {
return t.toString();
Copy link
Member

Choose a reason for hiding this comment

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

consider ternary conditional when what you care about is the value rather than side effects

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack

@@ -243,7 +255,10 @@ CtorData writeConstructorInvocation(
writeableFields.toSet().difference(usedCtorParamsAndFields);

var buffer = new StringBuffer();
buffer.write('new $className(');
//
// Generate the static factory method
Copy link
Member

Choose a reason for hiding this comment

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

should this be a separate method named generateStaticFactory?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is an old comment. This does not create a factory. It creates an invocation. Will remove.

@@ -0,0 +1,41 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
Copy link
Member

Choose a reason for hiding this comment

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

2018

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ack

@kevmoo kevmoo force-pushed the i116_generic_fun branch from 0184ef8 to 88bce47 Compare May 22, 2018 18:32
@kevmoo kevmoo force-pushed the i116_generic_fun branch from 88bce47 to 87c5e2c Compare May 22, 2018 18:49
@kevmoo kevmoo merged commit 87c5e2c into master May 22, 2018
@kevmoo kevmoo deleted the i116_generic_fun branch May 22, 2018 18:49
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