Skip to content

Commit

Permalink
feat(value): 支持对Enum对象的快速解析
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Jun 23, 2023
1 parent 3a0a8e7 commit eee4a27
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 111 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>3.6.0</version>
<version>3.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public ConfigValueBuilder(@NotNull Class<V> valueClass) {
return from(
Object.class, ConfigDataFunction.identity(),
ConfigValueParser.castObject(valueClass),
ConfigDataFunction.toObject(), ConfigDataFunction.toObject()
s -> {
if (s instanceof Enum<?>) return ((Enum<?>) s).name();
else return s;
}, ConfigDataFunction.toObject()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,67 @@ public interface ConfigDataFunction<T, R> {

default <V> @NotNull ConfigDataFunction<T, V> andThen(@NotNull ConfigDataFunction<? super R, V> after) {
Objects.requireNonNull(after);
return ((data) -> after.parse(parse(data)));
return data -> after.parse(parse(data));
}

@Contract(pure = true)
static <T> @NotNull ConfigDataFunction<T, T> identity() {
return (input) -> input;
return input -> input;
}

@Contract(pure = true)
static <T> @NotNull ConfigDataFunction<T, T> identity(Class<T> type) {
return (input) -> input;
return input -> input;
}

@Contract(pure = true)
static <T, V> @NotNull ConfigDataFunction<T, V> required() {
return (input) -> {
return input -> {
throw new IllegalArgumentException("Please specify the value parser.");
};
}

@Contract(pure = true)
static <T> @NotNull ConfigDataFunction<T, Object> toObject() {
return (input) -> input;
return input -> input;
}

@Contract(pure = true)
static <V> @NotNull ConfigDataFunction<Object, V> castObject(Class<V> valueClass) {
return (input) -> {
return input -> {
if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast value to " + valueClass.getName());
};
}

@Contract(pure = true)
static <V> @NotNull ConfigDataFunction<String, V> castFromString(Class<V> valueClass) {
return (input) -> {
return input -> {
if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
};
}

@Contract(pure = true)
static <T> @NotNull ConfigDataFunction<T, String> castToString() {
return (input) -> {
return input -> {
if (input instanceof String) return (String) input;
else if (input instanceof Enum<?>) return ((Enum<?>) input).name();
else return input.toString();
};
}

@Contract(pure = true)
static <V> @NotNull ConfigDataFunction<String, V> parseString(Class<V> valueClass) {
return (input) -> {
return input -> {
if (valueClass.isInstance(input)) return valueClass.cast(input);
else throw new IllegalArgumentException("Cannot cast string to " + valueClass.getName());
};
}

@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Integer> intValue() {
return (input) -> {
return input -> {
if (input instanceof Integer) {
return (Integer) input;
} else if (input instanceof Number) {
Expand All @@ -83,7 +84,7 @@ public interface ConfigDataFunction<T, R> {

@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Short> shortValue() {
return (input) -> {
return input -> {
if (input instanceof Short) {
return (Short) input;
} else if (input instanceof Number) {
Expand All @@ -94,7 +95,7 @@ public interface ConfigDataFunction<T, R> {

@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Double> doubleValue() {
return (input) -> {
return input -> {
if (input instanceof Double) {
return (Double) input;
} else if (input instanceof Number) {
Expand All @@ -105,7 +106,7 @@ public interface ConfigDataFunction<T, R> {

@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Byte> byteValue() {
return (input) -> {
return input -> {
if (input instanceof Byte) {
return (Byte) input;
} else if (input instanceof Number) {
Expand All @@ -116,7 +117,7 @@ public interface ConfigDataFunction<T, R> {

@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Float> floatValue() {
return (input) -> {
return input -> {
if (input instanceof Float) {
return (Float) input;
} else if (input instanceof Number) {
Expand All @@ -127,7 +128,7 @@ public interface ConfigDataFunction<T, R> {

@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Long> longValue() {
return (input) -> {
return input -> {
if (input instanceof Long) {
return (Long) input;
} else if (input instanceof Number) {
Expand All @@ -138,14 +139,14 @@ public interface ConfigDataFunction<T, R> {

@Contract(pure = true)
static @NotNull ConfigDataFunction<Object, Boolean> booleanValue() {
return (input) -> {
return input -> {
if (input instanceof Boolean) {
return (Boolean) input;
} else if (input instanceof String) {
String s = (String) input;
return Boolean.parseBoolean(s) || "yes".equalsIgnoreCase(s);
} else if (input instanceof Integer) {
return ((Integer) input) == 1;
return (Integer) input == 1;
} else throw new IllegalArgumentException("Cannot cast value to " + Boolean.class.getName());
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ default <V> ConfigValueParser<V, R> compose(@NotNull ConfigDataFunction<? super
}
} else if (Boolean.class.isAssignableFrom(valueClass)) {
input = booleanValue().parse(input, (Boolean) defaultValue);
} else if (Enum.class.isAssignableFrom(valueClass) && input instanceof String) {
String enumName = (String) input;
input = valueClass.getDeclaredMethod("valueOf", String.class).invoke(null, enumName);
}

if (valueClass.isInstance(input)) return valueClass.cast(input);
Expand Down Expand Up @@ -125,6 +128,21 @@ default <V> ConfigValueParser<V, R> compose(@NotNull ConfigDataFunction<? super
return (input, defaultValue) -> ConfigDataFunction.booleanValue().parse(input);
}

@Contract(pure = true)
static @NotNull <E extends Enum<E>> ConfigValueParser<Object, E> enumValue(Class<E> enumClass) {
return (input, defaultValue) -> {
if (input instanceof Enum) {
return enumClass.cast(input);
} else if (input instanceof String) {
return Enum.valueOf(enumClass, (String) input);
} else if (input instanceof Number) {
return enumClass.getEnumConstants()[((Number) input).intValue()];
} else {
throw new IllegalArgumentException("Cannot cast value to " + enumClass.getName());
}
};
}

}


2 changes: 1 addition & 1 deletion demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>easyconfiguration-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>3.6.0</version>
<version>3.7.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.configuration.demo.tests.model.TestModel;

import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.UUID;

Expand All @@ -25,6 +26,8 @@ public class DemoConfiguration extends ConfigurationRoot {
@ConfigPath(root = true)
public static final ConfigValue<Long> TEST_NUMBER = ConfiguredValue.of(Long.class, 1000000L);

public static final ConfigValue<ChronoUnit> TEST_ENUM = ConfiguredValue.of(ChronoUnit.class, ChronoUnit.DAYS);

// 支持通过 Class<?> 变量标注子配置,一并注册。
// 注意: 若对应类也有注解,则优先使用类的注解。
@ConfigPath("other-class-config") //支持通过注解修改子配置的主路径,若不修改则以变量名自动生成。
Expand Down
52 changes: 26 additions & 26 deletions impl/hocon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,17 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyconfiguration-parent</artifactId>
<version>3.6.0</version>
<version>3.7.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>easyconfiguration-hocon</artifactId>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
<artifactId>easyconfiguration-hocon</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
Expand All @@ -63,4 +40,27 @@
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import java.io.IOException;

public class EasyConfiguration {

private EasyConfiguration() {
}

public static HOCONFileConfigProvider from(File file, String source) {
HOCONFileConfigProvider provider = new HOCONFileConfigProvider(file);
try {
Expand All @@ -28,4 +32,5 @@ public static HOCONFileConfigProvider from(String fileName) {
public static HOCONFileConfigProvider from(String fileName, String source) {
return from(new File(fileName), source);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cc.carm.lib.configuration.hocon.exception.HOCONGetValueException;
import cc.carm.lib.configuration.hocon.util.HOCONUtils;
import com.typesafe.config.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -16,7 +17,7 @@
import java.util.List;
import java.util.Set;

public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapper> implements CommentedHOCON {
public class HOCONFileConfigProvider extends FileConfigProvider<HOCONConfigWrapper> {
protected final @NotNull ConfigurationComments comments = new ConfigurationComments();
protected HOCONConfigWrapper configuration;
protected ConfigInitializer<HOCONFileConfigProvider> initializer;
Expand Down Expand Up @@ -48,9 +49,10 @@ public void save() throws IOException {

@Override
protected void onReload() throws ConfigException {
this.configuration = new HOCONConfigWrapper(ConfigFactory.parseFile(this.file, ConfigParseOptions.defaults()
ConfigObject conf = ConfigFactory.parseFile(this.file, ConfigParseOptions.defaults()
.setSyntax(ConfigSyntax.CONF)
.setAllowMissing(false)).root());
.setAllowMissing(false)).root();
this.configuration = new HOCONConfigWrapper(conf);
}

@Override
Expand All @@ -63,15 +65,18 @@ protected void onReload() throws ConfigException {
return this.initializer;
}

@Override
public String serializeValue(@NotNull String key, @NotNull Object value) {
// 带有 key=value 的新空对象
return ConfigFactory.empty()
.withValue(key, ConfigValueFactory.fromAnyRef(value))
.root().render();
}

@Override
public @NotNull Set<String> getKeys() {
return getKeys(null, true);
}

@Contract("null,_->!null")
public @Nullable Set<String> getKeys(@Nullable String sectionKey, boolean deep) {
if (sectionKey == null) { // 当前路径
return HOCONUtils.getKeysFromObject(this.configuration, deep, "");
Expand All @@ -91,12 +96,10 @@ public String serializeValue(@NotNull String key, @NotNull Object value) {
return HOCONUtils.getKeysFromObject(section, deep, "");
}

@Override
public @Nullable Object getValue(@NotNull String key) {
return this.configuration.get(key);
}

@Override
public @Nullable List<String> getHeaderComments(@Nullable String key) {
return this.comments.getHeaderComment(key);
}
Expand Down
Loading

0 comments on commit eee4a27

Please sign in to comment.