Skip to content

Commit

Permalink
#384 address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Emily Jiang <[email protected]>
  • Loading branch information
Emily-Jiang committed Jan 23, 2019
1 parent 7907ec8 commit 0e6f308
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,6 @@ public interface ConfigAccessor<T> {
*/
String getPropertyName();

/**
* Returns the actual key which led to successful resolution and corresponds to the resolved value.
* This is useful when {@link ConfigAccessorBuilder#addLookupSuffix(String)} is used.
* Otherwise the resolved key should always be equal to the original key.
* This method is provided for cases, when parameterized resolution is
* requested and some of the fallback keys is used.
*
* This should be called only after calling {@link #getValue()} otherwise the value is undefined (but likely
* null).
*
* Note that this will only give you the resolved key from the last non-cached value resolving.
* @return the actual property name which led to successful resolution and corresponds to the resolved value.
*/
String getResolvedPropertyName();

/**
* Returns the default value provided by {@link ConfigAccessorBuilder#withDefault(Object)}
* or {@link ConfigAccessorBuilder#withStringDefault(String)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
package org.eclipse.microprofile.config;


import java.time.temporal.ChronoUnit;
import java.time.Duration;

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

Expand Down Expand Up @@ -82,11 +82,10 @@ public interface ConfigAccessorBuilder<T> {
* This is done by invoking the callback provided to the {@code ConfigSource} via
* {@link org.eclipse.microprofile.config.spi.ConfigSource#onAttributeChange(java.util.function.Consumer)}.
*
* @param value the amount of the TimeUnit to wait
* @param timeUnit the ChronoUnit for the value
* @param duration the duration (e.g. 3s) to cache for
* @return This builder
*/
ConfigAccessorBuilder<T> cacheFor(long value, ChronoUnit timeUnit);
ConfigAccessorBuilder<T> cacheFor(Duration duration);

/**
* Whether to evaluate variables in configured values.
Expand All @@ -107,52 +106,6 @@ public interface ConfigAccessorBuilder<T> {
*/
ConfigAccessorBuilder<T> evaluateVariables(boolean evaluateVariables);

/**
* The methods {@link ConfigAccessorBuilder#addLookupSuffix(String)}
* append the given parameters as optional suffixes to the {@link ConfigAccessor#getPropertyName()}.
* Those methods can be called multiple times.
* Each time the given suffix will be added to the end of suffix chain.
*
* This very version
*
* <p>Usage:
* <pre>
* String tenant = getCurrentTenant();
*
* Integer timeout = config.access("some.server.url", Integer.class)
* .addLookupSuffix(tenant)
* .addLookupSuffix(config.access("config.projectStage", String.class).build())
* .build()
* .getValue();
* </pre>
*
* Given the current tenant name is 'myComp' and the property
* {@code config.projectStage} is 'Production' this would lead to the following lookup order:
*
* <ul>
* <li>"some.server.url.myComp.Production"</li>
* <li>"some.server.url.myComp"</li>
* <li>"some.server.url.Production"</li>
* <li>"some.server.url"</li>
* </ul>
*
* The algorithm to use in {@link ConfigAccessor#getValue()} is a binary count down.
* Every parameter is either available (1) or not (0).
* Having 3 parameters, we start with binary {@code 111} and count down to zero.
* The first combination which resolves to a result is being treated as result.
*
* @param suffixValue fixed String to be used as suffix
* @return This builder
*/
ConfigAccessorBuilder<T> addLookupSuffix(String suffixValue);

/**
*
* @param suffixAccessor {@link ConfigAccessor} to be used to resolve the suffix.
* @return This builder
* @see #addLookupSuffix(String)
*/
ConfigAccessorBuilder<T> addLookupSuffix(ConfigAccessor<String> suffixAccessor);
/**
* Build a ConfigAccessor
* @return the configAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface ConfigProperty {
String UNCONFIGURED_VALUE="org.eclipse.microprofile.config.configproperty.unconfiguredvalue";
String UNCONFIGURED_VALUE="org.eclipse.microprofile.config.configproperty.unconfigureddvalue";
/**
* Provide a way to specify {@code null} value for a property.
* e.g. The following example is to set the default value of {@code my.port} to null if the property is not specified in any config sources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.eclipse.microprofile.config.tck;

import java.time.temporal.ChronoUnit;
import java.time.Duration;

import javax.inject.Inject;

Expand Down Expand Up @@ -108,41 +108,6 @@ public void testVariableReplacement() {
"http://${tck.config.variable.baseHost}/endpointOne");
}

@Test
public void testLookupChain() {
// set the projectstage to 'Production'
ConfigurableConfigSource.configure(config, "javaconfig.projectStage", "Production");

/**
* 1 1 -> com.foo.myapp.mycorp.Production
* 1 0 -> com.foo.myapp.mycorp
* 0 1 -> com.foo.myapp.Production
* 0 0 -> com.foo.myapp
*
*/
ConfigAccessor<String> cv = config.access("com.foo.myapp", String.class)
.addLookupSuffix("mycorp")
.addLookupSuffix(config.access("javaconfig.projectStage", String.class).build().getValue()).build();

Assert.assertFalse(cv.getOptionalValue().isPresent());

ConfigurableConfigSource.configure(config, "com.foo.myapp", "TheDefault");
Assert.assertEquals(cv.getValue(), "TheDefault");
Assert.assertEquals(cv.getResolvedPropertyName(), "com.foo.myapp");

ConfigurableConfigSource.configure(config, "com.foo.myapp.Production", "BasicWithProjectStage");
Assert.assertEquals(cv.getValue(), "BasicWithProjectStage");
Assert.assertEquals(cv.getResolvedPropertyName(), "com.foo.myapp.Production");

ConfigurableConfigSource.configure(config, "com.foo.myapp.mycorp", "WithTenant");
Assert.assertEquals(cv.getValue(), "WithTenant");
Assert.assertEquals(cv.getResolvedPropertyName(), "com.foo.myapp.mycorp");

ConfigurableConfigSource.configure(config, "com.foo.myapp.mycorp.Production", "WithTenantAndProjectStage");
Assert.assertEquals(cv.getValue(), "WithTenantAndProjectStage");
Assert.assertEquals(cv.getResolvedPropertyName(), "com.foo.myapp.mycorp.Production");
}

@Test
public void testIntegerConverter() {
Assert.assertEquals(config.access("tck.config.test.javaconfig.configvalue.integer", Integer.class).build().getValue(),
Expand Down Expand Up @@ -217,7 +182,7 @@ public void testBooleanConverter() {
public void testCacheFor() throws Exception {
String key = "tck.config.test.javaconfig.cachefor.key";
System.setProperty(key, "firstvalue");
ConfigAccessor<String> val = config.access(key, String.class).cacheFor(30, ChronoUnit.MILLIS).build();
ConfigAccessor<String> val = config.access(key, String.class).cacheFor(Duration.ofMillis(30)).build();
Assert.assertEquals(val.getValue(), "firstvalue");

// immediately change the value
Expand Down

0 comments on commit 0e6f308

Please sign in to comment.