Skip to content

Commit

Permalink
Improve handling of default Configurator values (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdennnoch authored Jun 3, 2023
1 parent f1ce805 commit 5026e18
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import gg.beemo.latte.config.annotations.ConfiguratorIgnore;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public interface Configurator {
Expand Down
95 changes: 43 additions & 52 deletions latte/src/main/java/gg/beemo/latte/config/ConfiguratorImpl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gg.beemo.latte.config;

import gg.beemo.latte.config.annotations.ConfiguratorIgnore;
import gg.beemo.latte.config.annotations.ConfiguratorDefault;
import gg.beemo.latte.config.annotations.ConfiguratorRedacted;
import gg.beemo.latte.config.annotations.ConfiguratorRename;
import gg.beemo.latte.config.annotations.ConfiguratorRequired;
import gg.beemo.latte.logging.LoggerKt;
Expand All @@ -12,9 +12,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ConfiguratorImpl implements Configurator {
Expand Down Expand Up @@ -81,67 +79,59 @@ public void mirror(Class<?> toClass) {
return;
}

if (field.trySetAccessible()) {
String name = field.getName();

if (field.isAnnotationPresent(ConfiguratorRename.class)) {
name = field.getAnnotation(ConfiguratorRename.class).actualKeyName();
}
String name = field.getName();
if (field.isAnnotationPresent(ConfiguratorRename.class)) {
name = field.getAnnotation(ConfiguratorRename.class).actualKeyName();
}

String value = get(name);
if (value == null) {
if (field.isAnnotationPresent(ConfiguratorDefault.class)) {
value = field.getAnnotation(ConfiguratorDefault.class).defaultValue();
} else {
LOGGER.warn("Configurator failed to find a value for a field. [field={}]", field.getName());
}
String value = get(name);
if (value == null) {
if (field.isAnnotationPresent(ConfiguratorRequired.class)) {
LOGGER.error("Configurator variable {} was not provided!", name);
System.exit(1);
}
return;
}
boolean isRedacted = field.isAnnotationPresent(ConfiguratorRedacted.class);
String logValue = isRedacted ? "*****<REDACTED>*****" : value;
LOGGER.debug("Setting variable {}={}", name, logValue);

try {
Class<?> type = field.getType();

// Sometimes, you may want this to be null?
if (value == null) {
if (field.isAnnotationPresent(ConfiguratorRequired.class)) {
LOGGER.error("{} was not provided during startup!", name);
System.exit(1);
} else {
field.set(field, null);
}
} else {
if (isTypeEither(type, Boolean.class, boolean.class)) {
field.setBoolean(field, Boolean.parseBoolean(value));
} else if (isTypeEither(type, Integer.class, int.class)) {
field.setInt(field, Integer.parseInt(value));
} else if (isTypeEither(type, Long.class, long.class)) {
field.setLong(field, Long.parseLong(value));
} else if (isTypeEither(type, Double.class, double.class)) {
field.setDouble(field, Double.parseDouble(value));
} else if (type.equals(String.class)) {
field.set(field, value);
} else if (adapters.containsKey(type)) {
field.set(field, adapters.get(type).transform(name, value, this));
} else {
LOGGER.error("Configurator failed to find a proper transformer or adapter for a field. " +
"Please use ConfiguratorIgnore to ignore otherwise add an adapter via Configurator.addAdapter(...). [field={}]", field.getName());
System.exit(1);
}
}
if (!field.trySetAccessible()) {
LOGGER.error("Configurator failed to mark field {} as accessible", field.getName());
System.exit(1);
}

} catch (IllegalAccessException | NumberFormatException e) {
LOGGER.error("Configurator encountered an throwable while attempting to convert a field. [field={}]", field.getName(), e);
try {
Class<?> type = field.getType();

if (isTypeEither(type, Boolean.class, boolean.class)) {
field.setBoolean(field, Boolean.parseBoolean(value));
} else if (isTypeEither(type, Integer.class, int.class)) {
field.setInt(field, Integer.parseInt(value));
} else if (isTypeEither(type, Long.class, long.class)) {
field.setLong(field, Long.parseLong(value));
} else if (isTypeEither(type, Double.class, double.class)) {
field.setDouble(field, Double.parseDouble(value));
} else if (type.equals(String.class)) {
field.set(field, value);
} else if (adapters.containsKey(type)) {
field.set(field, adapters.get(type).transform(name, value, this));
} else {
LOGGER.error("Configurator failed to find a proper transformer or adapter for a field. " +
"Please use ConfiguratorIgnore to ignore otherwise add an adapter via Configurator.addAdapter(...). [field={}]", field.getName());
System.exit(1);
}

} else {
LOGGER.error("Configurator failed to set a field accessible. [field={}]", field.getName());
} catch (IllegalAccessException | NumberFormatException e) {
LOGGER.error("Configurator encountered an throwable while attempting to convert a field. [field={}]", field.getName(), e);
System.exit(1);
}

});
}

/**
* An convenient helper method to identify whether the type specified is
* A convenient helper method to identify whether the type specified is
* either of these two. This is used against generic types which always has two
* types of classes.
*
Expand All @@ -164,4 +154,5 @@ private boolean isTypeEither(Class<?> type, Class<?> typeOne, Class<?> typeTwo)
public static void addAdapter(Class<?> clazz, ConfiguratorAdapter<?> adapter) {
adapters.put(clazz, adapter);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gg.beemo.latte.config.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Tells the {@link gg.beemo.latte.config.Configurator} to not log this
* field's value upon initialization.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConfiguratorRedacted {
}
5 changes: 1 addition & 4 deletions vanilla/src/main/java/gg/beemo/vanilla/Config.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package gg.beemo.vanilla;

import gg.beemo.latte.config.annotations.ConfiguratorDefault;

public class Config {

@ConfiguratorDefault(defaultValue = "localhost:9094")
public static String KAFKA_HOST;
public static String KAFKA_HOST = "localhost:9092";

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gg.beemo.vanilla;
package gg.beemo.vanilla

import gg.beemo.latte.kafka.KafkaClient
import gg.beemo.latte.kafka.KafkaConnection
Expand Down

0 comments on commit 5026e18

Please sign in to comment.