Search in sources :

Example 6 with ConfigObject

use of com.typesafe.config.ConfigObject in project config by typesafehub.

the class ConfigConcatenation method join.

/**
     * Add left and right, or their merger, to builder.
     */
private static void join(ArrayList<AbstractConfigValue> builder, AbstractConfigValue origRight) {
    AbstractConfigValue left = builder.get(builder.size() - 1);
    AbstractConfigValue right = origRight;
    // (this will be an object with numeric keys, like foo.0, foo.1)
    if (left instanceof ConfigObject && right instanceof SimpleConfigList) {
        left = DefaultTransformer.transform(left, ConfigValueType.LIST);
    } else if (left instanceof SimpleConfigList && right instanceof ConfigObject) {
        right = DefaultTransformer.transform(right, ConfigValueType.LIST);
    }
    // Since this depends on the type of two instances, I couldn't think
    // of much alternative to an instanceof chain. Visitors are sometimes
    // used for multiple dispatch but seems like overkill.
    AbstractConfigValue joined = null;
    if (left instanceof ConfigObject && right instanceof ConfigObject) {
        joined = right.withFallback(left);
    } else if (left instanceof SimpleConfigList && right instanceof SimpleConfigList) {
        joined = ((SimpleConfigList) left).concatenate((SimpleConfigList) right);
    } else if ((left instanceof SimpleConfigList || left instanceof ConfigObject) && isIgnoredWhitespace(right)) {
        joined = left;
    // it should be impossible that left is whitespace and right is a list or object
    } else if (left instanceof ConfigConcatenation || right instanceof ConfigConcatenation) {
        throw new ConfigException.BugOrBroken("unflattened ConfigConcatenation");
    } else if (left instanceof Unmergeable || right instanceof Unmergeable) {
    // leave joined=null, cannot join
    } else {
        // handle primitive type or primitive type mixed with object or list
        String s1 = left.transformToString();
        String s2 = right.transformToString();
        if (s1 == null || s2 == null) {
            throw new ConfigException.WrongType(left.origin(), "Cannot concatenate object or list with a non-object-or-list, " + left + " and " + right + " are not compatible");
        } else {
            ConfigOrigin joinedOrigin = SimpleConfigOrigin.mergeOrigins(left.origin(), right.origin());
            joined = new ConfigString.Quoted(joinedOrigin, s1 + s2);
        }
    }
    if (joined == null) {
        builder.add(right);
    } else {
        builder.remove(builder.size() - 1);
        builder.add(joined);
    }
}
Also used : ConfigOrigin(com.typesafe.config.ConfigOrigin) ConfigException(com.typesafe.config.ConfigException) ConfigObject(com.typesafe.config.ConfigObject)

Example 7 with ConfigObject

use of com.typesafe.config.ConfigObject in project config by typesafehub.

the class SimpleConfig method getConfigList.

@Override
public List<? extends Config> getConfigList(String path) {
    List<ConfigObject> objects = getObjectList(path);
    List<Config> l = new ArrayList<Config>();
    for (ConfigObject o : objects) {
        l.add(o.toConfig());
    }
    return l;
}
Also used : Config(com.typesafe.config.Config) ArrayList(java.util.ArrayList) ConfigObject(com.typesafe.config.ConfigObject)

Aggregations

ConfigObject (com.typesafe.config.ConfigObject)7 ConfigException (com.typesafe.config.ConfigException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 HashMap (java.util.HashMap)2 Config (com.typesafe.config.Config)1 ConfigList (com.typesafe.config.ConfigList)1 ConfigMemorySize (com.typesafe.config.ConfigMemorySize)1 ConfigOrigin (com.typesafe.config.ConfigOrigin)1 ConfigParseable (com.typesafe.config.ConfigParseable)1 ConfigSyntax (com.typesafe.config.ConfigSyntax)1 ConfigValue (com.typesafe.config.ConfigValue)1 Duration (java.time.Duration)1 EnumMap (java.util.EnumMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Entry (java.util.Map.Entry)1