You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by GitBox <gi...@apache.org> on 2020/04/20 09:19:09 UTC

[GitHub] [avro] anhldbk opened a new pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

anhldbk opened a new pull request #842:
URL: https://github.com/apache/avro/pull/842


   Make sure you have checked _all_ steps below.
   
   ### Jira
   
   - [x] My PR addresses the following [Avro-2723](https://issues.apache.org/jira/browse/AVRO-2723)
   
   ### Tests
   
   No new tests are needed. I just refactor the code base.
   
   ### Commits
   
   - [x] 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](https://chris.beams.io/posts/git-commit/)":
     1. Subject is separated from body by a blank line
     1. Subject is limited to 50 characters (not including Jira issue reference)
     1. Subject does not end with a period
     1. Subject uses the imperative mood ("add", not "adding")
     1. Body wraps at 72 characters
     1. Body explains "what" and "why", not "how"
   
   ### Documentation
   
   - [x] 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
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] cutting commented on a change in pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
cutting commented on a change in pull request #842:
URL: https://github.com/apache/avro/pull/842#discussion_r413383635



##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -500,6 +582,37 @@ static boolean isNonStringMapSchema(Schema s) {
     return false;
   }
 
+  /**
+   * Get default value for a schema field. Derived classes can override this
+   * method to provide values based on object instantiation
+   *
+   * @param type        Type
+   * @param field       Field
+   * @param fieldSchema Schema of the field
+   * @return The default value
+   */
+  protected Object createSchemaDefaultValue(Type type, Field field, Schema fieldSchema) {
+    Object defaultValue;
+    if (defaultGenerated) {
+      defaultValue = getOrCreateDefaultValue(type, field);
+      if (defaultValue != null) {
+        return defaultValue;

Review comment:
       Should we instead return deepCopy(fieldSchema, defaultValue)?

##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -110,14 +112,96 @@ public static ReflectData get() {
   }
 
   /**
-   * Cause a class to be treated as though it had an {@link Stringable}
-   ** annotation.
+   * Cause a class to be treated as though it had an {@link Stringable} *
+   * annotation.
    */
   public ReflectData addStringable(Class c) {
     stringableClasses.add(c);
     return this;
   }
 
+  /**
+   * If this flag is set to true, default values for fields will be assigned
+   * dynamically using Java reflections. Let's call this feature `default
+   * reflection`. Initially this feature is disabled
+   */
+  private boolean defaultGenerated = false;
+
+  /**
+   * Enable or disable `default reflection`
+   *
+   * @param enabled set to `true` to enable the feature. This feature is disabled
+   *                by default
+   * @return The current instance
+   */
+  public ReflectData setDefaultsGenerated(boolean enabled) {
+    this.defaultGenerated = enabled;
+    return this;
+  }
+
+  private final Map<String, Object> defaultValues = new WeakHashMap<>();

Review comment:
       This needs to be <Class,Object> so that defaults are GC'd when a class is.  Also, Class should probably be used instead of String for classes in other code below.

##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -110,14 +112,96 @@ public static ReflectData get() {
   }
 
   /**
-   * Cause a class to be treated as though it had an {@link Stringable}
-   ** annotation.
+   * Cause a class to be treated as though it had an {@link Stringable} *
+   * annotation.
    */
   public ReflectData addStringable(Class c) {
     stringableClasses.add(c);
     return this;
   }
 
+  /**
+   * If this flag is set to true, default values for fields will be assigned
+   * dynamically using Java reflections. Let's call this feature `default

Review comment:
       We might say a bit more about how this works, that, when enabled, defaults are the field values of an instance created with a no-arg constructor.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] anhldbk commented on pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
anhldbk commented on pull request #842:
URL: https://github.com/apache/avro/pull/842#issuecomment-619706282


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


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] cutting commented on issue #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
cutting commented on issue #842:
URL: https://github.com/apache/avro/pull/842#issuecomment-617319686


   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.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] anhldbk commented on pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
anhldbk commented on pull request #842:
URL: https://github.com/apache/avro/pull/842#issuecomment-618837638


   @cutting I refactored code according your suggestions. Travis complains an issue which is not related to my PR:
   
   <img width="1027" alt="Screen Shot 2020-04-24 at 13 53 06" src="https://user-images.githubusercontent.com/3270746/80183550-3b072700-8633-11ea-9095-5a244db8c7ca.png">
   
   Thank you & PTAL.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] anhldbk commented on issue #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
anhldbk commented on issue #842:
URL: https://github.com/apache/avro/pull/842#issuecomment-617786652


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


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] anhldbk commented on a change in pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
anhldbk commented on a change in pull request #842:
URL: https://github.com/apache/avro/pull/842#discussion_r413445873



##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -110,14 +112,96 @@ public static ReflectData get() {
   }
 
   /**
-   * Cause a class to be treated as though it had an {@link Stringable}
-   ** annotation.
+   * Cause a class to be treated as though it had an {@link Stringable} *
+   * annotation.
    */
   public ReflectData addStringable(Class c) {
     stringableClasses.add(c);
     return this;
   }
 
+  /**
+   * If this flag is set to true, default values for fields will be assigned
+   * dynamically using Java reflections. Let's call this feature `default
+   * reflection`. Initially this feature is disabled
+   */
+  private boolean defaultGenerated = false;
+
+  /**
+   * Enable or disable `default reflection`
+   *
+   * @param enabled set to `true` to enable the feature. This feature is disabled
+   *                by default
+   * @return The current instance
+   */
+  public ReflectData setDefaultsGenerated(boolean enabled) {
+    this.defaultGenerated = enabled;
+    return this;
+  }
+
+  private final Map<String, Object> defaultValues = new WeakHashMap<>();

Review comment:
       @cutting Agree. One small question: can I use `Map<Type, Object>` instead?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] anhldbk commented on a change in pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
anhldbk commented on a change in pull request #842:
URL: https://github.com/apache/avro/pull/842#discussion_r414276723



##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -500,6 +582,37 @@ static boolean isNonStringMapSchema(Schema s) {
     return false;
   }
 
+  /**
+   * Get default value for a schema field. Derived classes can override this
+   * method to provide values based on object instantiation
+   *
+   * @param type        Type
+   * @param field       Field
+   * @param fieldSchema Schema of the field
+   * @return The default value
+   */
+  protected Object createSchemaDefaultValue(Type type, Field field, Schema fieldSchema) {
+    Object defaultValue;
+    if (defaultGenerated) {
+      defaultValue = getOrCreateDefaultValue(type, field);
+      if (defaultValue != null) {
+        return defaultValue;

Review comment:
       Yep, we should.

##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -110,14 +112,96 @@ public static ReflectData get() {
   }
 
   /**
-   * Cause a class to be treated as though it had an {@link Stringable}
-   ** annotation.
+   * Cause a class to be treated as though it had an {@link Stringable} *
+   * annotation.
    */
   public ReflectData addStringable(Class c) {
     stringableClasses.add(c);
     return this;
   }
 
+  /**
+   * If this flag is set to true, default values for fields will be assigned
+   * dynamically using Java reflections. Let's call this feature `default

Review comment:
       I fixed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] cutting commented on a change in pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
cutting commented on a change in pull request #842:
URL: https://github.com/apache/avro/pull/842#discussion_r414034622



##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -110,14 +112,96 @@ public static ReflectData get() {
   }
 
   /**
-   * Cause a class to be treated as though it had an {@link Stringable}
-   ** annotation.
+   * Cause a class to be treated as though it had an {@link Stringable} *
+   * annotation.
    */
   public ReflectData addStringable(Class c) {
     stringableClasses.add(c);
     return this;
   }
 
+  /**
+   * If this flag is set to true, default values for fields will be assigned
+   * dynamically using Java reflections. Let's call this feature `default
+   * reflection`. Initially this feature is disabled
+   */
+  private boolean defaultGenerated = false;
+
+  /**
+   * Enable or disable `default reflection`
+   *
+   * @param enabled set to `true` to enable the feature. This feature is disabled
+   *                by default
+   * @return The current instance
+   */
+  public ReflectData setDefaultsGenerated(boolean enabled) {
+    this.defaultGenerated = enabled;
+    return this;
+  }
+
+  private final Map<String, Object> defaultValues = new WeakHashMap<>();

Review comment:
       Sure, that seems reasonable.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] anhldbk commented on issue #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
anhldbk commented on issue #842:
URL: https://github.com/apache/avro/pull/842#issuecomment-617120834


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


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [avro] anhldbk commented on a change in pull request #842: AVRO-2723: Refactor ReflectData to allow derived classes to customize default values for fields

Posted by GitBox <gi...@apache.org>.
anhldbk commented on a change in pull request #842:
URL: https://github.com/apache/avro/pull/842#discussion_r414277043



##########
File path: lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
##########
@@ -110,14 +112,96 @@ public static ReflectData get() {
   }
 
   /**
-   * Cause a class to be treated as though it had an {@link Stringable}
-   ** annotation.
+   * Cause a class to be treated as though it had an {@link Stringable} *
+   * annotation.
    */
   public ReflectData addStringable(Class c) {
     stringableClasses.add(c);
     return this;
   }
 
+  /**
+   * If this flag is set to true, default values for fields will be assigned
+   * dynamically using Java reflections. Let's call this feature `default
+   * reflection`. Initially this feature is disabled
+   */
+  private boolean defaultGenerated = false;
+
+  /**
+   * Enable or disable `default reflection`
+   *
+   * @param enabled set to `true` to enable the feature. This feature is disabled
+   *                by default
+   * @return The current instance
+   */
+  public ReflectData setDefaultsGenerated(boolean enabled) {
+    this.defaultGenerated = enabled;
+    return this;
+  }
+
+  private final Map<String, Object> defaultValues = new WeakHashMap<>();

Review comment:
       Fine for me :D 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org