mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
implement MetaStringPreprocessor
This commit is contained in:
@@ -21,6 +21,8 @@ dependencies {
|
||||
|
||||
"shadedApi"("commons-io:commons-io:2.6")
|
||||
|
||||
"shadedImplementation"("org.apache.commons:commons-text:1.9")
|
||||
|
||||
|
||||
"compileOnly"("com.google.guava:guava:30.0-jre")
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.dfsek.terra.config.preprocessor;
|
||||
|
||||
import com.dfsek.tectonic.config.Configuration;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.preprocessor.Result;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import org.apache.commons.text.StringSubstitutor;
|
||||
import org.apache.commons.text.lookup.StringLookup;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.util.Map;
|
||||
|
||||
public class MetaStringPreprocessor extends MetaPreprocessor<Meta> {
|
||||
public MetaStringPreprocessor(Map<String, Configuration> configs) {
|
||||
super(configs);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) {
|
||||
if(String.class.equals(t.getType()) && c instanceof String) { // String is final so we use #equals
|
||||
String candidate = (String) c;
|
||||
StringSubstitutor substitutor = new StringSubstitutor(key -> {
|
||||
Object meta = getMetaValue(key);
|
||||
if(!(meta instanceof String)) {
|
||||
throw new LoadException("MetaString template injection candidate must be string, is type " + meta.getClass().getCanonicalName());
|
||||
}
|
||||
return (String) meta;
|
||||
});
|
||||
return (Result<T>) Result.overwrite(substitutor.replace(candidate));
|
||||
}
|
||||
return Result.noOp();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user