adoroszlai commented on a change in pull request #1369:
URL: https://github.com/apache/hadoop-ozone/pull/1369#discussion_r480984328



##########
File path: 
hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigurationReflectionUtil.java
##########
@@ -0,0 +1,71 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hdds.conf;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Optional;
+
+/**
+ * Test the configuration reflection utility class.
+ */
+public class TestConfigurationReflectionUtil {
+
+  @Test
+  public void testClassWithConfigGroup() {
+    Optional<ConfigType> actualType =
+        ConfigurationReflectionUtil.getType(
+            ConfigurationExample.class, "waitTime");
+    Assert.assertTrue(actualType.isPresent());
+    Assert.assertEquals(ConfigType.TIME, actualType.get());
+
+    Optional<String> actualKey =
+        ConfigurationReflectionUtil.getKey(
+        ConfigurationExample.class, "waitTime");
+    Assert.assertTrue(actualKey.isPresent());
+    Assert.assertEquals("ozone.scm.client.wait", actualKey.get());
+
+    Optional<String> actualDefaultValue =
+        ConfigurationReflectionUtil.getDefaultValue(
+            ConfigurationExample.class, "waitTime");
+    Assert.assertTrue(actualKey.isPresent());
+    Assert.assertEquals("30m", actualDefaultValue.get());
+  }
+
+  @Test
+  public void testClassWithoutConfigGroup() {
+    Optional<ConfigType> actualType =
+        ConfigurationReflectionUtil.getType(
+            ConfigurationExampleGrandParent.class, "number");
+    Assert.assertTrue(actualType.isPresent());
+    Assert.assertEquals(ConfigType.AUTO, actualType.get());
+
+    Optional<String> actualKey =
+        ConfigurationReflectionUtil.getKey(
+            ConfigurationExampleGrandParent.class, "number");
+    Assert.assertTrue(actualKey.isPresent());
+    Assert.assertEquals("number", actualKey.get());
+
+    Optional<String> actualDefaultValue =
+        ConfigurationReflectionUtil.getDefaultValue(
+            ConfigurationExampleGrandParent.class, "number");
+    Assert.assertTrue(actualKey.isPresent());

Review comment:
       ```suggestion
       Assert.assertTrue(actualDefaultValue.isPresent());
   ```

##########
File path: 
hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
##########
@@ -240,4 +242,52 @@ private static ConfigType detectConfigType(Class<?> 
parameterType,
       }
     }
   }
+
+  public static Optional<String> getDefaultValue(Class<?> configClass,
+      String fieldName) {
+    Config annotation = findFieldConfigAnnotationByName(configClass,
+        fieldName);
+    if (annotation != null) {
+      return Optional.of(annotation.defaultValue());
+    }
+    return Optional.empty();
+  }
+
+  public static Optional<String> getKey(Class<?> configClass,
+      String fieldName) {
+    ConfigGroup configGroup =
+        configClass.getAnnotation(ConfigGroup.class);
+
+    Config annotation = findFieldConfigAnnotationByName(configClass,
+        fieldName);
+    if (annotation != null) {
+      String key = annotation.key();
+      if (configGroup != null) {
+        key = configGroup.prefix() + "." + annotation.key();
+      }
+      return Optional.of(key);
+    }
+    return Optional.empty();
+  }
+
+  public static Optional<ConfigType> getType(Class<?> configClass,
+      String fieldName) {
+    Config config = findFieldConfigAnnotationByName(configClass,
+        fieldName);
+    if (config != null) {
+      return Optional.of(config.type());
+    }
+    return Optional.empty();
+  }
+
+  private static Config findFieldConfigAnnotationByName(Class<?> configClass,
+      String fieldName) {
+    Optional<Field> field = Stream.of(configClass.getDeclaredFields())
+        .filter(f -> f.getName().equals(fieldName))
+        .findFirst();
+    if (field.isPresent()) {
+      return field.get().getAnnotation(Config.class);
+    }
+    return null;

Review comment:
       `findFieldConfigAnnotationByName` could return `Optional<Config>` 
instead of `Config` or `null`:
   
   ```java
           .findFirst()
           .map(field -> field.getAnnotation(Config.class));
   ```
   
   This would let the other new methods (`getType`, etc.) to be simplified to:
   
   ```java
       return findFieldConfigAnnotationByName(configClass, fieldName)
           .map(Config::type);
   ```

##########
File path: 
hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigurationReflectionUtil.java
##########
@@ -0,0 +1,71 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hdds.conf;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Optional;
+
+/**
+ * Test the configuration reflection utility class.
+ */
+public class TestConfigurationReflectionUtil {

Review comment:
       Can you please also add test cases for:
   
    * "field from parent class" (eg. `ConfigurationExample.class, "secure"`),
    * "non-existent field" (eg. `ConfigurationExample.class, "no-such-field"`), 
and
    * "field without `@Config` annotation" (eg. `ConfigFileAppender.class, 
"document"`)?

##########
File path: 
hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigurationReflectionUtil.java
##########
@@ -0,0 +1,71 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.hadoop.hdds.conf;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Optional;
+
+/**
+ * Test the configuration reflection utility class.
+ */
+public class TestConfigurationReflectionUtil {
+
+  @Test
+  public void testClassWithConfigGroup() {
+    Optional<ConfigType> actualType =
+        ConfigurationReflectionUtil.getType(
+            ConfigurationExample.class, "waitTime");
+    Assert.assertTrue(actualType.isPresent());
+    Assert.assertEquals(ConfigType.TIME, actualType.get());
+
+    Optional<String> actualKey =
+        ConfigurationReflectionUtil.getKey(
+        ConfigurationExample.class, "waitTime");
+    Assert.assertTrue(actualKey.isPresent());
+    Assert.assertEquals("ozone.scm.client.wait", actualKey.get());
+
+    Optional<String> actualDefaultValue =
+        ConfigurationReflectionUtil.getDefaultValue(
+            ConfigurationExample.class, "waitTime");
+    Assert.assertTrue(actualKey.isPresent());

Review comment:
       ```suggestion
       Assert.assertTrue(actualDefaultValue.isPresent());
   ```




----------------------------------------------------------------
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: ozone-issues-h...@hadoop.apache.org

Reply via email to