Skip to content

Commit

Permalink
Use new SmallRye Config map default feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Jul 25, 2023
1 parent 828f9a1 commit a99652a
Show file tree
Hide file tree
Showing 49 changed files with 297 additions and 492 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r
} else if (annotationName.equals(ANNOTATION_CONFIG_DOC_DEFAULT)) {
defaultValueDoc = annotationMirror.getElementValues().values().iterator().next().getValue().toString();
} else if (annotationName.equals(ANNOTATION_CONFIG_WITH_DEFAULT)) {
defaultValue = annotationMirror.getElementValues().values().iterator().next().getValue().toString();
defaultValue = annotationMirror.getElementValues().values().isEmpty() ? null
: annotationMirror.getElementValues().values().iterator().next().getValue().toString();
} else if (annotationName.equals(ANNOTATION_CONFIG_WITH_UNNAMED_KEY)) {
unnamedMapKey = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

import io.quarkus.runtime.LaunchMode;
import io.smallrye.config.ConfigMappingContext;
import io.smallrye.config.ConfigMappingContextCreator;
import io.smallrye.config.ConfigSourceInterceptor;
import io.smallrye.config.ConfigSourceInterceptorContext;
import io.smallrye.config.ConfigSourceInterceptorFactory;
Expand Down Expand Up @@ -313,16 +311,6 @@ public static <T> Optional<T> getFirstOptionalValue(List<String> propertyNames,
return Optional.empty();
}

/**
* Get a config group initialized with default values as specified in the config.
*/
public static <T> T getInitializedConfigGroup(Class<T> configGroup, String mapPath) {
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
ConfigMappingContext context = ConfigMappingContextCreator.createConfigMappingContext(config);
context.getStringBuilder().replace(0, context.getStringBuilder().length(), mapPath);
return context.constructGroup(configGroup);
}

private static class ConfigBuilderComparator implements Comparator<ConfigBuilder> {
private static final ConfigBuilderComparator INSTANCE = new ConfigBuilderComparator();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -312,40 +312,31 @@ private List<AggregatedDataSourceBuildTimeConfigBuildItem> getAggregatedConfigBu
List<DefaultDataSourceDbKindBuildItem> defaultDbKinds) {
List<AggregatedDataSourceBuildTimeConfigBuildItem> dataSources = new ArrayList<>();

Optional<String> effectiveDbKind = DefaultDataSourceDbKindBuildItem
.resolve(dataSourcesBuildTimeConfig.defaultDataSource().dbKind(), defaultDbKinds,
dataSourcesBuildTimeConfig.defaultDataSource().devservices().enabled()
.orElse(dataSourcesBuildTimeConfig.namedDataSources().isEmpty()),
curateOutcomeBuildItem);

if (effectiveDbKind.isPresent()) {
if (dataSourcesJdbcBuildTimeConfig.jdbc().enabled()) {
dataSources.add(new AggregatedDataSourceBuildTimeConfigBuildItem(DataSourceUtil.DEFAULT_DATASOURCE_NAME,
dataSourcesBuildTimeConfig.defaultDataSource(),
dataSourcesJdbcBuildTimeConfig.jdbc(),
effectiveDbKind.get(),
resolveDriver(DataSourceUtil.DEFAULT_DATASOURCE_NAME, effectiveDbKind.get(),
dataSourcesJdbcBuildTimeConfig.jdbc(), jdbcDriverBuildItems)));
}
}
for (Entry<String, DataSourceBuildTimeConfig> entry : dataSourcesBuildTimeConfig.namedDataSources().entrySet()) {
for (Entry<String, DataSourceBuildTimeConfig> entry : dataSourcesBuildTimeConfig.dataSources().entrySet()) {
DataSourceJdbcBuildTimeConfig jdbcBuildTimeConfig = dataSourcesJdbcBuildTimeConfig
.getDataSourceJdbcBuildTimeConfig(entry.getKey());
.dataSources().get(entry.getKey()).jdbc();
if (!jdbcBuildTimeConfig.enabled()) {
continue;
}
Optional<String> dbKind = DefaultDataSourceDbKindBuildItem

boolean enableImplicitResolution = DataSourceUtil.isDefault(entry.getKey())
? entry.getValue().devservices().enabled().orElse(!dataSourcesBuildTimeConfig.hasNamedDataSources())
: true;

Optional<String> effectiveDbKind = DefaultDataSourceDbKindBuildItem
.resolve(entry.getValue().dbKind(), defaultDbKinds,
true,
enableImplicitResolution,
curateOutcomeBuildItem);
if (!dbKind.isPresent()) {

if (!effectiveDbKind.isPresent()) {
continue;
}

dataSources.add(new AggregatedDataSourceBuildTimeConfigBuildItem(entry.getKey(),
entry.getValue(),
jdbcBuildTimeConfig,
dbKind.get(),
resolveDriver(entry.getKey(), dbKind.get(), jdbcBuildTimeConfig, jdbcDriverBuildItems)));
effectiveDbKind.get(),
resolveDriver(entry.getKey(), effectiveDbKind.get(), jdbcBuildTimeConfig, jdbcDriverBuildItems)));
}

return dataSources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public AgroalDataSource doCreateDataSource(String dataSourceName) {
}

DataSourceJdbcBuildTimeConfig dataSourceJdbcBuildTimeConfig = dataSourcesJdbcBuildTimeConfig
.getDataSourceJdbcBuildTimeConfig(dataSourceName);
DataSourceRuntimeConfig dataSourceRuntimeConfig = dataSourcesRuntimeConfig.getDataSourceRuntimeConfig(dataSourceName);
.dataSources().get(dataSourceName).jdbc();
DataSourceRuntimeConfig dataSourceRuntimeConfig = dataSourcesRuntimeConfig.dataSources().get(dataSourceName);
DataSourceJdbcRuntimeConfig dataSourceJdbcRuntimeConfig = dataSourcesJdbcRuntimeConfig
.getDataSourceJdbcRuntimeConfig(dataSourceName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefaults;
import io.smallrye.config.WithParentName;
import io.smallrye.config.WithUnnamedKey;

@ConfigMapping(prefix = "quarkus.datasource")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public interface DataSourcesJdbcBuildTimeConfig {

/**
* The default datasource.
*/
DataSourceJdbcBuildTimeConfig jdbc();

/**
* Additional named datasources.
* Datasources.
*/
@ConfigDocSection
@ConfigDocMapKey("datasource-name")
@WithParentName
Map<String, DataSourceJdbcOuterNamedBuildTimeConfig> namedDataSources();
@WithDefaults
@WithUnnamedKey(DataSourceUtil.DEFAULT_DATASOURCE_NAME)
Map<String, DataSourceJdbcOuterNamedBuildTimeConfig> dataSources();

@ConfigGroup
public interface DataSourceJdbcOuterNamedBuildTimeConfig {
Expand All @@ -37,18 +35,4 @@ public interface DataSourceJdbcOuterNamedBuildTimeConfig {
*/
DataSourceJdbcBuildTimeConfig jdbc();
}

default DataSourceJdbcBuildTimeConfig getDataSourceJdbcBuildTimeConfig(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return jdbc();
}

DataSourceJdbcOuterNamedBuildTimeConfig dataSourceJdbcBuildTimeConfig = namedDataSources().get(dataSourceName);

if (dataSourceJdbcBuildTimeConfig != null) {
return dataSourceJdbcBuildTimeConfig.jdbc();
}

return ConfigUtils.getInitializedConfigGroup(DataSourceJdbcBuildTimeConfig.class, "quarkus.datasource.*.jdbc");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import java.util.Map;

import io.quarkus.agroal.runtime.DataSourcesJdbcRuntimeConfig.DataSourceJdbcOuterNamedRuntimeConfig;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.runtime.annotations.ConfigDocMapKey;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefaults;
import io.smallrye.config.WithParentName;

@ConfigMapping(prefix = "quarkus.datasource")
Expand All @@ -28,6 +27,7 @@ public interface DataSourcesJdbcRuntimeConfig {
@ConfigDocSection
@ConfigDocMapKey("datasource-name")
@WithParentName
@WithDefaults
Map<String, DataSourceJdbcOuterNamedRuntimeConfig> namedDataSources();

@ConfigGroup
Expand All @@ -44,12 +44,6 @@ default DataSourceJdbcRuntimeConfig getDataSourceJdbcRuntimeConfig(String dataSo
return jdbc();
}

DataSourceJdbcOuterNamedRuntimeConfig dataSourceJdbcRuntimeConfig = namedDataSources().get(dataSourceName);

if (dataSourceJdbcRuntimeConfig != null) {
return dataSourceJdbcRuntimeConfig.jdbc();
}

return ConfigUtils.getInitializedConfigGroup(DataSourceJdbcRuntimeConfig.class, "quarkus.datasource.*.jdbc");
return namedDataSources().get(dataSourceName).jdbc();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public interface DevServicesDatasourceProvider {

RunningDevServicesDatasource startDatabase(Optional<String> username, Optional<String> password,
Optional<String> datasourceName,
String datasourceName,
DevServicesDatasourceContainerConfig devServicesDatasourceContainerConfig,
LaunchMode launchMode,
Optional<Duration> startupTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.datasource.common.runtime.DataSourceUtil;

public final class DevServicesDatasourceResultBuildItem extends SimpleBuildItem {

final DbResult defaultDatasource;
final Map<String, DbResult> namedDatasources;
final Map<String, DbResult> dataSources;

public DevServicesDatasourceResultBuildItem(DbResult defaultDatasource, Map<String, DbResult> namedDatasources) {
this.defaultDatasource = defaultDatasource;
this.namedDatasources = Collections.unmodifiableMap(namedDatasources);
public DevServicesDatasourceResultBuildItem(Map<String, DbResult> namedDatasources) {
this.dataSources = Collections.unmodifiableMap(namedDatasources);
}

public DbResult getDefaultDatasource() {
return defaultDatasource;
return dataSources.get(DataSourceUtil.DEFAULT_DATASOURCE_NAME);
}

public Map<String, DbResult> getNamedDatasources() {
return namedDatasources;
return dataSources.entrySet().stream()
.filter(e -> !DataSourceUtil.isDefault(e.getKey()))
.collect(Collectors.toUnmodifiableMap(e -> e.getKey(), e -> e.getValue()));
}

public Map<String, DbResult> getDatasources() {
return dataSources;
}

public static DbResult resolve(Optional<DevServicesDatasourceResultBuildItem> devDbResultBuildItem, String dataSourceName) {
if (devDbResultBuildItem.isPresent()) {
if (dataSourceName.equals(DataSourceUtil.DEFAULT_DATASOURCE_NAME)) {
return devDbResultBuildItem.get().defaultDatasource;
}
return devDbResultBuildItem.get().namedDatasources.get(dataSourceName);
return devDbResultBuildItem.get().dataSources.get(dataSourceName);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.datasource.deployment;

import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -11,13 +12,13 @@ public class DataSourceBindingProcessor {

@BuildStep
public void process(DataSourcesBuildTimeConfig config, BuildProducer<ServiceBindingQualifierBuildItem> bindings) {
config.defaultDataSource().dbKind().ifPresent(k -> {
bindings.produce(new ServiceBindingQualifierBuildItem(k, k, DEFAULT_DATASOURCE));
});

config.namedDataSources().forEach((n, c) -> {
config.dataSources().forEach((name, c) -> {
c.dbKind().ifPresent(dbKind -> {
bindings.produce(new ServiceBindingQualifierBuildItem(dbKind, n));
if (DataSourceUtil.isDefault(name)) {
bindings.produce(new ServiceBindingQualifierBuildItem(dbKind, dbKind, DEFAULT_DATASOURCE));
} else {
bindings.produce(new ServiceBindingQualifierBuildItem(dbKind, name));
}
});
});
}
Expand Down
Loading

0 comments on commit a99652a

Please sign in to comment.