Search in sources :

Example 1 with MapPropertyResolver

use of io.micronaut.core.value.MapPropertyResolver in project micronaut-graphql by micronaut-projects.

the class GraphiQLController method replaceParameters.

private String replaceParameters(final String str, final Map<String, String> parameters) {
    Map<String, Object> map = new HashMap<>();
    map.putAll(parameters);
    PropertyResolver propertyResolver = new MapPropertyResolver(map);
    PropertyPlaceholderResolver propertyPlaceholderResolver = new DefaultPropertyPlaceholderResolver(propertyResolver, conversionService);
    return propertyPlaceholderResolver.resolvePlaceholders(str).get();
}
Also used : DefaultPropertyPlaceholderResolver(io.micronaut.context.env.DefaultPropertyPlaceholderResolver) HashMap(java.util.HashMap) MapPropertyResolver(io.micronaut.core.value.MapPropertyResolver) DefaultPropertyPlaceholderResolver(io.micronaut.context.env.DefaultPropertyPlaceholderResolver) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) PropertyResolver(io.micronaut.core.value.PropertyResolver) MapPropertyResolver(io.micronaut.core.value.MapPropertyResolver)

Example 2 with MapPropertyResolver

use of io.micronaut.core.value.MapPropertyResolver in project micronaut-core by micronaut-projects.

the class PropertySourcePropertyResolver method getProperty.

@Override
public <T> Optional<T> getProperty(@NonNull String name, @NonNull ArgumentConversionContext<T> conversionContext) {
    if (StringUtils.isEmpty(name)) {
        return Optional.empty();
    } else {
        Objects.requireNonNull(conversionContext, "Conversion context should not be null");
        Class<T> requiredType = conversionContext.getArgument().getType();
        boolean cacheableType = ClassUtils.isJavaLangType(requiredType);
        Object cached = cacheableType ? resolvedValueCache.get(cacheKey(name, requiredType)) : null;
        if (cached != null) {
            return cached == NO_VALUE ? Optional.empty() : Optional.of((T) cached);
        } else {
            Map<String, Object> entries = resolveEntriesForKey(name, false, PropertyCatalog.GENERATED);
            if (entries == null) {
                entries = resolveEntriesForKey(name, false, PropertyCatalog.RAW);
            }
            if (entries != null) {
                Object value = entries.get(name);
                if (value == null) {
                    value = entries.get(normalizeName(name));
                    if (value == null && name.indexOf('[') == -1) {
                        // last chance lookup the raw value
                        Map<String, Object> rawEntries = resolveEntriesForKey(name, false, PropertyCatalog.RAW);
                        value = rawEntries != null ? rawEntries.get(name) : null;
                        if (value != null) {
                            entries = rawEntries;
                        }
                    }
                }
                if (value == null) {
                    int i = name.indexOf('[');
                    if (i > -1 && name.endsWith("]")) {
                        String newKey = name.substring(0, i);
                        value = entries.get(newKey);
                        String index = name.substring(i + 1, name.length() - 1);
                        if (value != null) {
                            if (StringUtils.isNotEmpty(index)) {
                                if (value instanceof List) {
                                    try {
                                        value = ((List) value).get(Integer.valueOf(index));
                                    } catch (NumberFormatException e) {
                                    // ignore
                                    }
                                } else if (value instanceof Map) {
                                    try {
                                        value = ((Map) value).get(index);
                                    } catch (NumberFormatException e) {
                                    // ignore
                                    }
                                }
                            }
                        } else {
                            if (StringUtils.isNotEmpty(index)) {
                                String subKey = newKey + '.' + index;
                                value = entries.get(subKey);
                            }
                        }
                    }
                }
                if (value != null) {
                    Optional<T> converted;
                    value = resolvePlaceHoldersIfNecessary(value);
                    if (requiredType.isInstance(value) && !CollectionUtils.isIterableOrMap(requiredType)) {
                        converted = (Optional<T>) Optional.of(value);
                    } else {
                        converted = conversionService.convert(value, conversionContext);
                    }
                    if (LOG.isTraceEnabled()) {
                        if (converted.isPresent()) {
                            LOG.trace("Resolved value [{}] for property: {}", converted.get(), name);
                        } else {
                            LOG.trace("Resolved value [{}] cannot be converted to type [{}] for property: {}", value, conversionContext.getArgument(), name);
                        }
                    }
                    if (cacheableType) {
                        resolvedValueCache.put(cacheKey(name, requiredType), converted.orElse((T) NO_VALUE));
                    }
                    return converted;
                } else if (cacheableType) {
                    resolvedValueCache.put(cacheKey(name, requiredType), NO_VALUE);
                    return Optional.empty();
                } else if (Properties.class.isAssignableFrom(requiredType)) {
                    Properties properties = resolveSubProperties(name, entries, conversionContext);
                    return Optional.of((T) properties);
                } else if (Map.class.isAssignableFrom(requiredType)) {
                    Map<String, Object> subMap = resolveSubMap(name, entries, conversionContext);
                    if (!subMap.isEmpty()) {
                        return conversionService.convert(subMap, requiredType, conversionContext);
                    } else {
                        return (Optional<T>) Optional.of(subMap);
                    }
                } else if (PropertyResolver.class.isAssignableFrom(requiredType)) {
                    Map<String, Object> subMap = resolveSubMap(name, entries, conversionContext);
                    return Optional.of((T) new MapPropertyResolver(subMap, conversionService));
                }
            }
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("No value found for property: {}", name);
    }
    Class<T> requiredType = conversionContext.getArgument().getType();
    if (Properties.class.isAssignableFrom(requiredType)) {
        return Optional.of((T) new Properties());
    } else if (Map.class.isAssignableFrom(requiredType)) {
        return Optional.of((T) Collections.emptyMap());
    }
    return Optional.empty();
}
Also used : EnvironmentProperties(io.micronaut.core.util.EnvironmentProperties) Properties(java.util.Properties) MapPropertyResolver(io.micronaut.core.value.MapPropertyResolver) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

MapPropertyResolver (io.micronaut.core.value.MapPropertyResolver)2 HashMap (java.util.HashMap)2 DefaultPropertyPlaceholderResolver (io.micronaut.context.env.DefaultPropertyPlaceholderResolver)1 PropertyPlaceholderResolver (io.micronaut.context.env.PropertyPlaceholderResolver)1 EnvironmentProperties (io.micronaut.core.util.EnvironmentProperties)1 PropertyResolver (io.micronaut.core.value.PropertyResolver)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1