mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 22:31:52 +00:00
MetaNumberPreprocessor fixes
This commit is contained in:
+2
-1
@@ -12,10 +12,11 @@ import com.dfsek.terra.api.util.reflection.TypeKey;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.lang.reflect.AnnotatedType;
|
import java.lang.reflect.AnnotatedType;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class MetaNumberPreprocessor extends MetaPreprocessor<Meta> {
|
public class MetaNumberPreprocessor extends MetaPreprocessor<Meta> {
|
||||||
public static final TypeKey<@Meta String> META_STRING_KEY = new TypeKey<>() {};
|
public static final TypeKey<String> META_STRING_KEY = new TypeKey<@Meta String>() {};
|
||||||
|
|
||||||
public MetaNumberPreprocessor(Map<String, Configuration> configs) {
|
public MetaNumberPreprocessor(Map<String, Configuration> configs) {
|
||||||
super(configs);
|
super(configs);
|
||||||
|
|||||||
+3
-3
@@ -24,10 +24,10 @@ public class MetaStringPreprocessor extends MetaPreprocessor<Meta> {
|
|||||||
String candidate = (String) c;
|
String candidate = (String) c;
|
||||||
StringSubstitutor substitutor = new StringSubstitutor(key -> {
|
StringSubstitutor substitutor = new StringSubstitutor(key -> {
|
||||||
Object meta = getMetaValue(key);
|
Object meta = getMetaValue(key);
|
||||||
if(!(meta instanceof String)) {
|
if(!(meta instanceof String) && !(meta instanceof Number) && !(meta instanceof Character) && !(meta instanceof Boolean)) {
|
||||||
throw new LoadException("MetaString template injection candidate must be string, is type " + meta.getClass().getCanonicalName());
|
throw new LoadException("MetaString template injection candidate must be string or primitive, is type " + meta.getClass().getCanonicalName());
|
||||||
}
|
}
|
||||||
return (String) meta;
|
return meta.toString();
|
||||||
});
|
});
|
||||||
return (Result<T>) Result.overwrite(substitutor.replace(candidate));
|
return (Result<T>) Result.overwrite(substitutor.replace(candidate));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.dfsek.tectonic.yaml.YamlConfiguration;
|
|||||||
import com.dfsek.terra.api.config.meta.Meta;
|
import com.dfsek.terra.api.config.meta.Meta;
|
||||||
import com.dfsek.terra.config.preprocessor.MetaListLikePreprocessor;
|
import com.dfsek.terra.config.preprocessor.MetaListLikePreprocessor;
|
||||||
import com.dfsek.terra.config.preprocessor.MetaMapPreprocessor;
|
import com.dfsek.terra.config.preprocessor.MetaMapPreprocessor;
|
||||||
|
import com.dfsek.terra.config.preprocessor.MetaNumberPreprocessor;
|
||||||
import com.dfsek.terra.config.preprocessor.MetaStringPreprocessor;
|
import com.dfsek.terra.config.preprocessor.MetaStringPreprocessor;
|
||||||
import com.dfsek.terra.config.preprocessor.MetaValuePreprocessor;
|
import com.dfsek.terra.config.preprocessor.MetaValuePreprocessor;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -26,9 +27,12 @@ public class MetaTest {
|
|||||||
configurationMap.put(metaTarget.getName(), metaTarget);
|
configurationMap.put(metaTarget.getName(), metaTarget);
|
||||||
|
|
||||||
ConfigLoader loader = new ConfigLoader();
|
ConfigLoader loader = new ConfigLoader();
|
||||||
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
|
||||||
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
|
||||||
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
|
||||||
|
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
|
||||||
|
|
||||||
loader.load(new MetaListConfig(), meta).list.forEach(System.out::println);
|
loader.load(new MetaListConfig(), meta).list.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
@@ -49,9 +53,12 @@ public class MetaTest {
|
|||||||
configurationMap.put(metaTarget.getName(), metaTarget);
|
configurationMap.put(metaTarget.getName(), metaTarget);
|
||||||
|
|
||||||
ConfigLoader loader = new ConfigLoader();
|
ConfigLoader loader = new ConfigLoader();
|
||||||
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
|
||||||
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
|
||||||
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
|
||||||
|
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
|
||||||
|
|
||||||
loader.load(new MetaMapConfig(), meta).map.forEach((k, v) -> System.out.println(k + ": " + v));
|
loader.load(new MetaMapConfig(), meta).map.forEach((k, v) -> System.out.println(k + ": " + v));
|
||||||
}
|
}
|
||||||
@@ -72,10 +79,13 @@ public class MetaTest {
|
|||||||
configurationMap.put(metaTarget.getName(), metaTarget);
|
configurationMap.put(metaTarget.getName(), metaTarget);
|
||||||
|
|
||||||
ConfigLoader loader = new ConfigLoader();
|
ConfigLoader loader = new ConfigLoader();
|
||||||
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
|
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
|
||||||
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
|
||||||
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
|
||||||
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
|
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
|
||||||
|
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
|
||||||
|
|
||||||
System.out.println(loader.load(new MetaStringConfig(), meta).string);
|
System.out.println(loader.load(new MetaStringConfig(), meta).string);
|
||||||
}
|
}
|
||||||
@@ -84,4 +94,34 @@ public class MetaTest {
|
|||||||
@Value("string")
|
@Value("string")
|
||||||
private @Meta String string;
|
private @Meta String string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMetaNumber() {
|
||||||
|
Configuration meta = new YamlConfiguration(MetaTest.class.getResourceAsStream("/meta.yml"), "meta.yml");
|
||||||
|
Configuration metaTarget = new YamlConfiguration(MetaTest.class.getResourceAsStream("/metaTarget.yml"), "metaTarget.yml");
|
||||||
|
|
||||||
|
Map<String, Configuration> configurationMap = new HashMap<>();
|
||||||
|
|
||||||
|
configurationMap.put(meta.getName(), meta);
|
||||||
|
configurationMap.put(metaTarget.getName(), metaTarget);
|
||||||
|
|
||||||
|
ConfigLoader loader = new ConfigLoader();
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
|
||||||
|
|
||||||
|
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
|
||||||
|
|
||||||
|
System.out.println("int: " + loader.load(new MetaNumberConfig(), meta).integer);
|
||||||
|
System.out.println("double: " + loader.load(new MetaNumberConfig(), meta).aDouble);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class MetaNumberConfig implements ConfigTemplate {
|
||||||
|
@Value("int")
|
||||||
|
private @Meta int integer;
|
||||||
|
|
||||||
|
@Value("double")
|
||||||
|
private @Meta double aDouble;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,4 +13,6 @@ map:
|
|||||||
- metaTarget.yml:map2
|
- metaTarget.yml:map2
|
||||||
one: ONE
|
one: ONE
|
||||||
two: TWO
|
two: TWO
|
||||||
string: "one-${metaTarget.yml:string.two}-${metaTarget.yml:string.three}-four-five-${metaTarget.yml:string.six}"
|
string: "one-${metaTarget.yml:string.two}-${metaTarget.yml:string.three}-four-five-${metaTarget.yml:string.six}"
|
||||||
|
int: 2 + 4
|
||||||
|
double: ${metaTarget.yml:double} + 5.6
|
||||||
@@ -13,4 +13,5 @@ map2:
|
|||||||
string:
|
string:
|
||||||
two: two
|
two: two
|
||||||
three: three
|
three: three
|
||||||
six: six
|
six: six
|
||||||
|
double: 1
|
||||||
Reference in New Issue
Block a user