Package com.typesafe.config

Examples of com.typesafe.config.ConfigValue


    private static ConfigObject expandSugarSkipResolve(Class<?> type, ConfigObject root, CodecConfig codec) {
        CodableClassInfo resolvedTypeInfo = codec.getOrCreateClassInfo(type);
        ConfigObject fieldDefaults = resolvedTypeInfo.getFieldDefaults().root();
        for (CodableFieldInfo fieldInfo : resolvedTypeInfo.values()) {
            String fieldName = fieldInfo.getName();
            ConfigValue fieldValue = root.get(fieldName);
            if ((fieldValue == null) && (fieldDefaults.get(fieldName) != null)) {
                ConfigValue fieldDefault = fieldDefaults.get(fieldName);
                fieldValue = ConfigValueFactory.fromAnyRef(
                        fieldDefault.unwrapped(), "global default : " + fieldDefault.origin().description());
                root = root.withValue(fieldName, fieldValue);
            }
            if (fieldValue == null) {
                continue;
            }
            if ((fieldInfo.isArray() || fieldInfo.isCollection()) &&
                (fieldValue.valueType() != ConfigValueType.LIST) && fieldInfo.autoArrayEnabled()) {
                fieldValue = ConfigValueFactory.fromIterable(
                        Collections.singletonList(fieldValue.unwrapped()), "auto collection of " +
                                                                           fieldValue.origin().description());
                root = root.withValue(fieldName, fieldValue);
            }
            if (!isCodableType(fieldInfo)) {
                continue;
            }
            if (fieldInfo.isArray() || fieldInfo.isCollection()) {
                if (fieldValue.valueType() != ConfigValueType.LIST) {
                    throw new ConfigException.WrongType(fieldValue.origin(), fieldName,
                                                        ConfigValueType.LIST.name(), fieldValue.valueType().name());
                }
                Class<?> elementType = elementType(fieldInfo);
                boolean nested = fieldInfo.isCollectionArray();
                fieldValue = expandSugarArray(fieldValue, elementType, codec, nested);
            } else if (fieldInfo.isMap()) {
                if (fieldValue.valueType() != ConfigValueType.OBJECT) {
                    throw new ConfigException.WrongType(fieldValue.origin(), fieldName,
                                                        ConfigValueType.OBJECT.name(), fieldValue.valueType().name());
                }
                Class<?> elementType = elementType(fieldInfo);
                boolean nested = fieldInfo.isMapValueArray();
                ConfigObject fieldMap = (ConfigObject) fieldValue;
                Map<String, Object> newMap = new HashMap<>(fieldMap.size());
                for (Map.Entry<String, ConfigValue> mapEntry : fieldMap.entrySet()) {
                    ConfigValue mapValue = mapEntry.getValue();
                    String mapKey = mapEntry.getKey();
                    ConfigValue resolvedMapObject;
                    if (nested) {
                        resolvedMapObject = expandSugarArray(mapValue, elementType, codec, false);
                    } else {
                        resolvedMapObject = expandSugar(elementType, mapValue, codec);
                    }
                    newMap.put(mapKey, resolvedMapObject.unwrapped());
                }
                fieldValue = ConfigValueFactory.fromMap(newMap, fieldMap.origin().description());
            } else {
                fieldValue = expandSugar(fieldInfo.getType(), fieldValue, codec);
            }
View Full Code Here


                                                 CodecConfig codec,
                                                 boolean nested) {
        ConfigList fieldList = (ConfigList) fieldValue;
        List<Object> newList = new ArrayList<>(fieldList.size());
        for (ConfigValue listEntry : fieldList) {
            ConfigValue listObject;
            if (nested) {
                listObject = expandSugarArray(listEntry, elementType, codec, false);
            } else {
                listObject = expandSugar(elementType, listEntry, codec);
            }
            newList.add(listObject.unwrapped());
        }
        return ConfigValueFactory.fromIterable(newList, fieldList.origin().description());
    }
View Full Code Here

            throw new ConfigException.WrongType(configValue.origin(),
                                                "invalid config type of " + configValue.valueType() +
                                                " for " + pluginMap);
        }
        ConfigObject root = (ConfigObject) configValue;
        ConfigValue classValue = root.get(classField);
        // normal, explicit typing
        if ((classValue != null) && (classValue.valueType() == ConfigValueType.STRING)) {
            String classValueString = (String) classValue.unwrapped();
            ConfigObject aliasDefaults = pluginMap.aliasDefaults(classValueString);
            return root.withFallback(aliasDefaults);
        }

        if ((type == null) || Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
            // single key as type
            if (root.size() == 1) {
                String onlyKey = root.keySet().iterator().next();
                try {
                    pluginMap.getClass(onlyKey); // make sure key is a valid type
                    ConfigValue onlyKeyValue = root.values().iterator().next();
                    ConfigObject aliasDefaults = pluginMap.aliasDefaults(onlyKey);
                    if (onlyKeyValue.valueType() != ConfigValueType.OBJECT) {
                        if (aliasDefaults.get("_primary") != null) {
                            onlyKeyValue = onlyKeyValue.atPath((String) aliasDefaults.get("_primary").unwrapped()).root();
                        } else {
                            throw new ConfigException.WrongType(onlyKeyValue.origin(), onlyKey,
                                                                "OBJECT", onlyKeyValue.valueType().toString());
                        }
                    }
                    ConfigObject fieldValues = (ConfigObject) onlyKeyValue;
                    return fieldValues.withValue(classField, ConfigValueFactory.fromAnyRef(
                            onlyKey, "single key to type from " + root.origin().description()))
                                      .withFallback(aliasDefaults);
                } catch (ClassNotFoundException ignored) {
                }
            }

            // inlined type
            String matched = null;
            for (String alias : pluginMap.inlinedAliases()) {
                if (root.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(root.origin(), message);
                    }
                    matched = alias;
                }
            }
            if (matched != null) {
                ConfigObject aliasDefaults = pluginMap.aliasDefaults(matched);
                ConfigValue inlinedValue = root.get(matched);
                String primaryField = (String) aliasDefaults.get("_primary").unwrapped();
                ConfigObject fieldValues =  root.toConfig().withValue(primaryField, inlinedValue).root()
                                                        .withoutKey(matched)
                                                        .withFallback(aliasDefaults);
                return fieldValues.withValue(classField, ConfigValueFactory.fromAnyRef(
                        matched, "inlined key to type from " + root.origin().description()));
            }

            // default type
            ConfigValue defaultObject = pluginMap.config().root().get("_default");
            if (defaultObject != null) {
                String defaultName = pluginMap.getLastAlias("_default");
                ConfigObject aliasDefaults = pluginMap.aliasDefaults("_default");
                return root.withValue(classField, ConfigValueFactory.fromAnyRef(
                        defaultName, pluginMap.category() + " default type : "
                                     + defaultObject.origin().description()))
                           .withFallback(aliasDefaults);
            }
        }
        return root;
    }
View Full Code Here

        return _nodeCursor;
    }

    @Override
    public JsonLocation getTokenLocation() {
        ConfigValue current = currentConfig();
        if (current == null) {
            return JsonLocation.NA;
        }
        ConfigOrigin nodeOrigin = current.origin();
        return new JsonLocation(current, -1, nodeOrigin.lineNumber(), -1);
    }
View Full Code Here

        }
        return _nodeCursor.currentNode();
    }

    protected JsonNode currentNumericNode() throws JsonParseException {
        ConfigValue configValue = currentNode();
        if ((configValue == null) || (configValue.valueType() != ConfigValueType.NUMBER)) {
            JsonToken t = (configValue == null) ? null : ConfigNodeCursor.forConfigValue(configValue);
            throw _constructError("Current token ("+t+") not numeric, can not use numeric value accessors");
        }
        Number value = (Number) configValue.unwrapped();
        if (value instanceof Double) {
            return JsonNodeFactory.instance.numberNode((Double) value);
        }
        if (value instanceof Long) {
            return JsonNodeFactory.instance.numberNode((Long) value);
View Full Code Here

    /**
     * Method called to create a new context for iterating all
     * contents of the current structured value (JSON array or object)
     */
    public final ConfigNodeCursor iterateChildren() {
        ConfigValue n = currentNode();
        if (n == null) throw new IllegalStateException("No current node");
        if (n.valueType() == LIST) { // false since we have already returned START_ARRAY
            return new Array((ConfigList) n, this);
        }
        if (n.valueType() == OBJECT) {
            return new Object((ConfigObject) n, this);
        }
        throw new IllegalStateException("Current node of type " + n.getClass().getName());
    }
View Full Code Here

        public ConfigValue currentNode() { return _currentNode; }

        @Override
        public boolean currentHasChildren() {
            // note: ONLY to be called for container nodes
            ConfigValue currentValue = currentNode();
            if (currentValue.valueType() == ConfigValueType.LIST) {
                return !((ConfigList) currentValue).isEmpty();
            } else {
                return !((ConfigObject) currentValue).isEmpty();
            }
        }
View Full Code Here

        }

        @Override
        public boolean currentHasChildren() {
            // note: ONLY to be called for container nodes
            ConfigValue currentValue = currentNode();
            if (currentValue.valueType() == ConfigValueType.LIST) {
                return !((ConfigList) currentValue).isEmpty();
            } else {
                return !((ConfigObject) currentValue).isEmpty();
            }
        }
View Full Code Here

                                                 CodecConfig codec,
                                                 boolean nested) {
        ConfigList fieldList = (ConfigList) fieldValue;
        List<Object> newList = new ArrayList<>(fieldList.size());
        for (ConfigValue listEntry : fieldList) {
            ConfigValue listObject;
            if (nested) {
                listObject = expandSugarArray(listEntry, elementType, codec, false);
            } else {
                listObject = expandSugar(elementType, listEntry, codec);
            }
            newList.add(listObject.unwrapped());
        }
        return ConfigValueFactory.fromIterable(newList, fieldList.origin().description());
    }
View Full Code Here

    public static JsonMappingException maybeImproveLocation(JsonLocation wrapLoc, JsonMappingException cause) {
        JsonLocation exLoc = cause.getLocation();
        if (isRealLocation(wrapLoc) && !isRealLocation(exLoc)) {
            if (wrapLoc.getSourceRef() instanceof ConfigValue) {
                ConfigValue locRef = (ConfigValue) wrapLoc.getSourceRef();
                List<JsonMappingException.Reference> paths = cause.getPath();
                for (JsonMappingException.Reference path : paths) {
                    if (locRef instanceof ConfigObject) {
                        String fieldName = path.getFieldName();
                        ConfigObject locRefObject = (ConfigObject) locRef;
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.