Skip to content

Commit

Permalink
Add Runtime Default values in the static Config
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Mar 16, 2022
1 parent ea3688c commit 66572ff
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,36 @@ public static final class GenerateOperation implements AutoCloseable {
.setModifiers(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL);
clinit.writeStaticField(C_RUN_TIME_DEFAULTS_CONFIG_SOURCE, clinit.newInstance(RTDVCS_NEW));

// create the map for run time specified values config source
final ResultHandle specifiedRunTimeValues = clinit.newInstance(HM_NEW);
if (!liveReloadPossible) {
//we don't need these in devmode
//including it would just cache the first values
//but these can already just be read directly, as we are in the same JVM
for (Map.Entry<String, String> entry : specifiedRunTimeDefaultValues.entrySet()) {
clinit.invokeVirtualMethod(HM_PUT, specifiedRunTimeValues, clinit.load(entry.getKey()),
clinit.load(entry.getValue()));
}
}

final ResultHandle specifiedRunTimeSource = clinit.newInstance(PCS_NEW, specifiedRunTimeValues,
clinit.load("Specified default values"), clinit.load(Integer.MIN_VALUE + 100));

cc.getFieldCreator(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE)
.setModifiers(Opcodes.ACC_STATIC | (liveReloadPossible ? Opcodes.ACC_VOLATILE : Opcodes.ACC_FINAL));
clinit.writeStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE, specifiedRunTimeSource);

// the build time config, which is for user use only (not used by us other than for loading converters)
final ResultHandle buildTimeBuilder = clinit.invokeStaticMethod(CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED,
clinit.load(true), clinit.load(false), clinit.load(launchMode));
final ResultHandle array = clinit.newArray(ConfigSource[].class, 2);
// build time values
final ResultHandle array = clinit.newArray(ConfigSource[].class, 3);
// build time values (recorded visible for runtime)
clinit.writeArrayValue(array, 0, buildTimeConfigSource);
// build time defaults
// build time runtime defaults for Config Roots
clinit.writeArrayValue(array, 1, buildTimeRunTimeDefaultValuesConfigSource);
// runtime default values (recorded during build time)
clinit.writeArrayValue(array, 2, clinit.readStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE));

clinit.invokeVirtualMethod(SRCB_WITH_SOURCES, buildTimeBuilder, array);
// add safe static sources
for (String runtimeConfigSource : staticConfigSources) {
Expand Down Expand Up @@ -595,24 +617,6 @@ public void run() {
readConfig.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, runTimeBuilder, readConfig.newInstance(
MethodDescriptor.ofConstructor("io.quarkus.runtime.generated.ConfigSourceProviderImpl")));

// create the map for run time specified values config source
final ResultHandle specifiedRunTimeValues = clinit.newInstance(HM_NEW);
if (!liveReloadPossible) {
//we don't need these in devmode
//including it would just cache the first values
//but these can already just be read directly, as we are in the same JVM
for (Map.Entry<String, String> entry : specifiedRunTimeDefaultValues.entrySet()) {
clinit.invokeVirtualMethod(HM_PUT, specifiedRunTimeValues, clinit.load(entry.getKey()),
clinit.load(entry.getValue()));
}
}
final ResultHandle specifiedRunTimeSource = clinit.newInstance(PCS_NEW, specifiedRunTimeValues,
clinit.load("Specified default values"), clinit.load(Integer.MIN_VALUE + 100));

cc.getFieldCreator(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE)
.setModifiers(Opcodes.ACC_STATIC | (liveReloadPossible ? Opcodes.ACC_VOLATILE : Opcodes.ACC_FINAL));
clinit.writeStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE, specifiedRunTimeSource);

// add in the custom sources that bootstrap config needs
ResultHandle bootstrapConfigSourcesArray = null;
if (bootstrapConfigSetupNeeded()) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.config;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.microprofile.config.spi.ConfigSource;

import io.smallrye.config.ConfigSourceContext;
import io.smallrye.config.ConfigSourceFactory;
import io.smallrye.config.ConfigValue;
import io.smallrye.config.common.MapBackedConfigSource;

public class RuntimeInitConfigSourceFactory implements ConfigSourceFactory {
@Override
public Iterable<ConfigSource> getConfigSources(final ConfigSourceContext context) {
ConfigValue value = context.getValue("skip.build.sources");
if (value.getValue() != null && value.getValue().equals("true")) {
return List.of(new MapBackedConfigSource("RuntimeInitConfigSource", Map.of("config.static.init.my-prop", "1234")) {
});
} else {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class StaticInitConfigMappingInvalidTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(RuntimeInitConfigSource.class)
.addAsServiceProvider("org.eclipse.microprofile.config.spi.ConfigSource",
RuntimeInitConfigSource.class.getName()))
.addClass(RuntimeInitConfigSourceFactory.class)
.addAsServiceProvider("io.smallrye.config.ConfigSourceFactory",
StaticInitConfigSourceFactory.class.getName()))
.assertException(throwable -> {
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.inject.Inject;

Expand All @@ -9,23 +10,27 @@

import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.SmallRyeConfig;

public class StaticInitConfigMappingTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(StaticInitConfigSource.class)
.addAsServiceProvider("org.eclipse.microprofile.config.spi.ConfigSource",
StaticInitConfigSource.class.getName()));
.addClass(StaticInitConfigSourceFactory.class)
.addAsServiceProvider("io.smallrye.config.ConfigSourceFactory",
StaticInitConfigSourceFactory.class.getName()));

// This does not come from the static init Config, but it is registered in both static and runtime.
// If it doesn't fail, it means that the static mapping was done correctly.
@Inject
StaticInitConfigMapping mapping;
@Inject
SmallRyeConfig config;

@Test
void staticInitMapping() {
assertEquals("1234", mapping.myProp());
assertTrue(config.getConfigSource("StaticInitConfigSource").isPresent());
}

@ConfigMapping(prefix = "config.static.init")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.config;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.microprofile.config.spi.ConfigSource;

import io.quarkus.runtime.annotations.StaticInitSafe;
import io.smallrye.config.ConfigSourceContext;
import io.smallrye.config.ConfigSourceFactory;
import io.smallrye.config.ConfigValue;
import io.smallrye.config.common.MapBackedConfigSource;

@StaticInitSafe
public class StaticInitConfigSourceFactory implements ConfigSourceFactory {
@Override
public Iterable<ConfigSource> getConfigSources(final ConfigSourceContext context) {
ConfigValue value = context.getValue("skip.build.sources");
if (value.getValue() != null && value.getValue().equals("true")) {
return List.of(new MapBackedConfigSource("StaticInitConfigSource", Map.of("config.static.init.my-prop", "1234")) {
});
} else {
return Collections.emptyList();
}
}
}

0 comments on commit 66572ff

Please sign in to comment.