zhedoubushishi commented on a change in pull request #2833:
URL: https://github.com/apache/hudi/pull/2833#discussion_r631480189



##########
File path: 
hudi-common/src/main/java/org/apache/hudi/common/config/DefaultHoodieConfig.java
##########
@@ -44,8 +56,93 @@ public static void setDefaultOnCondition(Properties props, 
boolean condition, De
     }
   }
 
+  public static <T> void set(Properties props, ConfigOption<T> cfg, String 
val) {
+    props.setProperty(cfg.key(), val);
+  }
+
+  public static <T> void setDefaultValue(Properties props, ConfigOption<T> 
configOption) {
+    if (!contains(props, configOption)) {
+      Option<String> inferValue = Option.empty();
+      if (configOption.getInferFunc() != null) {
+        inferValue = (Option<String>) configOption.getInferFunc().apply(props);
+      }
+      props.setProperty(configOption.key(), inferValue.isPresent() ? 
inferValue.get() : configOption.defaultValue().toString());
+    }
+  }
+
+  public static <T> void setDefaultValue(Map<String, String> props, 
ConfigOption<T> configOption) {
+    if (!contains(props, configOption)) {
+      Option<String> inferValue = Option.empty();
+      if (configOption.getInferFunc() != null) {
+        inferValue = (Option<String>) configOption.getInferFunc().apply(props);
+      }
+      props.put(configOption.key(), inferValue.isPresent() ? inferValue.get() 
: configOption.defaultValue().toString());
+    }
+  }
+
+  public static <T> boolean contains(Map props, ConfigOption<T> configOption) {
+    if (props.containsKey(configOption.key())) {
+      return true;
+    }
+    return 
Arrays.stream(configOption.getAlternatives()).anyMatch(props::containsKey);
+  }
+
+  public static <T> Option<Object> getRawValue(Map props, ConfigOption<T> 
configOption) {
+    if (props.containsKey(configOption.key())) {
+      return Option.ofNullable(props.get(configOption.key()));
+    }
+    for (String alternative : configOption.getAlternatives()) {
+      if (props.containsKey(alternative)) {
+        LOG.warn(String.format("The configuration key '%s' has been deprecated 
"
+                + "and may be removed in the future. Please use the new key 
'%s' instead.",
+            alternative, configOption.key()));
+        return Option.ofNullable(props.get(alternative));
+      }
+    }
+    return Option.empty();
+  }
+
+  public static <T> String getString(Map props, ConfigOption<T> configOption) {
+    Option<Object> rawValue = getRawValue(props, configOption);
+    return rawValue.map(Object::toString).orElse(null);
+  }
+
+  public static <T> Integer getInt(Map props, ConfigOption<T> configOption) {
+    Option<Object> rawValue = getRawValue(props, configOption);
+    return rawValue.map(v -> Integer.parseInt(v.toString())).orElse(null);
+  }
+
+  public static <T> Boolean getBoolean(Map props, ConfigOption<T> 
configOption) {
+    Option<Object> rawValue = getRawValue(props, configOption);
+    return rawValue.map(v -> Boolean.parseBoolean(v.toString())).orElse(null);
+  }
+
+  public static <T> Long getLong(Map props, ConfigOption<T> configOption) {
+    Option<Object> rawValue = getRawValue(props, configOption);
+    return rawValue.map(v -> Long.parseLong(v.toString())).orElse(null);
+  }
+
+  public static <T> Float getFloat(Map props, ConfigOption<T> configOption) {
+    Option<Object> rawValue = getRawValue(props, configOption);
+    return rawValue.map(v -> Float.parseFloat(v.toString())).orElse(null);
+  }
+
+  public static <T> Double getDouble(Map props, ConfigOption<T> configOption) {
+    Option<Object> rawValue = getRawValue(props, configOption);
+    return rawValue.map(v -> Double.parseDouble(v.toString())).orElse(null);
+  }
+
+  public static <T> String getStringOrDefault(Map props, ConfigOption<T> 
configOption) {
+    Option<Object> rawValue = getRawValue(props, configOption);
+    return rawValue.orElse(configOption.defaultValue().toString()).toString();
+  }
+
+  public static <T> String getStringOrElse(Map props, ConfigOption<T> 
configOption, String defaultVal) {

Review comment:
       Make sense!




-- 
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:
[email protected]


Reply via email to