mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
create MetaMapPreprocessor
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
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 com.dfsek.terra.api.util.reflection.TypeKey;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.AnnotatedType;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MetaMapPreprocessor extends MetaPreprocessor<Meta> {
|
||||
public MetaMapPreprocessor(Map<String, Configuration> configs) {
|
||||
super(configs);
|
||||
}
|
||||
|
||||
private static final TypeKey<List<String>> STRING_LIST = new TypeKey<>() {};
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) {
|
||||
if(t.getType() instanceof ParameterizedType) {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) t.getType();
|
||||
if(parameterizedType.getRawType() instanceof Class) { // Should always be true but we check anyways
|
||||
Class<?> baseClass = (Class<?>) parameterizedType.getRawType();
|
||||
|
||||
if(Map.class.isAssignableFrom(baseClass) && c instanceof Map) { // List metaconfig
|
||||
Map<Object, Object> map = (Map<Object, Object>) c;
|
||||
|
||||
Map<Object, Object> newMap = new HashMap<>(map);
|
||||
|
||||
if(map.containsKey("<<")) {
|
||||
newMap.putAll(map);
|
||||
newMap.remove("<<"); // Remove placeholder
|
||||
|
||||
List<String> keys = (List<String>) loader.loadType(STRING_LIST.getAnnotatedType(), map.get("<<"));
|
||||
keys.forEach(key -> {
|
||||
Object meta = getMetaValue(key);
|
||||
if(!(meta instanceof Map)) {
|
||||
throw new LoadException("MetaMap injection candidate must be list, is type " + meta.getClass().getCanonicalName());
|
||||
}
|
||||
newMap.putAll((Map<?, ?>) meta);
|
||||
});
|
||||
return (Result<T>) Result.overwrite(newMap);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Result.noOp();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user