Search in sources :

Example 11 with ProvidenceConfigException

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());
}
Also used : PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PField(net.morimekta.providence.descriptor.PField) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Example 12 with ProvidenceConfigException

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());
    }
}
Also used : ArrayList(java.util.ArrayList) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) IOException(java.io.IOException) ProvidenceConfigUtil.resolveFile(net.morimekta.providence.config.impl.ProvidenceConfigUtil.resolveFile) File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException)

Example 13 with ProvidenceConfigException

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);
    }
}
Also used : GeneratorFactory(net.morimekta.providence.generator.GeneratorFactory) JsonGeneratorFactory(net.morimekta.providence.generator.format.json.JsonGeneratorFactory) FactoryLoader(net.morimekta.providence.generator.util.FactoryLoader) ProvidenceTools(net.morimekta.providence.tools.common.ProvidenceTools) JsonGeneratorFactory(net.morimekta.providence.generator.format.json.JsonGeneratorFactory) TreeMap(java.util.TreeMap) UncheckedProvidenceConfigException(net.morimekta.providence.config.UncheckedProvidenceConfigException) File(java.io.File) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) UncheckedProvidenceConfigException(net.morimekta.providence.config.UncheckedProvidenceConfigException)

Aggregations

ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)13 File (java.io.File)8 TokenizerException (net.morimekta.providence.serializer.pretty.TokenizerException)4 Test (org.junit.Test)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 PMap (net.morimekta.providence.descriptor.PMap)3 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 PMessage (net.morimekta.providence.PMessage)2 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)2 ProvidenceTools (net.morimekta.providence.tools.common.ProvidenceTools)2 Database (net.morimekta.test.providence.config.Database)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 ArgumentException (net.morimekta.console.args.ArgumentException)1