Skip to content

Commit

Permalink
Merge pull request #36 from amzn/integration_specific_context_type_names
Browse files Browse the repository at this point in the history
Support specifying integration-specific context type names.
  • Loading branch information
tachyonics authored Sep 6, 2022
2 parents 1cd25df + 6e3f4eb commit 7bc9a74
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct SmokeFrameworkCodeGen: Codable {
let baseName: String
let applicationSuffix: String?
let generationType: GenerationType
let integrations: ServiceIntegrations?
let applicationDescription: String?
let modelOverride: ModelOverride?
let httpClientConfiguration: HttpClientConfiguration?
Expand Down
9 changes: 7 additions & 2 deletions Sources/SmokeFrameworkApplicationGenerate/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct Parameters {
var baseFilePath: String
var baseOutputFilePath: String?
var generationType: GenerationType
var integrations: ServiceIntegrations?
var applicationDescription: String?
var modelOverride: ConfigurationProvider<ModelOverride>?
var generateCodeGenConfig: Bool?
Expand Down Expand Up @@ -96,6 +97,7 @@ private func startCodeGeneration(
applicationDescription: String, applicationSuffix: String,
modelFilePath: String, modelFormat: ModelFormat,
generationType: GenerationType,
integrations: ServiceIntegrations?,
asyncAwait: AsyncAwaitCodeGenParameters,
eventLoopFutureOperationHandlers: CodeGenFeatureStatus,
initializationType: InitializationType,
Expand Down Expand Up @@ -135,7 +137,7 @@ private func startCodeGeneration(
return try SmokeFrameworkCodeGeneration.generateFromModel(
modelFilePath: modelFilePath,
modelType: OpenAPIServiceModel.self,
generationType: generationType,
generationType: generationType, integrations: integrations,
customizations: customizations,
applicationDescription: fullApplicationDescription,
operationStubGenerationRule: operationStubGenerationRule,
Expand All @@ -150,7 +152,7 @@ private func startCodeGeneration(
return try SmokeFrameworkCodeGeneration.generateFromModel(
modelFilePath: modelFilePath,
modelType: SwaggerServiceModel.self,
generationType: generationType,
generationType: generationType, integrations: integrations,
customizations: customizations,
applicationDescription: fullApplicationDescription,
operationStubGenerationRule: operationStubGenerationRule,
Expand Down Expand Up @@ -210,6 +212,7 @@ func handleApplication(parameters: Parameters) throws {
applicationSuffix: applicationSuffix,
modelFilePath: parameters.modelFilePath, modelFormat: parameters.modelFormat ?? .swagger,
generationType: parameters.generationType,
integrations: parameters.integrations,
asyncAwait: parameters.asyncAwait ?? .default,
eventLoopFutureOperationHandlers: parameters.eventLoopFutureOperationHandlers ?? .disabled,
initializationType: parameters.initializationType ?? .original,
Expand Down Expand Up @@ -241,6 +244,7 @@ func handleApplication(parameters: Parameters) throws {
baseName: parameters.baseName,
applicationSuffix: parameters.applicationSuffix,
generationType: .serverUpdate,
integrations: parameters.integrations,
applicationDescription: parameters.applicationDescription,
modelOverride: modelOverride,
httpClientConfiguration: httpClientConfigurationOptional,
Expand Down Expand Up @@ -429,6 +433,7 @@ struct SmokeFrameworkApplicationGenerateCommand: ParsableCommand {
baseFilePath: baseFilePath,
baseOutputFilePath: baseOutputFilePath,
generationType: theGenerationType,
integrations: config?.integrations,
applicationDescription: theApplicationDescription,
modelOverride: modelOverride,
generateCodeGenConfig: generateCodeGenConfig ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extension ServiceModelCodeGenerator {
*/
func generateServerHanderSelector(operationStubGenerationRule: OperationStubGenerationRule,
initializationType: InitializationType,
contextTypeName: String,
eventLoopFutureOperationHandlers: CodeGenFeatureStatus) {

let fileBuilder = FileBuilder()
Expand Down Expand Up @@ -69,14 +70,14 @@ extension ServiceModelCodeGenerator {
case .original:
fileBuilder.appendLine("""
public func addOperations<SelectorType: SmokeHTTP1HandlerSelector>(selector: inout SelectorType)
where SelectorType.ContextType == \(baseName)OperationsContext,
where SelectorType.ContextType == \(contextTypeName),
SelectorType.OperationIdentifer == \(baseName)ModelOperations {
""")
case .streamlined:
fileBuilder.appendLine("""
public extension \(baseName)ModelOperations {
static func addToSmokeServer<SelectorType: SmokeHTTP1HandlerSelector>(selector: inout SelectorType)
where SelectorType.ContextType == \(baseName)OperationsContext,
where SelectorType.ContextType == \(contextTypeName),
SelectorType.OperationIdentifer == \(baseName)ModelOperations {
""")
fileBuilder.incIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ extension ServiceModelCodeGenerator {
Generate the stub operations context generator for the generated application.
*/
func generateOperationsContextGenerator(generationType: GenerationType,
contextTypeName: String,
initializationType: InitializationType,
mainAnnotation: CodeGenFeatureStatus,
asyncInitialization: CodeGenFeatureStatus) {
switch initializationType {
case .original:
generateOriginalOperationsContextGenerator(generationType: generationType, mainAnnotation: mainAnnotation)
generateOriginalOperationsContextGenerator(generationType: generationType,
contextTypeName: contextTypeName,
mainAnnotation: mainAnnotation)
case .streamlined:
generateStreamlinedOperationsContextGenerator(generationType: generationType, mainAnnotation: mainAnnotation,
generateStreamlinedOperationsContextGenerator(generationType: generationType,
contextTypeName: contextTypeName,
mainAnnotation: mainAnnotation,
asyncInitialization: asyncInitialization)
}
}

private func generateOriginalOperationsContextGenerator(generationType: GenerationType,
contextTypeName: String,
mainAnnotation: CodeGenFeatureStatus) {

let fileBuilder = FileBuilder()
Expand Down Expand Up @@ -83,7 +89,7 @@ extension ServiceModelCodeGenerator {
fileBuilder.appendLine("""
struct \(baseName)PerInvocationContextInitializer: SmokeServerPerInvocationContextInitializer {
typealias SelectorType =
StandardSmokeHTTP1HandlerSelector<\(baseName)OperationsContext, \(baseName)OperationDelegate,
StandardSmokeHTTP1HandlerSelector<\(contextTypeName), \(baseName)OperationDelegate,
\(baseName)ModelOperations>
let handlerSelector: SelectorType
Expand All @@ -106,8 +112,8 @@ extension ServiceModelCodeGenerator {
On invocation.
*/
public func getInvocationContext(invocationReporting: SmokeServerInvocationReporting<SmokeInvocationTraceContext>)
-> \(baseName)OperationsContext {
return \(baseName)OperationsContext(logger: invocationReporting.logger)
-> \(contextTypeName) {
return \(contextTypeName)(logger: invocationReporting.logger)
}
/**
Expand All @@ -127,6 +133,7 @@ extension ServiceModelCodeGenerator {
}

private func generateStreamlinedOperationsContextGenerator(generationType: GenerationType,
contextTypeName: String,
mainAnnotation: CodeGenFeatureStatus,
asyncInitialization: CodeGenFeatureStatus) {
let fileBuilder = FileBuilder()
Expand Down Expand Up @@ -191,8 +198,8 @@ extension ServiceModelCodeGenerator {
On invocation.
*/
public func getInvocationContext(invocationReporting: SmokeServerInvocationReporting<SmokeInvocationTraceContext>)
-> \(baseName)OperationsContext {
return \(baseName)OperationsContext(logger: invocationReporting.logger)
-> \(contextTypeName) {
return \(contextTypeName)(logger: invocationReporting.logger)
}
/**
Expand All @@ -208,6 +215,7 @@ extension ServiceModelCodeGenerator {
}

public func generateStreamlinedOperationsContextProtocolGenerator(generationType: GenerationType,
contextTypeName: String,
asyncInitialization: CodeGenFeatureStatus) {
let fileBuilder = FileBuilder()
let baseName = applicationDescription.baseName
Expand Down Expand Up @@ -247,7 +255,7 @@ extension ServiceModelCodeGenerator {
Convenience protocol for the initialization of \(baseName)\(applicationSuffix).
*/
public protocol \(baseName)PerInvocationContextInitializerProtocol: StandardJSONSmoke\(baseInitializerInfix)ServerPerInvocationContextInitializer
where ContextType == \(baseName)OperationsContext, OperationIdentifer == \(baseName)ModelOperations {
where ContextType == \(contextTypeName), OperationIdentifer == \(baseName)ModelOperations {
init(eventLoopGroup: EventLoopGroup) \(asyncPrefix)throws
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public enum GenerationType: String, Codable, ExpressibleByArgument {
}
}

public struct ServiceIntegration: Codable {
let contextTypeName: String?
}

public struct ServiceIntegrations: Codable {
let http: ServiceIntegration?
}

public enum InitializationType: String, Codable {
case original = "ORIGINAL"
case streamlined = "STREAMLINED"
Expand Down Expand Up @@ -153,6 +161,7 @@ public struct SmokeFrameworkCodeGeneration {
modelFilePath: String,
modelType: ModelType.Type,
generationType: GenerationType,
integrations: ServiceIntegrations?,
customizations: CodeGenerationCustomizations,
applicationDescription: ApplicationDescription,
operationStubGenerationRule: OperationStubGenerationRule,
Expand All @@ -166,6 +175,7 @@ public struct SmokeFrameworkCodeGeneration {
func generatorFunction(codeGenerator: ServiceModelCodeGenerator,
serviceModel: ModelType) throws {
try codeGenerator.generateFromModel(serviceModel: serviceModel, generationType: generationType,
integrations: integrations,
asyncAwaitClientAPIs: customizations.asyncAwaitAPIs,
eventLoopFutureClientAPIs: customizations.eventLoopFutureClientAPIs,
minimumCompilerSupport: customizations.minimumCompilerSupport,
Expand All @@ -192,6 +202,7 @@ extension ServiceModelCodeGenerator {

func generateFromModel<ModelType: ServiceModel>(serviceModel: ModelType,
generationType: GenerationType,
integrations: ServiceIntegrations?,
asyncAwaitClientAPIs: CodeGenFeatureStatus,
eventLoopFutureClientAPIs: CodeGenFeatureStatus,
minimumCompilerSupport: MinimumCompilerSupport,
Expand Down Expand Up @@ -228,13 +239,17 @@ extension ServiceModelCodeGenerator {
contentType: "application/json", signAllHeaders: false,
defaultInvocationTraceContext: InvocationTraceContextDeclaration(name: "SmokeInvocationTraceContext", importPackage: "SmokeOperationsHTTP1"))
let awsModelErrorsDelegate = SmokeFrameworkModelErrorsDelegate()
let defaultContextTypeName = "\(applicationDescription.baseName)OperationsContext"

if generationType.isNotCodeGenPlugIn {
let contextTypeName = integrations?.http?.contextTypeName ?? defaultContextTypeName

generateServerOperationHandlerStubs(generationType: generationType, operationStubGenerationRule: operationStubGenerationRule,
asyncOperationStubs: asyncOperationStubs)
generateServerApplicationFiles(generationType: generationType, mainAnnotation: mainAnnotation)
generateOperationsContext(generationType: generationType)
generateOperationsContextGenerator(generationType: generationType, initializationType: initializationType,
generateOperationsContextGenerator(generationType: generationType, contextTypeName: contextTypeName,
initializationType: initializationType,
mainAnnotation: mainAnnotation, asyncInitialization: asyncInitialization)
generateOperationTests(generationType: generationType, operationStubGenerationRule: operationStubGenerationRule,
asyncOperationStubs: asyncOperationStubs, testDiscovery: testDiscovery)
Expand Down Expand Up @@ -281,14 +296,18 @@ extension ServiceModelCodeGenerator {
}

if generationType.needsHttp1 {
let contextTypeName = integrations?.http?.contextTypeName ?? defaultContextTypeName

generateModelOperationHTTPInput()
generateModelOperationHTTPOutput()
generateServerHanderSelector(operationStubGenerationRule: operationStubGenerationRule,
initializationType: initializationType,
contextTypeName: contextTypeName,
eventLoopFutureOperationHandlers: eventLoopFutureOperationHandlers)

if initializationType == .streamlined {
generateStreamlinedOperationsContextProtocolGenerator(generationType: generationType,
contextTypeName: contextTypeName,
asyncInitialization: asyncInitialization)
}
} else if generationType.isWithPlugin {
Expand Down

0 comments on commit 7bc9a74

Please sign in to comment.