use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.
the class ProvidenceConfigUtil method getInMessage.
/**
* Look up a key in the message structure. If the key is not found, return
* the default value. Note that the default value will be converted to the
* type of the declared field, not returned verbatim.
*
* @param message The message to look up into.
* @param key The key to look up.
* @param defValue The default value.
* @return The value found or the default.
* @throws ProvidenceConfigException When unable to get value from message.
*/
static Object getInMessage(PMessage message, final String key, Object defValue) throws ProvidenceConfigException {
String sub = key;
String name;
PMessageDescriptor descriptor = message.descriptor();
while (sub.contains(".")) {
int idx = sub.indexOf(".");
name = sub.substring(0, idx);
sub = sub.substring(idx + 1);
PField field = descriptor.findFieldByName(name);
if (field == null) {
throw new ProvidenceConfigException("Message " + descriptor.getQualifiedName() + " has no field named " + name);
}
PDescriptor fieldDesc = field.getDescriptor();
if (fieldDesc.getType() != PType.MESSAGE) {
throw new ProvidenceConfigException("Field '" + name + "' is not of message type in " + descriptor.getQualifiedName());
}
descriptor = (PMessageDescriptor) fieldDesc;
if (message != null) {
message = (PMessage) message.get(field.getId());
}
}
PField field = descriptor.findFieldByName(sub);
if (field == null) {
throw new ProvidenceConfigException("Message " + descriptor.getQualifiedName() + " has no field named " + sub);
}
if (message == null || !message.has(field.getId())) {
return asType(field.getDescriptor(), defValue);
}
return message.get(field.getId());
}
use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.
the class ProvidenceConfigParser method checkAndParseInternal.
// --- private
private <M extends PMessage<M, F>, F extends PField> Pair<M, Set<String>> checkAndParseInternal(@Nonnull Path configFile, @Nullable M parent, String... includeStack) throws ProvidenceConfigException {
try {
// So we map actual loaded files by the absolute canonical location.
String canonicalFile = readCanonicalPath(configFile).toString();
List<String> stackList = new ArrayList<>();
Collections.addAll(stackList, includeStack);
if (Arrays.binarySearch(includeStack, canonicalFile) >= 0) {
stackList.add(canonicalFile);
throw new ProvidenceConfigException("Circular includes detected: " + String.join(" -> ", stackList.stream().map(p -> new File(p).getName()).collect(Collectors.toList())));
}
stackList.add(canonicalFile);
return parseConfigRecursively(configFile, parent, stackList.toArray(new String[stackList.size()]));
} catch (IOException e) {
if (e instanceof ProvidenceConfigException) {
ProvidenceConfigException pce = (ProvidenceConfigException) e;
if (pce.getFile() == null) {
pce.setFile(configFile.getFileName().toString());
}
throw pce;
}
if (e instanceof TokenizerException) {
TokenizerException te = (TokenizerException) e;
if (te.getFile() == null) {
te.setFile(configFile.getFileName().toString());
}
throw new ProvidenceConfigException(te);
}
throw new ProvidenceConfigException(e, e.getMessage()).setFile(configFile.getFileName().toString());
}
}
use of net.morimekta.providence.config.ProvidenceConfigException in project providence by morimekta.
the class GeneratorOptions method getFactories.
public Map<String, GeneratorFactory> getFactories() {
try {
FactoryLoader<GeneratorFactory> loader = new FactoryLoader<>(GeneratorFactory.MANIFEST_PROPERTY);
Map<String, GeneratorFactory> factories = new TreeMap<>();
factories.put("json", new JsonGeneratorFactory());
File currentDir = currentJarDirectory();
if (currentDir != null) {
File generators = new File(currentDir, "generator");
if (generators.isDirectory()) {
addToMap(factories, loader.getFactories(generators));
}
}
{
ProvidenceTools config = getConfig();
if (config.hasGeneratorPaths()) {
for (String path : config.getGeneratorPaths()) {
addToMap(factories, loader.getFactories(new File(path)));
}
}
}
for (File extra : extraGenerators) {
GeneratorFactory factory = loader.getFactory(extra);
factories.put(factory.generatorName(), factory);
}
return factories;
} catch (ProvidenceConfigException e) {
throw new UncheckedProvidenceConfigException(e);
}
}
Aggregations