Skip to content

Commit

Permalink
remove optional args from ConfigBeanRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
trentjeff committed Mar 29, 2023
1 parent 3e78f11 commit 63f889b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import io.helidon.builder.config.spi.ConfigBeanInfo;
Expand Down Expand Up @@ -56,38 +55,48 @@ public interface ConfigBeanRegistry extends HelidonConfigBeanRegistry {
List<ConfiguredServiceProvider<?, ?>> configuredServiceProviders();

/**
* These are the managed/slave service providers that are associated with config bean instances with the config key provided.
* These are the managed/slave service providers that are associated with config bean instances with the config {@code key}
* provided.
*
* @param key the config options key - note that this is a partial key - and not relative to the parent - the same
* key used by {@link io.helidon.builder.config.ConfigBean#value()}.
* @param key the config options key - note that this is a partial key - and not relative to the parent - the same
* key used by {@link io.helidon.builder.config.ConfigBean#value()}.
* @return the list of configured services
*/
List<ConfiguredServiceProvider<?, ?>> configuredServiceProvidersConfiguredBy(String key);

/**
* Returns all the known config beans in order of rank given the short config key / alias. Callers should understand
* that this list might be incomplete until ready state is reached (see {@link #ready()}).
* Returns all the known config beans in order of rank given the {@code key}. Callers should understand
* that this list might be incomplete until ready state is reached (see {@link #ready()}). Note also that callers should
* attempt to use {@link #configBeansByConfigKey(String)} whenever possible since it will generate more precise matches.
*
* @param key the config options key - note that this is a partial key - and not relative to the parent - the same
* key used by {@link io.helidon.builder.config.ConfigBean#value()}.
* @param fullConfigKey optionally, the full config key - if not passed will return the list of all matches
* using just the key
* @return the set of known config keys
*/
Set<?> configBeansByConfigKey(String key);

/**
* Returns all the known config beans in order of rank matching the {@code key} and {@code fullConfigKey}. Callers should
* understand that this list might be incomplete until ready state is reached (see {@link #ready()}).
*
* @param key the config options key - note that this is a partial key - and not relative to the parent - the same
* key used by {@link io.helidon.builder.config.ConfigBean#value()}.
* @param fullConfigKey the full config key
* @return the set of known config keys matching the provided criteria
*/
Set<?> configBeansByConfigKey(String key,
Optional<String> fullConfigKey);
String fullConfigKey);

/**
* Similar to {@link #configBeansByConfigKey}, but instead returns all the known config beans in a
* map where the key of the map is the config key.
*
* @param key the config options key - note that this is a partial key - and not relative to the parent - the same
* key used by {@link io.helidon.builder.config.ConfigBean#value()}.
* @param fullConfigKey optionally, the full config key - if not passed will return the list of all matches
* using just the key
* @return the map of known config keys
* @param fullConfigKey the full config key
* @return the map of known config keys to config beans
*/
Map<String, ?> configBeanMapByConfigKey(String key,
Optional<String> fullConfigKey);
String fullConfigKey);

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static List<?> findInConfigBeanRegistryAsList(ResolutionContext ctx,
DefaultPicoConfigBeanRegistry cbr = (DefaultPicoConfigBeanRegistry) ConfigBeanRegistryHolder.configBeanRegistry()
.orElseThrow();
String fullConfigKey = fullConfigKeyOf(safeDowncastOf(ctx.config()), request.configKey(), meta);
Set<?> result = cbr.configBeansByConfigKey(request.configKey(), Optional.of(fullConfigKey));
Set<?> result = cbr.configBeansByConfigKey(request.configKey(), fullConfigKey);
return new ArrayList<>(result);
}

Expand All @@ -135,7 +135,7 @@ static Map<String, Object> findInConfigBeanRegistryAsMap(ResolutionContext ctx,
DefaultPicoConfigBeanRegistry cbr = (DefaultPicoConfigBeanRegistry) ConfigBeanRegistryHolder.configBeanRegistry()
.orElseThrow();
String fullConfigKey = fullConfigKeyOf(safeDowncastOf(ctx.config()), request.configKey(), meta);
Map<String, ?> result = cbr.configBeanMapByConfigKey(request.configKey(), Optional.of(fullConfigKey));
Map<String, ?> result = cbr.configBeanMapByConfigKey(request.configKey(), fullConfigKey);
return Objects.requireNonNull((Map<String, Object>) result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,36 +253,19 @@ public boolean ready() {
}

@Override
public Set<?> configBeansByConfigKey(String key,
Optional<String> optFullConfigKey) {
List<ConfiguredServiceProvider<?, ?>> cspsUsingSameKey =
configuredServiceProvidersByConfigKey.get(Objects.requireNonNull(key));
if (cspsUsingSameKey == null) {
return Set.of();
}
public Set<?> configBeansByConfigKey(String key) {
return configBeansByConfigKey(key, Optional.empty());
}

Set<Object> result = new LinkedHashSet<>();
cspsUsingSameKey.stream()
.filter(csp -> csp instanceof AbstractConfiguredServiceProvider)
.map(AbstractConfiguredServiceProvider.class::cast)
.forEach(csp -> {
Map<String, ?> configBeans = csp.configBeanMap();
if (optFullConfigKey.isEmpty()) {
result.addAll(configBeans.values());
} else {
configBeans.forEach((k, v) -> {
if (optFullConfigKey.get().equals(k)) {
result.add(v);
}
});
}
});
return result;
@Override
public Set<?> configBeansByConfigKey(String key,
String fullConfigKey) {
return configBeansByConfigKey(key, Optional.of(fullConfigKey));
}

@Override
public Map<String, ?> configBeanMapByConfigKey(String key,
Optional<String> optFullConfigKey) {
String fullConfigKey) {
List<ConfiguredServiceProvider<?, ?>> cspsUsingSameKey =
configuredServiceProvidersByConfigKey.get(Objects.requireNonNull(key));
if (cspsUsingSameKey == null) {
Expand All @@ -296,7 +279,7 @@ public Set<?> configBeansByConfigKey(String key,
.forEach(csp -> {
Map<String, ?> configBeans = csp.configBeanMap();
configBeans.forEach((k, v) -> {
if (optFullConfigKey.isEmpty() || optFullConfigKey.get().equals(k)) {
if (fullConfigKey.isEmpty() || fullConfigKey.equals(k)) {
Object prev = result.put(k, v);
if (prev != null && prev != v) {
throw new IllegalStateException("had two entries with the same key: " + prev + " and " + v);
Expand Down Expand Up @@ -526,6 +509,33 @@ <CB> void registerConfigBean(Object configBean,
csp.registerConfigBean(instanceId, configBean);
}

private Set<?> configBeansByConfigKey(String key,
Optional<String> optFullConfigKey) {
List<ConfiguredServiceProvider<?, ?>> cspsUsingSameKey =
configuredServiceProvidersByConfigKey.get(Objects.requireNonNull(key));
if (cspsUsingSameKey == null) {
return Set.of();
}

Set<Object> result = new LinkedHashSet<>();
cspsUsingSameKey.stream()
.filter(csp -> csp instanceof AbstractConfiguredServiceProvider)
.map(AbstractConfiguredServiceProvider.class::cast)
.forEach(csp -> {
Map<String, ?> configBeans = csp.configBeanMap();
if (optFullConfigKey.isEmpty()) {
result.addAll(configBeans.values());
} else {
configBeans.forEach((k, v) -> {
if (optFullConfigKey.get().equals(k)) {
result.add(v);
}
});
}
});
return result;
}

private void initialize(Config commonCfg) {
if (configuredServiceProvidersByConfigKey.isEmpty()) {
LOGGER.log(System.Logger.Level.DEBUG, "No config driven services found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -100,7 +99,7 @@ void serverConfigWithOneSocketConfigNested() {
"fake-server"
));

Set<?> configBeans = cbr.configBeansByConfigKey("fake-server", Optional.empty());
Set<?> configBeans = cbr.configBeansByConfigKey("fake-server");
assertThat(configBeans.toString(), configBeans.size(),
is(1));

Expand Down

0 comments on commit 63f889b

Please sign in to comment.