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

feat(experimentalIdentityAndAuth): update ConfigField::configFieldWriter() #1083

Merged
merged 4 commits into from
Dec 12, 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 @@ -33,7 +33,7 @@ public final record ConfigField(
Type type,
Symbol inputType,
Symbol resolvedType,
BiConsumer<TypeScriptWriter, ConfigField> configFieldWriter,
Optional<BiConsumer<TypeScriptWriter, ConfigField>> configFieldWriter,
Copy link
Contributor

Choose a reason for hiding this comment

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

Optionals as inputs will get you in trouble with Brian Goetz, but I defer 😄

Optional<Consumer<TypeScriptWriter>> docs
) implements ToSmithyBuilder<ConfigField> {

Expand Down Expand Up @@ -63,7 +63,7 @@ public Builder toBuilder() {
.type(type)
.inputType(inputType)
.resolvedType(resolvedType)
.configFieldWriter(configFieldWriter)
.configFieldWriter(configFieldWriter.orElse(null))
.docs(docs.orElse(null));
}

Expand All @@ -77,17 +77,12 @@ public static final class Builder implements SmithyBuilder<ConfigField> {

@Override
public ConfigField build() {
if (configFieldWriter == null) {
configFieldWriter = type.equals(Type.MAIN)
? ConfigField::defaultMainConfigFieldWriter
: ConfigField::defaultAuxiliaryConfigFieldWriter;
}
return new ConfigField(
SmithyBuilder.requiredState("name", name),
SmithyBuilder.requiredState("type", type),
SmithyBuilder.requiredState("inputType", inputType),
SmithyBuilder.requiredState("resolvedType", resolvedType),
SmithyBuilder.requiredState("configFieldWriter", configFieldWriter),
Optional.ofNullable(configFieldWriter),
Optional.ofNullable(docs));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import software.amazon.smithy.typescript.codegen.auth.http.sections.DefaultHttpAuthSchemeProviderFunctionCodeSection;
import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthOptionFunctionCodeSection;
import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthOptionFunctionsCodeSection;
import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthSchemeParametersInterfaceCodeSection;
import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthSchemeParametersProviderInterfaceCodeSection;
import software.amazon.smithy.typescript.codegen.auth.http.sections.HttpAuthSchemeProviderInterfaceCodeSection;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
Expand Down Expand Up @@ -116,11 +117,12 @@ export interface WeatherHttpAuthSchemeParameters extends HttpAuthSchemeParameter
*/
private void generateHttpAuthSchemeParametersInterface() {
delegator.useFileWriter(AuthUtils.HTTP_AUTH_SCHEME_PROVIDER_PATH, w -> {
w.pushState(HttpAuthSchemeProviderInterfaceCodeSection.builder()
w.pushState(HttpAuthSchemeParametersInterfaceCodeSection.builder()
.service(serviceShape)
.settings(settings)
.model(model)
.symbolProvider(symbolProvider)
.httpAuthSchemeParameters(httpAuthSchemeParameters)
.build());
w.addDependency(TypeScriptDependency.SMITHY_TYPES);
w.addImport("HttpAuthSchemeParameters", null, TypeScriptDependency.SMITHY_TYPES);
Expand Down Expand Up @@ -328,7 +330,7 @@ private void generateHttpAuthOptionFunction(
w.popState();
}

private static String normalizeAuthSchemeName(ShapeId shapeId) {
public static String normalizeAuthSchemeName(ShapeId shapeId) {
return String.join("", Arrays
.asList(shapeId.toString().split("[.#]"))
.stream().map(StringUtils::capitalize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

package software.amazon.smithy.typescript.codegen.auth.http.integration;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import software.amazon.smithy.codegen.core.CodegenException;
import java.util.stream.Collectors;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
Expand Down Expand Up @@ -146,30 +145,15 @@ private getIdentityProviderConfigProvider() {
s.getModel(),
s.getSettings());
ServiceIndex serviceIndex = ServiceIndex.of(s.getModel());
Map<ShapeId, HttpAuthScheme> httpAuthSchemes
= AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(s.getService(), serviceIndex, authIndex);
// TODO(experimentalIdentityAndAuth): figure out a better deduping strategy
Map<String, ConfigField> visitedConfigFields = new HashMap<>();
Map<ShapeId, HttpAuthScheme> httpAuthSchemes =
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(s.getService(), serviceIndex, authIndex);
for (HttpAuthScheme scheme : httpAuthSchemes.values()) {
if (scheme == null) {
continue;
}
for (ConfigField configField : scheme.getConfigFields()) {
if (visitedConfigFields.containsKey(configField.name())) {
ConfigField visitedConfigField = visitedConfigFields.get(configField.name());
if (!configField.equals(visitedConfigField)) {
throw new CodegenException("Contradicting `ConfigField` definitions for `"
+ configField.name()
+ "`; existing: "
+ visitedConfigField
+ ", conflict: "
+ configField);
}
} else {
visitedConfigFields.put(configField.name(), configField);
if (configField.type().equals(ConfigField.Type.MAIN)) {
w.write("$S: config.$L,", scheme.getSchemeId().toString(), configField.name());
}
if (configField.type().equals(ConfigField.Type.MAIN)) {
w.write("$S: config.$L,", scheme.getSchemeId().toString(), configField.name());
}
}
}
Expand Down Expand Up @@ -286,8 +270,10 @@ private void generateHttpAuthSchemeInputConfigInterface(
w.write("httpAuthSchemeProvider?: $L;\n", httpAuthSchemeProviderName);

for (ConfigField configField : configFields.values()) {
configField.docs().ifPresent(docs -> w.writeDocs(() -> w.write("$C", docs)));
w.write("$L?: $T;", configField.name(), configField.inputType());
if (configField.configFieldWriter().isPresent()) {
configField.docs().ifPresent(docs -> w.writeDocs(() -> w.write("$C", docs)));
w.write("$L?: $T;", configField.name(), configField.inputType());
}
}
w.dedent();
w.write("}\n");
Expand Down Expand Up @@ -349,8 +335,10 @@ private void generateHttpAuthSchemeResolvedConfigInterface(
@internal""");
w.write("readonly httpAuthSchemeProvider: $L;\n", httpAuthSchemeProviderName);
for (ConfigField configField : configFields.values()) {
configField.docs().ifPresent(docs -> w.writeDocs(() -> w.write("$C", docs)));
w.write("readonly $L?: $T;", configField.name(), configField.resolvedType());
if (configField.configFieldWriter().isPresent()) {
configField.docs().ifPresent(docs -> w.writeDocs(() -> w.write("$C", docs)));
w.write("readonly $L?: $T;", configField.name(), configField.resolvedType());
}
}
w.dedent();
w.write("}\n");
Expand Down Expand Up @@ -379,33 +367,23 @@ private void generateResolveHttpAuthSchemeConfigFunction(
w.pushState(s);
Map<String, ConfigField> configFields = s.getConfigFields();
Map<Symbol, ResolveConfigFunction> resolveConfigFunctions = s.getResolveConfigFunctions();
Map<Symbol, ResolveConfigFunction> previousResolvedFunctions = resolveConfigFunctions.entrySet().stream()
.filter(e -> e.getValue().previouslyResolved().isPresent())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
w.writeDocs("@internal");
w.writeInline("export const resolveHttpAuthSchemeConfig = <T>(config: T & HttpAuthSchemeInputConfig");
if (!resolveConfigFunctions.isEmpty()) {
w.writeInline(" & ");
Iterator<ResolveConfigFunction> iter = resolveConfigFunctions.values().iterator();
while (iter.hasNext()) {
ResolveConfigFunction entry = iter.next();
w.writeInline("$T", entry.inputConfig());
entry.previouslyResolved().ifPresent(p -> w.writeInline(" & $T", p));
if (iter.hasNext()) {
w.writeInline(" & ");
}
}
}
w.writeInline("): T & HttpAuthSchemeResolvedConfig");
if (!resolveConfigFunctions.isEmpty()) {
if (!previousResolvedFunctions.isEmpty()) {
w.writeInline(" & ");
Iterator<ResolveConfigFunction> iter = resolveConfigFunctions.values().iterator();
Iterator<ResolveConfigFunction> iter = previousResolvedFunctions.values().iterator();
while (iter.hasNext()) {
ResolveConfigFunction entry = iter.next();
w.writeInline("$T", entry.resolvedConfig());
w.writeInline("$T", entry.previouslyResolved().get());
if (iter.hasNext()) {
w.writeInline(" & ");
}
}
}
w.write(" => {");
w.write("): T & HttpAuthSchemeResolvedConfig => {");
w.indent();
w.pushState(ResolveHttpAuthSchemeConfigFunctionConfigFieldsCodeSection.builder()
.service(s.getService())
Expand All @@ -417,7 +395,7 @@ private void generateResolveHttpAuthSchemeConfigFunction(
.build());
w.addDependency(TypeScriptDependency.SMITHY_CORE);
for (ConfigField configField : configFields.values()) {
configField.configFieldWriter().accept(w, configField);
configField.configFieldWriter().ifPresent(cfw -> cfw.accept(w, configField));
}
w.popState();
w.pushState(ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection.builder()
Expand All @@ -428,16 +406,23 @@ private void generateResolveHttpAuthSchemeConfigFunction(
.integrations(s.getIntegrations())
.configFields(configFields)
.build());

Integer i = 0;
String configName = "config";
for (ResolveConfigFunction resolveConfigFunction : resolveConfigFunctions.values()) {
w.write("config = $T(config);", resolveConfigFunction.resolveConfigFunction());
w.write("const config_$L = $T($L);", i, resolveConfigFunction.resolveConfigFunction(), configName);
configName = "config_" + i;
i++;
}
w.openBlock("return {", "} as T & HttpAuthSchemeResolvedConfig;", () -> {
w.write("...config,");
for (ConfigField configField : configFields.values()) {
w.write("return {");
w.indent();
w.write("...$L,", configName);
for (ConfigField configField : configFields.values()) {
if (configField.configFieldWriter().isPresent()) {
w.write("$L,", configField.name());
}
});
}
w.dedent();
w.write("} as T & HttpAuthSchemeResolvedConfig;");
w.popState();
w.dedent();
w.write("};");
Expand Down
Loading