-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate auth params and bindings (#453)
- Loading branch information
Showing
12 changed files
with
427 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/AuthGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.go.codegen.auth; | ||
|
||
import software.amazon.smithy.codegen.core.CodegenException; | ||
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; | ||
|
||
/** | ||
* Entry point into smithy client auth generation. | ||
*/ | ||
public class AuthGenerator { | ||
private final ProtocolGenerator.GenerationContext context; | ||
|
||
public AuthGenerator(ProtocolGenerator.GenerationContext context) { | ||
this.context = context; | ||
} | ||
|
||
public void generate() { | ||
if (context.getWriter().isEmpty()) { | ||
throw new CodegenException("writer is required"); | ||
} | ||
|
||
context.getWriter().get() | ||
.write("$W", new AuthParametersGenerator(context).generate()) | ||
.write("") | ||
.write("$W", new AuthParametersResolverGenerator(context).generate()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/AuthParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.go.codegen.auth; | ||
|
||
import software.amazon.smithy.codegen.core.Symbol; | ||
import software.amazon.smithy.go.codegen.GoUniverseTypes; | ||
|
||
public record AuthParameter(String name, String docs, Symbol type) { | ||
public static final AuthParameter OPERATION = new AuthParameter( | ||
"Operation", | ||
"The name of the operation being invoked.", | ||
GoUniverseTypes.String | ||
); | ||
|
||
public static final AuthParameter REGION = new AuthParameter( | ||
"Region", | ||
"The region in which the operation is being invoked.", | ||
GoUniverseTypes.String | ||
); | ||
} |
99 changes: 99 additions & 0 deletions
99
...codegen/src/main/java/software/amazon/smithy/go/codegen/auth/AuthParametersGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.go.codegen.auth; | ||
|
||
import static software.amazon.smithy.go.codegen.GoWriter.goDocTemplate; | ||
import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; | ||
|
||
import java.util.ArrayList; | ||
import software.amazon.smithy.codegen.core.Symbol; | ||
import software.amazon.smithy.go.codegen.GoWriter; | ||
import software.amazon.smithy.go.codegen.SymbolUtils; | ||
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; | ||
import software.amazon.smithy.utils.ListUtils; | ||
import software.amazon.smithy.utils.MapUtils; | ||
|
||
/** | ||
* Generates auth scheme resolver parameters. | ||
* By default, the only field that exists universally is the name of the operation being invoked. Services that use | ||
* SigV4[A] will also have a field for the region. | ||
* Additional parameters can be loaded via GoIntegration. | ||
*/ | ||
public class AuthParametersGenerator { | ||
public static final String STRUCT_NAME = "AuthResolverParameters"; | ||
|
||
public static final Symbol STRUCT_SYMBOL = SymbolUtils.createPointableSymbolBuilder(STRUCT_NAME).build(); | ||
|
||
private final ProtocolGenerator.GenerationContext context; | ||
|
||
private final ArrayList<AuthParameter> fields = new ArrayList<>( | ||
ListUtils.of(AuthParameter.OPERATION) | ||
); | ||
|
||
public AuthParametersGenerator(ProtocolGenerator.GenerationContext context) { | ||
this.context = context; | ||
} | ||
|
||
public GoWriter.Writable generate() { | ||
loadFields(); | ||
|
||
return goTemplate( | ||
""" | ||
$doc:W | ||
type $name:L struct { | ||
$fields:W | ||
} | ||
""", | ||
MapUtils.of( | ||
"doc", generateDocs(), | ||
"name", STRUCT_NAME, | ||
"fields", generateFields() | ||
) | ||
); | ||
} | ||
|
||
private GoWriter.Writable generateDocs() { | ||
return goDocTemplate( | ||
"$name:L contains the set of inputs necessary for auth scheme resolution.", | ||
MapUtils.of("name", STRUCT_NAME) | ||
); | ||
} | ||
|
||
private GoWriter.Writable generateFields() { | ||
return (writer) -> { | ||
for (var field: fields) { | ||
writer.write(""" | ||
$W | ||
$L $P | ||
""", | ||
goDocTemplate(field.docs()), | ||
field.name(), | ||
field.type() | ||
); | ||
} | ||
}; | ||
} | ||
|
||
private void loadFields() { | ||
for (var integration: context.getIntegrations()) { | ||
var plugins = integration.getClientPlugins().stream().filter(it -> | ||
it.matchesService(context.getModel(), context.getService())).toList(); | ||
for (var plugin: plugins) { | ||
fields.addAll(plugin.getAuthParameters()); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/AuthParametersResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.go.codegen.auth; | ||
|
||
import software.amazon.smithy.codegen.core.Symbol; | ||
|
||
public record AuthParametersResolver(Symbol resolver) { } |
96 changes: 96 additions & 0 deletions
96
...src/main/java/software/amazon/smithy/go/codegen/auth/AuthParametersResolverGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.go.codegen.auth; | ||
|
||
import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; | ||
|
||
import java.util.ArrayList; | ||
import software.amazon.smithy.codegen.core.Symbol; | ||
import software.amazon.smithy.go.codegen.GoWriter; | ||
import software.amazon.smithy.go.codegen.SymbolUtils; | ||
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; | ||
import software.amazon.smithy.utils.MapUtils; | ||
|
||
/** | ||
* Generates a single method which binds auth scheme resolver parameters from operation input and client options. | ||
* The only value bound by default is the operation name. Generators must load additional bindings through | ||
* GoIntegration. | ||
*/ | ||
public class AuthParametersResolverGenerator { | ||
public static final String FUNC_NAME = "bindAuthResolverParams"; | ||
|
||
public static final Symbol FUNC_SYMBOL = SymbolUtils.createValueSymbolBuilder(FUNC_NAME).build(); | ||
|
||
private final ProtocolGenerator.GenerationContext context; | ||
|
||
private final ArrayList<AuthParametersResolver> resolvers = new ArrayList<>(); | ||
|
||
public AuthParametersResolverGenerator(ProtocolGenerator.GenerationContext context) { | ||
this.context = context; | ||
} | ||
|
||
public GoWriter.Writable generate() { | ||
loadResolvers(); | ||
|
||
return goTemplate(""" | ||
$operationNamer:W | ||
func $name:L(input interface{}, options Options) $params:P { | ||
params := &$params:T{ | ||
Operation: input.(operationNamer).operationName(), | ||
} | ||
$bindings:W | ||
return params | ||
} | ||
""", | ||
MapUtils.of( | ||
"name", FUNC_NAME, | ||
"operationNamer", generateOperationNamer(), | ||
"params", AuthParametersGenerator.STRUCT_SYMBOL, | ||
"bindings", generateResolvers() | ||
)); | ||
} | ||
|
||
private GoWriter.Writable generateOperationNamer() { | ||
return (writer) -> { | ||
writer.write(""" | ||
type operationNamer interface { | ||
operationName() string | ||
} | ||
"""); | ||
}; | ||
} | ||
|
||
private GoWriter.Writable generateResolvers() { | ||
return (writer) -> { | ||
for (var resolver: resolvers) { | ||
writer.write("$T(params, input, options)", resolver.resolver()); | ||
} | ||
}; | ||
} | ||
|
||
private void loadResolvers() { | ||
for (var integration: context.getIntegrations()) { | ||
var plugins = integration.getClientPlugins().stream().filter(it -> | ||
it.matchesService(context.getModel(), context.getService())).toList(); | ||
for (var plugin: plugins) { | ||
resolvers.addAll(plugin.getAuthParameterResolvers()); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.