apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brightchen <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: - MLHR-1908 Added helper metho...
Date Tue, 17 Nov 2015 23:46:04 GMT
Github user brightchen commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/103#discussion_r45140443
  
    --- Diff: library/src/main/java/com/datatorrent/lib/appdata/gpo/GPOUtils.java ---
    @@ -178,33 +227,40 @@ public static void setFieldFromJSON(GPOMutable gpo, Type type, String
field, JSO
        */
       public static void setFieldFromJSON(GPOMutable gpo, String field, JSONObject jo)
       {
    -    Type type = gpo.getFieldDescriptor().getType(field);
    +    Object val = getFieldFromJSON(gpo.getFieldDescriptor(), field, jo);
    +    gpo.setFieldGeneric(field, val);
    +  }
     
    -    if(type == Type.BOOLEAN) {
    -      Boolean val;
    +  /**
    +   * This method gets the given field from the given {@link JSONObject} and converts
the field to an object
    +   * of the type specified in the given {@link FieldsDescriptor}.
    +   * @param fd The {@link FieldsDescriptor} describing the type of each field.
    +   * @param field The field to retrieve from the given {@link JSONObject}.
    +   * @param jo The {@link JSONObject} to retrieve a field from.
    +   * @return The value of the given field converted to an object of the correct type.
    +   */
    +  public static Object getFieldFromJSON(FieldsDescriptor fd, String field, JSONObject
jo)
    +  {
    +    Type type = fd.getType(field);
     
    +    if (type == Type.BOOLEAN) {
           try {
    -        val = jo.getBoolean(field);
    -      }
    -      catch(JSONException ex) {
    +        return jo.getBoolean(field);
    +      } catch (JSONException ex) {
             throw new IllegalArgumentException("The key " + field + " does not have a valid
bool value.", ex);
           }
    -
    -      gpo.setFieldGeneric(field, val);
    -    }
    -    else if(type == Type.BYTE) {
    +    } else if (type == Type.BYTE) {
           int val;
     
           try {
             val = jo.getInt(field);
    -      }
    -      catch(JSONException ex) {
    +      } catch (JSONException ex) {
             throw new IllegalArgumentException("The key "
                                                + field
                                                + " does not have a valid byte value.", ex);
           }
     
    -      if(val < (int)Byte.MIN_VALUE) {
    +      if (val < (int)Byte.MIN_VALUE) {
    --- End diff --
    
    what about combine these two logic into one and throw IllegalArgumentException with "Out
of range". 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message