adoroszlai commented on code in PR #12128:
URL: https://github.com/apache/ozone/pull/12128#discussion_r4669997873
##########
hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java:
##########
@@ -240,43 +242,37 @@ public static <T> void
updateConfiguration(ConfigurationTarget config, T configO
}
}
- public static Optional<String> getDefaultValue(Class<?> configClass,
- String fieldName) {
- return findFieldConfigAnnotationByName(configClass, fieldName)
- .map(Config::defaultValue);
- }
+ public static Optional<Config> getConfigByKey(Class<?> configClass, String
key) {
+ for (Class<?> clazz = configClass; clazz != null;) {
+ final List<Config> configs = Stream.of(clazz.getDeclaredFields())
+ .map(f -> f.getAnnotation(Config.class))
+ .filter(c -> c != null && c.key().equals(key))
+ .collect(Collectors.toList());
- public static Optional<String> getKey(Class<?> configClass,
- String fieldName) {
- return findFieldConfigAnnotationByName(configClass, fieldName)
- .map(Config::key);
- }
+ final int size = configs.size();
+ if (size == 1) {
+ return Optional.of(configs.get(0));
+ } else if (size > 1) {
+ throw new IllegalStateException("Duplicated key \"" + key + "\": found
" + size + " fields in " + configClass);
+ }
- public static Optional<ConfigType> getType(Class<?> configClass,
- String fieldName) {
- return findFieldConfigAnnotationByName(configClass, fieldName)
- .map(Config::type);
+ clazz = clazz.getSuperclass();
+ if (Object.class.equals(clazz)) {
+ break;
+ }
+ }
+ return Optional.empty();
}
- private static Optional<Config> findFieldConfigAnnotationByName(
- final Class<?> configClass, String fieldName) {
- Class<?> theClass = configClass;
- while (theClass != null) {
- Optional<Config> config = Stream.of(theClass.getDeclaredFields())
- .filter(f -> f.getName().equals(fieldName))
- .findFirst()
- .map(f -> f.getAnnotation(Config.class));
-
- if (config.isPresent()) {
- return config;
+ public static void assertNoDuplicatedKeys(Class<?> configClass) {
Review Comment:
Sure, it's good to have it here.
[`generate-config-doc`](https://github.com/apache/ozone/actions/runs/45176536852/job/137710301093)
check (which generates markdown from config keys, default values,
descriptions, etc.) fails if there are any duplicate keys globally.
--
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]