Package com.typesafe.config

Examples of com.typesafe.config.ConfigValue


    public Long getBytes(String path) {
        Long size = null;
        try {
            size = getLong(path);
        } catch (ConfigException.WrongType e) {
            ConfigValue v = find(path, ConfigValueType.STRING);
            size = parseBytes((String) v.unwrapped(),
                    v.origin(), path);
        }
        return size;
    }
View Full Code Here


        return getDuration(path, TimeUnit.NANOSECONDS);
    }

    @Override
    public long getDuration(String path, TimeUnit unit) {
        ConfigValue v = find(path, ConfigValueType.STRING);
        long result = unit.convert(
                       parseDuration((String) v.unwrapped(), v.origin(), path),
                       TimeUnit.NANOSECONDS);
        return result;
    }
View Full Code Here

    @Override
    public AbstractConfigValue withFallback(ConfigMergeable mergeable) {
        if (ignoresFallbacks()) {
            return this;
        } else {
            ConfigValue other = ((MergeableValue) mergeable).toFallbackValue();

            if (other instanceof Unmergeable) {
                return mergedWithTheUnmergeable((Unmergeable) other);
            } else if (other instanceof AbstractConfigObject) {
                return mergedWithObject((AbstractConfigObject) other);
View Full Code Here

        Class<?> expectedType = field.getType();
        String fieldName = field.getName();
        if ((config == null) || !config.hasPath(fieldName)) {
            return null;
        } else if (field.isArray()) { // check CodableFieldInfo instead of expectedType
            ConfigValue configValue = config.root().get(fieldName);
            if ((configValue.valueType() != ConfigValueType.LIST) &&
                field.autoArrayEnabled()) {
                Object singleArrayValue = hydrateField(expectedType, fieldName, config);
                Object wrappingArray    = Array.newInstance(expectedType, 1);
                Array.set(wrappingArray, 0, singleArrayValue);
                return wrappingArray;
View Full Code Here

    private <T> T hydrateObject(@Nullable CodableClassInfo info,
                                PluginMap pluginMap,
                                @Nullable Class<T> type,
                                ConfigObject configObject) {
        String classField = pluginMap.classField();
        ConfigValue typeValue = configObject.get(classField);
        String stype = null;
        if ((typeValue != null) && (typeValue.valueType() == ConfigValueType.STRING)) {
            stype = (String) typeValue.unwrapped();
        }
        // if otherwise doomed to fail, try supporting "type-value : {...}"  syntax
        if ((stype == null) && (configObject.size() == 1) &&
            ((type == null) || Modifier.isAbstract(type.getModifiers()) ||
             Modifier.isInterface(type.getModifiers()))) {
View Full Code Here

      
       Map<String, String> keys = new HashMap<>();
       Map<String, ExtractConfiguration> sub = new HashMap<>();
      
       private static List<String> getStringList(Config c, String key) {
         ConfigValue v = c.getValue(key);
         if (v.valueType() == ConfigValueType.LIST) {
           List<String> l = c.getStringList(key);
           if (l.isEmpty()) {
             throw new RuntimeException("Cannot be empty: " + key);
           }
           return l;
View Full Code Here

            // different from hasPath in that this will accept ConfigValueType.NULL
            return config.root().get(fieldName);
        } else if (!config.hasPath(fieldName)) {
            return null;
        } else if (field.isArray()) { // check CodableFieldInfo instead of expectedType
            ConfigValue configValue = config.root().get(fieldName);
            if ((configValue.valueType() != ConfigValueType.LIST) &&
                field.autoArrayEnabled()) {
                Object singleArrayValue = hydrateFieldComponent(expectedType, fieldName, config);
                Object wrappingArray    = Array.newInstance(expectedType, 1);
                Array.set(wrappingArray, 0, singleArrayValue);
                return wrappingArray;
View Full Code Here

            String singleKeyName = configObject.keySet().iterator().next();
            try {
                Class<T> singleKeyType = (Class<T>) pluginMap.getClass(singleKeyName);
                CodableClassInfo singleKeyInfo = getOrCreateClassInfo(singleKeyType);
                ConfigObject aliasDefaults = pluginMap.aliasDefaults(singleKeyName);
                ConfigValue configValue = configObject.get(singleKeyName);
                if (configValue.valueType() != ConfigValueType.OBJECT) {
                    if (aliasDefaults.get("_primary") != null) {
                        // if value is not an object, try supporting _primary syntax to derive one
                        configValue = configValue.atPath((String) aliasDefaults.get("_primary").unwrapped()).root();
                    } else if (ValueCodable.class.isAssignableFrom(singleKeyType)) {
                        // see if the resolved type is innately okay with non-objects
                        try {
                            T objectShell = singleKeyType.newInstance();
                            Config fieldDefaults = singleKeyInfo.getFieldDefaults();
                            // do not merge objects between global defaults and user defaults (incl. alias defaults)
                            ConfigObject mergedDefaults = aliasDefaults;
                            for (Map.Entry<String, ConfigValue> pair : fieldDefaults.entrySet()) {
                                if (!mergedDefaults.containsKey(pair.getKey())) {
                                    mergedDefaults = mergedDefaults.withValue(pair.getKey(), pair.getValue());
                                }
                            }
                            ((ValueCodable) objectShell).fromConfigValue(configValue, mergedDefaults);
                            return objectShell;
                        } catch (InstantiationException | IllegalAccessException | RuntimeException ex) {
                            throw new ConfigException.BadValue(configValue.origin(), singleKeyType.getName(),
                                                               "exception during instantiation of a ValueCodable", ex);
                        }
                    } else {
                        throw new ConfigException.WrongType(configValue.origin(), singleKeyName,
                                                            "OBJECT", configValue.valueType().toString());
                    }
                }
                ConfigObject fieldValues = ((ConfigObject) configValue).withFallback(aliasDefaults);
                return createAndPopulate(singleKeyInfo, singleKeyType, fieldValues);
            } catch (ClassNotFoundException ignored) {
View Full Code Here

                                @Nullable Class<T> type,
                                ConfigObject configObject) {

        /* look for normal, explicit type syntax. ie. "{type: my-type, val: my-val}" */
        String classField = pluginMap.classField();
        ConfigValue typeValue = configObject.get(classField);
        if (typeValue != null) {
            if (typeValue.valueType() != ConfigValueType.STRING) {
                throw new ConfigException.WrongType(typeValue.origin(), classField,
                                                    "STRING", typeValue.valueType().toString());
            }
            String stype = (String) typeValue.unwrapped();
            try {
                Class<T> normalType = (Class<T>) pluginMap.getClass(stype);
                ConfigObject aliasDefaults = pluginMap.aliasDefaults(stype);
                ConfigObject fieldValues = configObject.withoutKey(classField).withFallback(aliasDefaults);
                CodableClassInfo normalInfo = getOrCreateClassInfo(normalType);
                return createAndPopulate(normalInfo, normalType, fieldValues);
            } catch (ClassNotFoundException e) {
                String helpMessage = Plugins.classNameSuggestions(pluginRegistry, pluginMap, stype);
                throw new ConfigException.UnresolvedSubstitution(configObject.origin(), helpMessage, e);
            }
        }

        /* if no chance of instantiating current type, try to get a new type from various special syntax/ settings */
        if ((type == null) || Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
            T maybeSingleKey = hydrateSingleKeyObject(pluginMap, configObject);
            if (maybeSingleKey != null) {
                return maybeSingleKey;
            }

            /* inlined types syntax ie "{ type-value: some-value, some-field: some-other-value, ...}".
             * Opt-in is on a per alias basis, and the target type must be unambiguous amongst aliases
             * that have opted in. The recognized alias label is then replaced with the _primary field. */
            String matched = null;
            for (String alias : pluginMap.inlinedAliases()) {
                if (configObject.get(alias) != null) {
                    if (matched != null) {
                        String message = String.format(
                                "no type specified, more than one key, and both %s and %s match for inlined types.",
                                matched, alias);
                        throw new ConfigException.Parse(configObject.origin(), message);
                    }
                    matched = alias;
                }
            }
            if (matched != null) {
                Class<T> inlinedType = (Class<T>) pluginMap.getClassIfConfigured(matched);
                assert inlinedType != null : "matched is always a key from the pluginMap's inlinedAliases set";
                CodableClassInfo inlinedInfo = getOrCreateClassInfo(inlinedType);
                ConfigObject aliasDefaults = pluginMap.aliasDefaults(matched);
                ConfigValue configValue = configObject.get(matched);
                String primaryField = (String) aliasDefaults.get("_primary").unwrapped();
                ConfigObject fieldValues =  configObject.withoutKey(matched).toConfig()
                                                        .withValue(primaryField, configValue).root()
                                                        .withFallback(aliasDefaults);
                return createAndPopulate(inlinedInfo, inlinedType, fieldValues);
View Full Code Here

                                      @Nonnull Object objectShell,
                                      @Nonnull Config config) throws IllegalAccessException {
        Config fieldDefaults = classInfo.getFieldDefaults();
        Collection<String> unusedKeys = new HashSet<>(config.root().keySet());

        ConfigValue fieldAliasesValue = config.root().get("_rename");
        ConfigObject fieldAliases;
        if ((fieldAliasesValue != null) && (fieldAliasesValue.valueType() != ConfigValueType.NULL)) {
            fieldAliases = (ConfigObject) fieldAliasesValue;
        } else {
            fieldAliases = ConfigFactory.empty().root();
        }

        ConfigValue primaryFieldNameValue = config.root().get("_primary");
        String primaryFieldName = null;
        boolean usedPrimaryField = false;
        if ((primaryFieldNameValue != null) && (primaryFieldNameValue.valueType() != ConfigValueType.NULL)) {
            primaryFieldName = (String) primaryFieldNameValue.unwrapped();
        }

        for (CodableFieldInfo field : classInfo.values()) {
            if (field.isWriteOnly()) {
                continue;
            }
            String fieldName = field.getName();
            if (fieldAliases.containsKey(fieldName)) {
                String aliasName = (String) fieldAliases.get(fieldName).unwrapped();
                unusedKeys.remove(aliasName);
                if (config.root().containsKey(fieldName)
                    && (config.root().get(fieldName).valueType() == ConfigValueType.NULL)) {
                    // complain about values for renamed fields unless null or used elsewhere
                    unusedKeys.remove(fieldName);
                }
            } else {
                unusedKeys.remove(fieldName);
            }
        }
        if (!unusedKeys.isEmpty()) {
            for (Iterator<String> unusedKeyIterator = unusedKeys.iterator();
                 unusedKeyIterator.hasNext(); ) {
                String unusedKey = unusedKeyIterator.next();
                if (unusedKey.charAt(0) == '_') {
                    unusedKeyIterator.remove();
                }
            }
        }
        if (unusedKeys.size() > 1) {
            throw new ConfigException.BadPath(config.origin(), "unrecognized key(s) " + unusedKeys.toString());
        }

        for (CodableFieldInfo field : classInfo.values()) {
            if (field.isWriteOnly()) {
                continue;
            }
            String fieldName = field.getName();
            String resolvedName;
            Config resolvedConfig;
            if (fieldAliases.containsKey(fieldName)) {
                String aliasName = (String) fieldAliases.get(fieldName).unwrapped();
                if (config.hasPath(aliasName)) {
                    ConfigValue aliasValue = config.getValue(aliasName); // alias targets are paths
                    resolvedConfig = config.root().withValue(fieldName, aliasValue).toConfig();
                } else {
                    resolvedConfig = config.root().withoutKey(fieldName).toConfig();
                }
                resolvedName = aliasName;
View Full Code Here

TOP

Related Classes of com.typesafe.config.ConfigValue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.