zachjsh commented on code in PR #13627:
URL: https://github.com/apache/druid/pull/13627#discussion_r1066355611


##########
server/src/main/java/org/apache/druid/catalog/model/CatalogUtils.java:
##########
@@ -144,4 +188,125 @@ public static <T> List<T> concatLists(
         .flatMap(Collection::stream)
         .collect(Collectors.toList());
   }
+
+  /**
+   * Get a string parameter that can either be null or non-blank.
+   */
+  public static String getNonBlankString(Map<String, Object> args, String 
parameter)
+  {
+    String value = CatalogUtils.getString(args, parameter);
+    if (value != null) {
+      value = value.trim();
+      if (value.isEmpty()) {
+        throw new IAE("%s parameter cannot be a blank string", parameter);
+      }
+    }
+    return value;
+  }
+
+  public static List<String> getUriListArg(Map<String, Object> args, String 
parameter)
+  {
+    String urisString = CatalogUtils.getString(args, parameter);
+    if (Strings.isNullOrEmpty(urisString)) {
+      throw new IAE("One or more values are required for parameter %s", 
parameter);
+    }
+    return stringToList(urisString);
+  }
+
+  public static List<URI> stringToUriList(String uris)
+  {
+    return stringListToUriList(stringToList(uris));
+  }
+
+  /**
+   * Convert a list of strings to a list of {@link URI} objects.
+   */
+  public static List<URI> stringListToUriList(List<String> list)
+  {
+    if (list == null) {
+      return null;
+    }
+    List<URI> uris = new ArrayList<>();
+    for (String strValue : list) {
+      try {
+        uris.add(new URI(strValue));
+      }
+      catch (URISyntaxException e) {
+        throw new IAE(StringUtils.format("Argument [%s] is not a valid URI", 
strValue));
+      }
+    }
+    return uris;
+  }
+
+  /**
+   * Merge the properties for an object using a set of updates in a map. If the
+   * update value is {@code null}, then remove the property in the revised 
set. If the
+   * property is known, use the column definition to merge the values. Else, 
the
+   * update replaces any existing value.
+   * <p>
+   * This method does not validate the properties, except as needed to do a
+   * merge. A separate validation step is done on the final, merged object.
+   */
+  public static Map<String, Object> mergeProperties(
+      final Map<String, PropertyDefn<?>> properties,
+      final Map<String, Object> source,
+      final Map<String, Object> update
+  )
+  {
+    if (update == null) {
+      return source;
+    }
+    if (source == null) {
+      return update;
+    }
+    final Map<String, Object> merged = new HashMap<>(source);
+    for (Map.Entry<String, Object> entry : update.entrySet()) {
+      if (entry.getValue() == null) {
+        merged.remove(entry.getKey());
+      } else {
+        Object value = entry.getValue();
+        final PropertyDefn<?> propDefn = properties.get(entry.getKey());
+        if (propDefn != null) {
+          value = propDefn.merge(merged.get(entry.getKey()), entry.getValue());
+        }
+        merged.put(entry.getKey(), value);
+      }
+    }
+    return merged;
+  }
+
+  public static void validateGranularity(String value)
+  {
+    if (value == null) {
+      return;
+    }
+    Granularity granularity;
+    try {
+      granularity = new PeriodGranularity(new Period(value), null, null);
+    }
+    catch (IllegalArgumentException e) {
+      throw new IAE(StringUtils.format("[%s] is an invalid granularity 
string", value));
+    }
+    if (!GranularityType.isStandard(granularity)) {
+      throw new IAE(
+          "Unsupported segment graularity. "
+          + "Please use an equivalent of these granularities: %s.",
+          Arrays.stream(GranularityType.values())
+                .filter(granularityType -> 
!granularityType.equals(GranularityType.NONE))
+                .map(Enum::name)

Review Comment:
   Should this return the enum name of the Period string? If the user provides 
the period name on the next go, that seems that it will fail the validation 
here, as it wont be  a valid Period?



##########
server/src/main/java/org/apache/druid/catalog/model/CatalogUtils.java:
##########
@@ -144,4 +188,125 @@ public static <T> List<T> concatLists(
         .flatMap(Collection::stream)
         .collect(Collectors.toList());
   }
+
+  /**
+   * Get a string parameter that can either be null or non-blank.
+   */
+  public static String getNonBlankString(Map<String, Object> args, String 
parameter)
+  {
+    String value = CatalogUtils.getString(args, parameter);
+    if (value != null) {
+      value = value.trim();
+      if (value.isEmpty()) {
+        throw new IAE("%s parameter cannot be a blank string", parameter);
+      }
+    }
+    return value;
+  }
+
+  public static List<String> getUriListArg(Map<String, Object> args, String 
parameter)
+  {
+    String urisString = CatalogUtils.getString(args, parameter);
+    if (Strings.isNullOrEmpty(urisString)) {
+      throw new IAE("One or more values are required for parameter %s", 
parameter);
+    }
+    return stringToList(urisString);
+  }
+
+  public static List<URI> stringToUriList(String uris)
+  {
+    return stringListToUriList(stringToList(uris));
+  }
+
+  /**
+   * Convert a list of strings to a list of {@link URI} objects.
+   */
+  public static List<URI> stringListToUriList(List<String> list)
+  {
+    if (list == null) {
+      return null;
+    }
+    List<URI> uris = new ArrayList<>();
+    for (String strValue : list) {
+      try {
+        uris.add(new URI(strValue));
+      }
+      catch (URISyntaxException e) {
+        throw new IAE(StringUtils.format("Argument [%s] is not a valid URI", 
strValue));
+      }
+    }
+    return uris;
+  }
+
+  /**
+   * Merge the properties for an object using a set of updates in a map. If the
+   * update value is {@code null}, then remove the property in the revised 
set. If the
+   * property is known, use the column definition to merge the values. Else, 
the
+   * update replaces any existing value.
+   * <p>
+   * This method does not validate the properties, except as needed to do a
+   * merge. A separate validation step is done on the final, merged object.
+   */
+  public static Map<String, Object> mergeProperties(
+      final Map<String, PropertyDefn<?>> properties,
+      final Map<String, Object> source,
+      final Map<String, Object> update
+  )
+  {
+    if (update == null) {
+      return source;
+    }
+    if (source == null) {
+      return update;
+    }
+    final Map<String, Object> merged = new HashMap<>(source);
+    for (Map.Entry<String, Object> entry : update.entrySet()) {
+      if (entry.getValue() == null) {
+        merged.remove(entry.getKey());
+      } else {
+        Object value = entry.getValue();
+        final PropertyDefn<?> propDefn = properties.get(entry.getKey());
+        if (propDefn != null) {
+          value = propDefn.merge(merged.get(entry.getKey()), entry.getValue());
+        }
+        merged.put(entry.getKey(), value);
+      }
+    }
+    return merged;
+  }
+
+  public static void validateGranularity(String value)
+  {
+    if (value == null) {
+      return;
+    }
+    Granularity granularity;
+    try {
+      granularity = new PeriodGranularity(new Period(value), null, null);
+    }
+    catch (IllegalArgumentException e) {
+      throw new IAE(StringUtils.format("[%s] is an invalid granularity 
string", value));
+    }
+    if (!GranularityType.isStandard(granularity)) {
+      throw new IAE(
+          "Unsupported segment graularity. "
+          + "Please use an equivalent of these granularities: %s.",
+          Arrays.stream(GranularityType.values())
+                .filter(granularityType -> 
!granularityType.equals(GranularityType.NONE))
+                .map(Enum::name)

Review Comment:
   Should this return the enum name as it is or the Period string? If the user 
provides the period name on the next go, that seems that it will fail the 
validation here, as it wont be  a valid Period?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to