Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeneratedConfigBean should be easily available from Config-driven-services #6680

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected void appendMetaProps(StringBuilder builder,
String tag,
AtomicBoolean needsCustomMapOf) {
builder.append("\t\t").append(tag);
builder.append(".put(\"__meta\", Map.of(").append(ConfigBeanInfo.class.getName());
builder.append(".put(" + ConfigBeanInfo.class.getName() + ".TAG_META, Map.of(").append(ConfigBeanInfo.class.getName());
builder.append(".class.getName(),\n\t\t\t\t").append(MetaConfigBeanInfo.class.getName()).append(".builder()\n");
appendConfigBeanInfoAttributes(builder,
ctx.typeInfo(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
@Builder(implPrefix = "Meta")
public interface ConfigBeanInfo extends ConfigBean {

/**
* The tag used to represent the meta properties.
*/
String TAG_META = "__meta";

/**
* The attribute name for {@link #value()} ()}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package io.helidon.builder.config.spi;

import java.util.Optional;

import io.helidon.common.config.Config;

/**
* Every {@link io.helidon.builder.config.ConfigBean}-annotated type will also implement this contract.
*/
Expand All @@ -28,17 +32,25 @@ public interface GeneratedConfigBean extends GeneratedConfigBeanCommon {
*/

/**
* Set the instance id of this config bean.
* Returns the instance id assigned to this bean.
* Note that the instance id is typically assigned the {@link Config#key()} in most circumstances.
*
* @param val the new instance identifier
* @return the instance id assigned to this bean
*/
void __instanceId(String val);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: The setter was taken out of this interface and pushed down to the abstract impl.

String __instanceId();

/**
* Returns the existing instance identifier.
* Returns the {@link Config#name()} if available.
*
* @return the instance identifier
* @return the config name or else empty
*/
String __instanceId();
Optional<String> __name();

/**
* Returns the generated config bean meta information.
*
* @return the config bean meta information
*/
MetaConfigBeanInfo __metaInfo();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

package io.helidon.builder.config.spi;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import io.helidon.common.config.Config;

/**
* Minimal implementation for the {@link GeneratedConfigBeanCommon}. This is the base for generated config beans.
*/
public abstract class GeneratedConfigBeanBase implements GeneratedConfigBeanCommon {
public abstract class GeneratedConfigBeanBase implements GeneratedConfigBean {
private final Config config;
private String instanceId;

Expand All @@ -46,17 +48,37 @@ public Optional<Config> __config() {
return Optional.ofNullable(config);
}

@Override
public Optional<String> __name() {
if (__config().isEmpty()) {
return Optional.empty();
}

return Optional.of(__config().get().name());
}

/**
* Returns the instance id assigned to this bean.
* The meta attributes for this generated config bean.
*
* @return the instance id assigned to this bean
* @return meta attributes for this config bean
*/
public abstract Map<String, Map<String, Object>> __metaProps();

@Override
public MetaConfigBeanInfo __metaInfo() {
Map<String, Object> meta = __metaProps().get(ConfigBeanInfo.TAG_META);
MetaConfigBeanInfo metaInfo = (MetaConfigBeanInfo) meta.get(ConfigBeanInfo.class.getName());
return Objects.requireNonNull(metaInfo);
}

@Override
public String __instanceId() {
return instanceId;
}

/**
* Assigns the instance id assigned to this bean.
* Note that the instance id is typically assigned the {@link Config#key()} in most circumstances.
*
* @param val the new instance id for this bean
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public interface HelidonConfigBeanRegistry {
/**
* Returns all config beans indexed by its config key.
*
* @param <CB> the config bean type
* @param <GCB> the generated config bean type
* @return all config beans
*/
<CB> Map<String, Collection<CB>> allConfigBeans();
<GCB extends GeneratedConfigBean> Map<String, Collection<GCB>> allConfigBeans();

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class HelidonConfigResolver implements ConfigResolver, ConfigResolverProv
/**
* Tag that represents meta information about the attribute. Used in the maps for various methods herein.
*/
public static final String TAG_META = "__meta";
public static final String TAG_META = ConfigBeanInfo.TAG_META;

/**
* Tag that represents the component type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ static void internalMetaAttributes(StringBuilder builder) {
builder.append("\tpublic static Map<String, Map<String, Object>> __metaAttributes() {\n"
+ "\t\treturn ").append(BodyContext.TAG_META_PROPS).append(";\n"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: unfortunately we still need the static approach for other use cases.

+ "\t}\n\n");

GenerateJavadoc.internalMetaAttributes(builder);
builder.append("\tpublic Map<String, Map<String, Object>> __metaProps() {\n"
+ "\t\treturn __metaAttributes();\n\t}\n\n");
}

static void nonOptionalSetter(StringBuilder builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.List;
import java.util.Map;

import io.helidon.builder.config.spi.GeneratedConfigBean;
import io.helidon.builder.config.spi.MetaConfigBeanInfo;
import io.helidon.builder.config.testsubjects.DefaultTestClientConfig;
import io.helidon.builder.config.testsubjects.DefaultTestServerConfig;
import io.helidon.builder.config.testsubjects.TestClientConfig;
Expand Down Expand Up @@ -72,6 +74,18 @@ void acceptConfig() {
contains("a", "b", "c"));
assertThat(serverConfig.toString(),
endsWith("(name=server, port=8080, cipherSuites=[a, b, c], pswd=not-null, description=Optional[test])"));
GeneratedConfigBean generatedCB = (GeneratedConfigBean) serverConfig;
assertThat(generatedCB.__name(),
optionalValue(equalTo("")));
assertThat(generatedCB.__metaInfo(),
equalTo(MetaConfigBeanInfo.builder()
.value("test-server")
.repeatable(true)
.drivesActivation(false)
.atLeastOne(true)
.wantDefaultConfigBean(false)
.levelType(io.helidon.builder.config.ConfigBean.LevelType.ROOT)
.build()));

TestClientConfig clientConfig = DefaultTestClientConfig.toBuilder(cfg).build();
assertThat(clientConfig.name(),
Expand All @@ -91,6 +105,18 @@ void acceptConfig() {
assertThat(clientConfig.toString(),
endsWith("(name=server, port=8080, cipherSuites=[a, b, c], pswd=not-null, "
+ "serverPort=0, headers={headers.1=header2, headers.0=header1})"));
generatedCB = (GeneratedConfigBean) clientConfig;
assertThat(generatedCB.__name(),
optionalValue(equalTo("")));
assertThat(generatedCB.__metaInfo(),
equalTo(MetaConfigBeanInfo.builder()
.value("test-client")
.repeatable(true)
.drivesActivation(false)
.atLeastOne(false)
.wantDefaultConfigBean(false)
.levelType(io.helidon.builder.config.ConfigBean.LevelType.ROOT)
.build()));
}

@Test
Expand All @@ -103,6 +129,10 @@ void emptyConfig() {
equalTo("default"));
assertThat(serverConfig.port(),
equalTo(0));

GeneratedConfigBean generatedCB = (GeneratedConfigBean) serverConfig;
assertThat(generatedCB.__name(),
optionalValue(equalTo("")));
}

/**
Expand Down Expand Up @@ -148,6 +178,10 @@ void noConfig() {
equalTo(Map.of()));
assertThat(clientConfig.cipherSuites(),
equalTo(List.of()));

GeneratedConfigBean generatedCB = (GeneratedConfigBean) clientConfig;
assertThat(generatedCB.__name(),
optionalEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public Map<String, Map<String, Object>> configBeanAttributes() {

// note that all responsibilities to resolve is delegated to the root provider
@Override
@SuppressWarnings("unchecked")
@SuppressWarnings({"unchecked", "rawtypes"})
public Optional<Object> resolve(InjectionPointInfo ipInfo,
PicoServices picoServices,
ServiceProvider<?> serviceProvider,
Expand Down Expand Up @@ -335,7 +335,7 @@ public Optional<Object> resolve(InjectionPointInfo ipInfo,
return Optional.of(configBeanType());
}

return (Optional<Object>) configBean();
return (Optional) configBean();
}

/**
Expand Down
Loading