Search in sources :

Example 1 with StringConvention

use of io.micronaut.core.naming.conventions.StringConvention in project micronaut-core by micronaut-projects.

the class PropertySourcePropertyResolver method resolveSubProperties.

/**
 * @param name              The property name
 * @param entries           The entries
 * @param conversionContext The conversion context
 * @return The subproperties
 */
protected Properties resolveSubProperties(String name, Map<String, Object> entries, ArgumentConversionContext<?> conversionContext) {
    // special handling for maps for resolving sub keys
    Properties properties = new Properties();
    AnnotationMetadata annotationMetadata = conversionContext.getAnnotationMetadata();
    StringConvention keyConvention = annotationMetadata.enumValue(MapFormat.class, "keyFormat", StringConvention.class).orElse(null);
    if (keyConvention == StringConvention.RAW) {
        entries = resolveEntriesForKey(name, false, PropertyCatalog.RAW);
    }
    String prefix = name + '.';
    entries.entrySet().stream().filter(map -> map.getKey().startsWith(prefix)).forEach(entry -> {
        Object value = entry.getValue();
        if (value != null) {
            String key = entry.getKey().substring(prefix.length());
            key = keyConvention != null ? keyConvention.format(key) : key;
            properties.put(key, resolvePlaceHoldersIfNecessary(value.toString()));
        }
    });
    return properties;
}
Also used : Arrays(java.util.Arrays) ListIterator(java.util.ListIterator) StringConvention(io.micronaut.core.naming.conventions.StringConvention) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) HashMap(java.util.HashMap) Random(java.util.Random) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PropertyResolver(io.micronaut.core.value.PropertyResolver) Matcher(java.util.regex.Matcher) EnvironmentProperties(io.micronaut.core.util.EnvironmentProperties) Nullable(io.micronaut.core.annotation.Nullable) MapPropertyResolver(io.micronaut.core.value.MapPropertyResolver) Map(java.util.Map) Argument(io.micronaut.core.type.Argument) NameUtils(io.micronaut.core.naming.NameUtils) ConversionService(io.micronaut.core.convert.ConversionService) StaticOptimizations(io.micronaut.core.optim.StaticOptimizations) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) ClassUtils(io.micronaut.core.reflect.ClassUtils) Logger(org.slf4j.Logger) Properties(java.util.Properties) ValueException(io.micronaut.core.value.ValueException) SocketUtils(io.micronaut.core.io.socket.SocketUtils) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MapFormat(io.micronaut.core.convert.format.MapFormat) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Consumer(java.util.function.Consumer) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) ConversionContext(io.micronaut.core.convert.ConversionContext) CollectionUtils(io.micronaut.core.util.CollectionUtils) Optional(java.util.Optional) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) MapFormat(io.micronaut.core.convert.format.MapFormat) StringConvention(io.micronaut.core.naming.conventions.StringConvention) EnvironmentProperties(io.micronaut.core.util.EnvironmentProperties) Properties(java.util.Properties) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Example 2 with StringConvention

use of io.micronaut.core.naming.conventions.StringConvention in project micronaut-security by micronaut-projects.

the class InterceptUrlMapConverter method transform.

private Map<String, Object> transform(Map<String, Object> map) {
    Map<String, Object> transformed = new HashMap<>();
    StringConvention convention = StringConvention.HYPHENATED;
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        transformed.put(convention.format(entry.getKey()), entry.getValue());
    }
    return transformed;
}
Also used : HashMap(java.util.HashMap) StringConvention(io.micronaut.core.naming.conventions.StringConvention) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with StringConvention

use of io.micronaut.core.naming.conventions.StringConvention in project micronaut-core by micronaut-projects.

the class PropertySourcePropertyResolver method resolveSubMap.

/**
 * @param name              The property name
 * @param entries           The entries
 * @param conversionContext The conversion context
 * @return The submap
 */
protected Map<String, Object> resolveSubMap(String name, Map<String, Object> entries, ArgumentConversionContext<?> conversionContext) {
    // special handling for maps for resolving sub keys
    AnnotationMetadata annotationMetadata = conversionContext.getAnnotationMetadata();
    StringConvention keyConvention = annotationMetadata.enumValue(MapFormat.class, "keyFormat", StringConvention.class).orElse(null);
    if (keyConvention == StringConvention.RAW) {
        entries = resolveEntriesForKey(name, false, PropertyCatalog.RAW);
    }
    MapFormat.MapTransformation transformation = annotationMetadata.enumValue(MapFormat.class, "transformation", MapFormat.MapTransformation.class).orElse(MapFormat.MapTransformation.NESTED);
    return resolveSubMap(name, entries, conversionContext, keyConvention, transformation);
}
Also used : MapFormat(io.micronaut.core.convert.format.MapFormat) StringConvention(io.micronaut.core.naming.conventions.StringConvention) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Aggregations

StringConvention (io.micronaut.core.naming.conventions.StringConvention)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 MapFormat (io.micronaut.core.convert.format.MapFormat)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 ConversionService (io.micronaut.core.convert.ConversionService)1 SocketUtils (io.micronaut.core.io.socket.SocketUtils)1 NameUtils (io.micronaut.core.naming.NameUtils)1 StaticOptimizations (io.micronaut.core.optim.StaticOptimizations)1 ClassUtils (io.micronaut.core.reflect.ClassUtils)1 Argument (io.micronaut.core.type.Argument)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 EnvironmentProperties (io.micronaut.core.util.EnvironmentProperties)1 StringUtils (io.micronaut.core.util.StringUtils)1 MapPropertyResolver (io.micronaut.core.value.MapPropertyResolver)1