Search in sources :

Example 1 with ConfigObject

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

the class SimpleIncluder method fromBasename.

// this function is a little tricky because there are three places we're
// trying to use it; for 'include "basename"' in a .conf file, for
// loading app.{conf,json,properties} from classpath, and for
// loading app.{conf,json,properties} from the filesystem.
static ConfigObject fromBasename(NameSource source, String name, ConfigParseOptions options) {
    ConfigObject obj;
    if (name.endsWith(".conf") || name.endsWith(".json") || name.endsWith(".properties")) {
        ConfigParseable p = source.nameToParseable(name, options);
        obj = p.parse(p.options().setAllowMissing(options.getAllowMissing()));
    } else {
        ConfigParseable confHandle = source.nameToParseable(name + ".conf", options);
        ConfigParseable jsonHandle = source.nameToParseable(name + ".json", options);
        ConfigParseable propsHandle = source.nameToParseable(name + ".properties", options);
        boolean gotSomething = false;
        List<ConfigException.IO> fails = new ArrayList<ConfigException.IO>();
        ConfigSyntax syntax = options.getSyntax();
        obj = SimpleConfigObject.empty(SimpleConfigOrigin.newSimple(name));
        if (syntax == null || syntax == ConfigSyntax.CONF) {
            try {
                obj = confHandle.parse(confHandle.options().setAllowMissing(false).setSyntax(ConfigSyntax.CONF));
                gotSomething = true;
            } catch (ConfigException.IO e) {
                fails.add(e);
            }
        }
        if (syntax == null || syntax == ConfigSyntax.JSON) {
            try {
                ConfigObject parsed = jsonHandle.parse(jsonHandle.options().setAllowMissing(false).setSyntax(ConfigSyntax.JSON));
                obj = obj.withFallback(parsed);
                gotSomething = true;
            } catch (ConfigException.IO e) {
                fails.add(e);
            }
        }
        if (syntax == null || syntax == ConfigSyntax.PROPERTIES) {
            try {
                ConfigObject parsed = propsHandle.parse(propsHandle.options().setAllowMissing(false).setSyntax(ConfigSyntax.PROPERTIES));
                obj = obj.withFallback(parsed);
                gotSomething = true;
            } catch (ConfigException.IO e) {
                fails.add(e);
            }
        }
        if (!options.getAllowMissing() && !gotSomething) {
            if (ConfigImpl.traceLoadsEnabled()) {
                // the individual exceptions should have been logged already
                // with tracing enabled
                ConfigImpl.trace("Did not find '" + name + "' with any extension (.conf, .json, .properties); " + "exceptions should have been logged above.");
            }
            if (fails.isEmpty()) {
                // this should not happen
                throw new ConfigException.BugOrBroken("should not be reached: nothing found but no exceptions thrown");
            } else {
                StringBuilder sb = new StringBuilder();
                for (Throwable t : fails) {
                    sb.append(t.getMessage());
                    sb.append(", ");
                }
                sb.setLength(sb.length() - 2);
                throw new ConfigException.IO(SimpleConfigOrigin.newSimple(name), sb.toString(), fails.get(0));
            }
        } else if (!gotSomething) {
            if (ConfigImpl.traceLoadsEnabled()) {
                ConfigImpl.trace("Did not find '" + name + "' with any extension (.conf, .json, .properties); but '" + name + "' is allowed to be missing. Exceptions from load attempts should have been logged above.");
            }
        }
    }
    return obj;
}
Also used : ConfigParseable(com.typesafe.config.ConfigParseable) ConfigSyntax(com.typesafe.config.ConfigSyntax) ArrayList(java.util.ArrayList) ConfigException(com.typesafe.config.ConfigException) ConfigObject(com.typesafe.config.ConfigObject)

Example 2 with ConfigObject

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

the class SerializedConfigValue method writeValueData.

private static void writeValueData(DataOutput out, ConfigValue value) throws IOException {
    SerializedValueType st = SerializedValueType.forValue(value);
    out.writeByte(st.ordinal());
    switch(st) {
        case BOOLEAN:
            out.writeBoolean(((ConfigBoolean) value).unwrapped());
            break;
        case NULL:
            break;
        case INT:
            // saving numbers as both string and binary is redundant but easy
            out.writeInt(((ConfigInt) value).unwrapped());
            out.writeUTF(((ConfigNumber) value).transformToString());
            break;
        case LONG:
            out.writeLong(((ConfigLong) value).unwrapped());
            out.writeUTF(((ConfigNumber) value).transformToString());
            break;
        case DOUBLE:
            out.writeDouble(((ConfigDouble) value).unwrapped());
            out.writeUTF(((ConfigNumber) value).transformToString());
            break;
        case STRING:
            out.writeUTF(((ConfigString) value).unwrapped());
            break;
        case LIST:
            ConfigList list = (ConfigList) value;
            out.writeInt(list.size());
            for (ConfigValue v : list) {
                writeValue(out, v, (SimpleConfigOrigin) list.origin());
            }
            break;
        case OBJECT:
            ConfigObject obj = (ConfigObject) value;
            out.writeInt(obj.size());
            for (Map.Entry<String, ConfigValue> e : obj.entrySet()) {
                out.writeUTF(e.getKey());
                writeValue(out, e.getValue(), (SimpleConfigOrigin) obj.origin());
            }
            break;
    }
}
Also used : ConfigValue(com.typesafe.config.ConfigValue) ConfigObject(com.typesafe.config.ConfigObject) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) ConfigList(com.typesafe.config.ConfigList)

Example 3 with ConfigObject

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

the class ConfigImpl method fromAnyRef.

static AbstractConfigValue fromAnyRef(Object object, ConfigOrigin origin, FromMapMode mapMode) {
    if (origin == null)
        throw new ConfigException.BugOrBroken("origin not supposed to be null");
    if (object == null) {
        if (origin != defaultValueOrigin)
            return new ConfigNull(origin);
        else
            return defaultNullValue;
    } else if (object instanceof AbstractConfigValue) {
        return (AbstractConfigValue) object;
    } else if (object instanceof Boolean) {
        if (origin != defaultValueOrigin) {
            return new ConfigBoolean(origin, (Boolean) object);
        } else if ((Boolean) object) {
            return defaultTrueValue;
        } else {
            return defaultFalseValue;
        }
    } else if (object instanceof String) {
        return new ConfigString.Quoted(origin, (String) object);
    } else if (object instanceof Number) {
        // Double, Integer, or Long.
        if (object instanceof Double) {
            return new ConfigDouble(origin, (Double) object, null);
        } else if (object instanceof Integer) {
            return new ConfigInt(origin, (Integer) object, null);
        } else if (object instanceof Long) {
            return new ConfigLong(origin, (Long) object, null);
        } else {
            return ConfigNumber.newNumber(origin, ((Number) object).doubleValue(), null);
        }
    } else if (object instanceof Duration) {
        return new ConfigLong(origin, ((Duration) object).toMillis(), null);
    } else if (object instanceof Map) {
        if (((Map<?, ?>) object).isEmpty())
            return emptyObject(origin);
        if (mapMode == FromMapMode.KEYS_ARE_KEYS) {
            Map<String, AbstractConfigValue> values = new HashMap<String, AbstractConfigValue>();
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
                Object key = entry.getKey();
                if (!(key instanceof String))
                    throw new ConfigException.BugOrBroken("bug in method caller: not valid to create ConfigObject from map with non-String key: " + key);
                AbstractConfigValue value = fromAnyRef(entry.getValue(), origin, mapMode);
                values.put((String) key, value);
            }
            return new SimpleConfigObject(origin, values);
        } else {
            return PropertiesParser.fromPathMap(origin, (Map<?, ?>) object);
        }
    } else if (object instanceof Iterable) {
        Iterator<?> i = ((Iterable<?>) object).iterator();
        if (!i.hasNext())
            return emptyList(origin);
        List<AbstractConfigValue> values = new ArrayList<AbstractConfigValue>();
        while (i.hasNext()) {
            AbstractConfigValue v = fromAnyRef(i.next(), origin, mapMode);
            values.add(v);
        }
        return new SimpleConfigList(origin, values);
    } else if (object instanceof ConfigMemorySize) {
        return new ConfigLong(origin, ((ConfigMemorySize) object).toBytes(), null);
    } else {
        throw new ConfigException.BugOrBroken("bug in method caller: not valid to create ConfigValue from: " + object);
    }
}
Also used : HashMap(java.util.HashMap) ConfigException(com.typesafe.config.ConfigException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Duration(java.time.Duration) ConfigMemorySize(com.typesafe.config.ConfigMemorySize) ConfigObject(com.typesafe.config.ConfigObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ConfigObject

use of com.typesafe.config.ConfigObject in project aerosolve by airbnb.

the class CustomLinearLogQuantizeTransform method parseTokensOutOfLimitBucketPairs.

private static TreeMap<Double, Double> parseTokensOutOfLimitBucketPairs(List<? extends ConfigObject> pairs) {
    TreeMap<Double, Double> parsedTokensMap = new TreeMap<>();
    for (ConfigObject configObject : pairs) {
        List<Entry<String, ConfigValue>> entries = new ArrayList<>(configObject.entrySet());
        parsedTokensMap.put(Double.parseDouble(entries.get(0).getKey()), Double.parseDouble(entries.get(0).getValue().unwrapped().toString()));
    }
    return parsedTokensMap;
}
Also used : Entry(java.util.Map.Entry) ConfigObject(com.typesafe.config.ConfigObject)

Example 5 with ConfigObject

use of com.typesafe.config.ConfigObject in project aerosolve by airbnb.

the class ReplaceAllStringsTransform method processString.

@Override
public String processString(String rawString) {
    if (rawString == null) {
        return null;
    }
    for (ConfigObject replacementCO : replacements) {
        Map<String, Object> replacementMap = replacementCO.unwrapped();
        for (Map.Entry<String, Object> replacementEntry : replacementMap.entrySet()) {
            String regex = replacementEntry.getKey();
            String replacement = (String) replacementEntry.getValue();
            rawString = rawString.replaceAll(regex, replacement);
        }
    }
    return rawString;
}
Also used : ConfigObject(com.typesafe.config.ConfigObject) ConfigObject(com.typesafe.config.ConfigObject) Map(java.util.Map)

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