Skip to content

feature: allow customizing enum value serialization via annotations #251

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
Jun 29, 2018

Conversation

kevmoo
Copy link
Collaborator

@kevmoo kevmoo commented Jun 28, 2018

feature: allow customizing enum value serialization via annotations

  • Require the latest Dart SDK
    Annotated enums fail without the CFE

  • Require the latest pkg:analyzer
    Enum metadata parsing was just recently added

  • Require the latest pkg:source_gen
    Uses multiple return values to allow generating shared helpers

  • Removed several helpers in json_annotation
    They can now be generated as part of the source, which minimizes
    versioning issues with helpers.

  • Small, but breaking changes to Generator API to return multiple
    values

  • Added addMember API to Context classes to allow helpers to be
    generated.

  • Updated integration test code to include an annotated enum

  • Updated yaml test code to remove now superfluous convert logic and use
    annotated enums instead

Fixes #38

@kevmoo kevmoo requested review from jakemac53 and natebosch June 28, 2018 00:02
@kevmoo
Copy link
Collaborator Author

kevmoo commented Jun 28, 2018

Question for reviewers (@natebosch , @jakemac53 )

Should I create another annotation class – JsonEnumValue or similar?

@sir-boformer
Copy link

Just to give some reasoning: Integers are the default representation of enums in many other languages (e.g. C#, Kotlin, TypeScript), and it's also common to serialize enum values as integers, as it is a very efficient format.

* Require the latest Dart SDK
  Annotated enums fail without the CFE

* Require the latest pkg:analyzer
  Enum metadata parsing was just recently added

* Require the latest pkg:source_gen
  Uses multiple return values to allow generating shared helpers

* Removed several helpers in json_annotation
  They can now be generated as part of the source, which minimizes
  versioning issues with helpers.

* Small, but breaking changes to Generator API to return multiple
  values

* Added `addMember` API to Context classes to allow helpers to be
  generated.

* Updated integration test code to include an annotated enum

* Updated yaml test code to remove now superfluous convert logic and use
  annotated enums instead

Fixes #38
@kevmoo kevmoo force-pushed the i38_enum_annotations branch from 21b6749 to dd8f50b Compare June 28, 2018 16:41
@kevmoo
Copy link
Collaborator Author

kevmoo commented Jun 28, 2018

@sir-boformer – added JsonValue class which supports String and int values.

@jakemac53 – ready for review

///
/// Can be a [String] or an [int].
final dynamic value;
const JsonValue(this.value);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: newline above here?

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

return null;
}

context.addMember(_enumDecodeHelper);
Copy link
Collaborator

Choose a reason for hiding this comment

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

So we will actually be adding one of these each time this helper is called right? Is that once per field using the enum or more than that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

once per field using the enum – but then they are collapsed.

The storage is a set, so it's only recorded once.

Copy link
Collaborator

Choose a reason for hiding this comment

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

kk

/// If [targetType] is not an enum, `null` is returned.
Map<FieldElement, dynamic> enumFieldsMap(DartType targetType) {
if (targetType is InterfaceType && targetType.element.isEnum) {
var value = _enumMapExpando[targetType];
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: return _enumMapExpando[targetType] ??= ...?

Copy link
Collaborator

Choose a reason for hiding this comment

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

also, I would consider taking moving big anonymous closure out to a private method or even just a local variable so its easier to reason about what is happening here

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
Copy link
Collaborator Author

kevmoo commented Jun 29, 2018

@jakemac53 – PTAL

@kevmoo kevmoo merged commit 7a550cc into master Jun 29, 2018
@kevmoo kevmoo deleted the i38_enum_annotations branch June 29, 2018 21:29
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