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

AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields #842

Merged
merged 14 commits into from
Apr 30, 2020

Conversation

anhldbk
Copy link
Contributor

@anhldbk anhldbk commented Mar 8, 2020

Make sure you have checked all steps below.

Jira

Tests

No new tests are needed. I just refactor the code base.

Commits

  • My commits all reference Jira issues in their subject lines. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters (not including Jira issue reference)
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

Documentation

  • In case of new functionality, my PR adds documentation that describes how to use it.
    • All the public functions and the classes in the PR contain Javadoc that explain what it does

@probot-autolabeler probot-autolabeler bot added the Java Pull Requests for Java binding label Mar 8, 2020
@anhldbk
Copy link
Contributor Author

anhldbk commented Mar 10, 2020

@sekikn I tried to fix errors with Travis. Would you please to have a look at this error? https://travis-ci.org/apache/avro/jobs/660406896#L38-L41. I don't know how to troubleshoot it.
Screen Shot 2020-03-10 at 14 28 27

@sekikn
Copy link
Contributor

sekikn commented Mar 16, 2020

That seems an intermittent error and irrelevant to your fix. So just wait for some committer to review your PR or ping them (@Fokko @RyanSkraba could you take a look?). They will rerun the CI if needed.
My comments are as follows:

  • The PR title should follow the format defined in the PR template. i.e., "AVRO-1234: My Avro PR" (not "Avro 2723: ..."). This will automatically create a link to this PR on the corresponding JIRA issue and makes the git commit summaries consistent.
  • It may be a matter of personal preference, but with regard to the parameter order, createSchemaDefaultValue(type, field, fieldSchema) feels more natural to me than createSchemaDefaultValue(type, fieldSchema, field), because fieldSchema is derived from field and field is derived from type.

@anhldbk anhldbk changed the title Avro 2723: Refactor ReflectData to allow derived classes to customize default values for fields Avro-2723: Refactor ReflectData to allow derived classes to customize default values for fields Mar 16, 2020
@anhldbk anhldbk changed the title Avro-2723: Refactor ReflectData to allow derived classes to customize default values for fields AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields Mar 16, 2020
@anhldbk
Copy link
Contributor Author

anhldbk commented Mar 17, 2020

@sekikn Thank you for your thoughtful reviews. I've updated with the comments.

@Fokko I rebased :D Please have a look

@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 3, 2020

@sekikn @Fokko ping ....

@sekikn
Copy link
Contributor

sekikn commented Apr 3, 2020

Your PR looks good to me, but unfortunately I'm just an Avro contributor and not a committer. Sorry 😔

@cutting
Copy link
Contributor

cutting commented Apr 3, 2020

The implementation looks reasonable to me. Can you please add a test of this new functionality? Thanks!

@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 6, 2020

Can you please add a test of this new functionality?

@cutting The refectored code does not change the current logic (all tests are passed). So I think the effective scenario is:

  • derive class ReflectData
  • override the new method
  • assert if things are right

Is that ok?

@cutting
Copy link
Contributor

cutting commented Apr 6, 2020

Yes, that is the sort of test I imagine, a subclass that overrides this new method and a test that validates that the overridden method is called in the expected manner and can achieve the desired effects.

public int f1 = 55;
public String f2 = "a-string";
public List<String> f3 = Arrays.asList("one", "two", "three");
// public User usr = new User();
Copy link
Contributor Author

@anhldbk anhldbk Apr 9, 2020

Choose a reason for hiding this comment

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

@cutting I commented out this field due to an issue with JacksonUtils as described here https://issues.apache.org/jira/browse/AVRO-2775

@cutting
Copy link
Contributor

cutting commented Apr 9, 2020

The test looks good. It also looks like an implementation that's generally useful and should probably be included in Avro, not just in a test, no? If we're concerned about back-compatibility, we could implement it optionally, e.g., maybe adding a ReflectData#setGenerateDefaults(boolean) method or something. Is there a reason not to make this the default behavior in some future release?

@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 10, 2020

@cutting

It also looks like an implementation that's generally useful and should probably be included in Avro, not just in a test, no?

Actually I did implement such things with suggestions from @sekikn. Please have a look at:

So I decided to put a partial implementation in this PR and gradually resolved my issues.

Is that ok?

@cutting
Copy link
Contributor

cutting commented Apr 10, 2020 via email

@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 11, 2020

@cutting

Then we might consider changing the default behavior in a
future release, since using class-specified defaults seems more natural and
expected.

Yep, I like this point.

So to finalize this PR, I'm gonna implement ReflectData#setDefaultsGenerated(boolean) & modify my tests accordingly. Correct?

@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 16, 2020

@cutting Please response ;)

@cutting
Copy link
Contributor

cutting commented Apr 16, 2020

Yes, that sounds like a good plan to me. Thanks.

@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 19, 2020

@cutting Please have a look at my new commits.

Travis failed with an unrelated error (in Ruby)

Screen Shot 2020-04-19 at 15 02 31

@anhldbk anhldbk closed this Apr 20, 2020
@anhldbk anhldbk reopened this Apr 20, 2020
@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 21, 2020

@cutting @Fokko my code is clean now. PTAL.

@cutting
Copy link
Contributor

cutting commented Apr 21, 2020

New public & protected methods need javadoc comments. Also, the new fields should probably be private, not protected.

Might the cache be better as WeakHashMap<Class,Object>?

I don't see where isFieldInitialized is called.

- More Java docs
- All protected fields are marked as private
- Use WeakHashMap instead of HashMap for caching default values
- Rename function `setDefaultValue` into `setDefaultGeneratedValue` to make it clear about the purpose
@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 22, 2020

@cutting Thank you for your thorough comments. I've fixed them.

- `defaultValues` to use WeakHashMap to friendly with GC
- more thorough documentations.

Thank you @cutting for point me out
@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 24, 2020

@cutting I refactored code according your suggestions. Travis complains an issue which is not related to my PR:

Screen Shot 2020-04-24 at 13 53 06

Thank you & PTAL.

@anhldbk
Copy link
Contributor Author

anhldbk commented Apr 27, 2020

@cutting @Fokko would you please have a look at my PR?

@cutting cutting merged commit cfb6a12 into apache:master Apr 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java Pull Requests for Java binding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants