Skip to content

Made generator set field to the initial value if null #115

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

Closed
wants to merge 1 commit into from
Closed

Made generator set field to the initial value if null #115

wants to merge 1 commit into from

Conversation

AlexanderJohr
Copy link
Contributor

Hello Kevin Moore, from all the serialization libraries for dart out there, yours was always my favorite. But for me, it was lacking 2 major features: an ignore annotation and keep initial values if the deserialized field is null. I created two branches in this fork which include these features. With them, it's all I wanted from a serialization library for dart for a long time and I use it with joy already, but I want to share my joy with the community.

I want to add examples, tests, how to guides in the readme and so on. I also have to run the tests, which is difficult for me under windows, because the tools don't work so well and I'm lacking time to do it on Linux.

The reason for those pull requests is that I want to get in touch with you first and get feedback on how to change or rename certain things.

This pull request is for the feature that the generator sets the field to the initial value if the deserialized value is missing or null.

Without it all values are written to the fields even if the deserialized value is null or not found:

Column _$ColumnFromJson(Map<String, dynamic> json) =>
    new Column(containerId: json['containerId'] as String)
      ..settings =
          new ColumnSettings.fromJson(json['settings'] as Map<String, dynamic>);

With it, it checks if the deserialized value is null and if so it assigns the value that was already assigned to it.

Column _$ColumnFromJson(Map<String, dynamic> json) {
  Column column = new Column(containerId: json['containerId'] as String);
  column
    ..settings =
        new ColumnSettings.fromJson(json['settings'] as Map<String, dynamic>) ??
            column.settings;
  return column;
}

What I want to know is the following:

  • This feature currently ignores the nullable annotation at all. This is because I don't know if there should be another name for the annotation as nullable already has the purpose to accept null values and let the user do the handling.
  • same as includeIfNull = true, nullable = false causes _writeJsonValueNaive to be true and reduces the code size as null checks can be omitted. But in this case, nullable = false increases the code size of the FromJson factory constructor. this can't be used as in ..settings = valueFromJson ?? this.settings; because this.settings doesn't exist in the mixing but in the class that uses the mixin. An extra object is needed for that and like that it's not a single statement anymore which adds the return statement and the curly braces.
  • it would also be possible or even more performant to generate if(valueFromJson != null) blocks, because an extra assignment could be prevented like this but it would also increase the code size.

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.

@kevmoo
Copy link
Collaborator

kevmoo commented Mar 21, 2018

@AlexanderJohr – are you running with this one, too?

@AlexanderJohr
Copy link
Contributor Author

I guess I better close it for now and try it again, when my first pull request is finished.

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