dongjoon-hyun commented on code in PR #12:
URL: 
https://github.com/apache/spark-kubernetes-operator/pull/12#discussion_r1611984015


##########
spark-operator/src/main/java/org/apache/spark/k8s/operator/config/ConfigOption.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.spark.k8s.operator.config;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+
+import org.apache.spark.k8s.operator.utils.ModelUtils;
+
+/** Config options for Spark Operator. Supports primitive and serialized JSON 
*/
+@RequiredArgsConstructor
+@AllArgsConstructor
+@EqualsAndHashCode
+@ToString
+@Builder
+@Slf4j
+public class ConfigOption<T> {
+  @Getter @Builder.Default private boolean enableDynamicOverride = true;
+  @Getter private String key;
+  @Getter private String description;
+  private T defaultValue;
+  private Class<T> typeParameterClass;
+
+  public T getValue() {
+    T resolvedValue = resolveValue();
+    if (log.isDebugEnabled()) {
+      log.debug("Resolved value for property {}={}", key, resolvedValue);
+    }
+    return resolveValue();
+  }
+
+  private T resolveValue() {
+    try {
+      String value = SparkOperatorConfManager.INSTANCE.getValue(key);
+      if (!enableDynamicOverride) {
+        value = SparkOperatorConfManager.INSTANCE.getInitialValue(key);
+      }
+      if (StringUtils.isNotEmpty(value)) {
+        if (typeParameterClass.isPrimitive() || typeParameterClass == 
String.class) {
+          return (T) resolveValueToPrimitiveType(typeParameterClass, value);
+        } else {
+          return ModelUtils.objectMapper.readValue(value, typeParameterClass);
+        }
+      } else {
+        return defaultValue;
+      }
+    } catch (Throwable t) {
+      log.error(
+          "Failed to resolve value for config key {}, using default value {}",
+          key,
+          defaultValue,
+          t);
+      return defaultValue;
+    }
+  }
+
+  public static Object resolveValueToPrimitiveType(Class clazz, String value) {

Review Comment:
   Do we have a plan to support `Unit` in the future like Spark?



-- 
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: reviews-unsubscr...@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to