Skip to content

Commit

Permalink
Merge pull request #12 from amzn/invocation_reporting_as_instance
Browse files Browse the repository at this point in the history
Move invocation context to instance variable for clients.
  • Loading branch information
tachyonics authored Mar 7, 2020
2 parents 6c423d6 + 367c052 commit 739e3d2
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extension ServiceModelCodeGenerator {

generateClient(delegate: myClientDelegate)
generateModelOperationsEnum()
generateOperationsReporting()
generateModelOperationClientInput()
generateModelOperationClientOutput()
generateModelOperationHTTPInput()
Expand Down
15 changes: 9 additions & 6 deletions Sources/ServiceModelCodeGeneration/ModelClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import ServiceModelEntities
public protocol ModelClientDelegate {
/// The type of client being generated.
var clientType: ClientType { get }
/// The description of the type being generated.
var typeDescription: String { get }
/// The result type to use for asynchronous functions.
var asyncResultType: AsyncResultType? { get }

func getFileDescription(isGenerator: Bool) -> String

/**
Add any custom file headers to the client file.
Expand All @@ -40,7 +40,8 @@ public protocol ModelClientDelegate {
*/
func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator,
delegate: ModelClientDelegate,
fileBuilder: FileBuilder)
fileBuilder: FileBuilder,
isGenerator: Bool)

/**
Add any common functions to the body of the client type.
Expand All @@ -54,7 +55,8 @@ public protocol ModelClientDelegate {
func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator,
delegate: ModelClientDelegate,
fileBuilder: FileBuilder,
sortedOperations: [(String, OperationDescription)])
sortedOperations: [(String, OperationDescription)],
isGenerator: Bool)

/**
Add the body for an operation to the client type.
Expand All @@ -76,15 +78,16 @@ public protocol ModelClientDelegate {
operationName: String,
operationDescription: OperationDescription,
functionInputType: String?,
functionOutputType: String?)
functionOutputType: String?,
isGenerator: Bool)
}

/// The type of client being generated.
public enum ClientType {
/// A protocol with the specified name
case `protocol`(name: String)
/// A struct with the specified name and conforming to the specified protocol
case `struct`(name: String, conformingProtocolName: String)
case `struct`(name: String, genericParameters: [(typeName: String, conformingTypeName: String?)], conformingProtocolName: String)
}

/**
Expand Down
17 changes: 12 additions & 5 deletions Sources/ServiceModelGenerate/ClientProtocolDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,32 @@ public struct ClientProtocolDelegate: ModelClientDelegate {
self.typeDescription = "Client Protocol for the \(baseName) service."
}

public func getFileDescription(isGenerator: Bool) -> String {
return self.typeDescription
}

public func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator,
delegate: ModelClientDelegate,
fileBuilder: FileBuilder) {
fileBuilder: FileBuilder,
isGenerator: Bool) {
// no custom file header
}

public func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator,
delegate: ModelClientDelegate,
fileBuilder: FileBuilder,
sortedOperations: [(String, OperationDescription)]) {
sortedOperations: [(String, OperationDescription)],
isGenerator: Bool) {
// for each of the operations
for (name, operationDescription) in sortedOperations {
codeGenerator.addOperation(fileBuilder: fileBuilder, name: name,
operationDescription: operationDescription,
delegate: delegate, invokeType: .sync,
forTypeAlias: true)
forTypeAlias: true, isGenerator: isGenerator)
codeGenerator.addOperation(fileBuilder: fileBuilder, name: name,
operationDescription: operationDescription,
delegate: delegate, invokeType: .async,
forTypeAlias: true)
forTypeAlias: true, isGenerator: isGenerator)
}
}

Expand All @@ -72,7 +78,8 @@ public struct ClientProtocolDelegate: ModelClientDelegate {
operationName: String,
operationDescription: OperationDescription,
functionInputType: String?,
functionOutputType: String?) {
functionOutputType: String?,
isGenerator: Bool) {
// nothing to do
}
}
35 changes: 21 additions & 14 deletions Sources/ServiceModelGenerate/MockClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,18 @@ public struct MockClientDelegate: ModelClientDelegate {
+ "returns the `__default` property of its return type."
}

self.clientType = .struct(name: name,
self.clientType = .struct(name: name, genericParameters: [],
conformingProtocolName: "\(baseName)ClientProtocol")
}

public func getFileDescription(isGenerator: Bool) -> String {
return self.typeDescription
}

public func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator,
delegate: ModelClientDelegate,
fileBuilder: FileBuilder) {
fileBuilder: FileBuilder,
isGenerator: Bool) {
// no custom file header
}

Expand All @@ -74,23 +79,24 @@ public struct MockClientDelegate: ModelClientDelegate {
}

let variableName = name.upperToLowerCamelCase
fileBuilder.appendLine("\(variableName)Async: \(protocolTypeName).\(name.startingWithUppercase)AsyncType? = nil,")
fileBuilder.appendLine("\(variableName)Sync: \(protocolTypeName).\(name.startingWithUppercase)SyncType? = nil\(postfix)")
fileBuilder.appendLine("\(variableName)Async: \(name.startingWithUppercase)AsyncType? = nil,")
fileBuilder.appendLine("\(variableName)Sync: \(name.startingWithUppercase)SyncType? = nil\(postfix)")
}

public func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator,
delegate: ModelClientDelegate,
fileBuilder: FileBuilder,
sortedOperations: [(String, OperationDescription)]) {
sortedOperations: [(String, OperationDescription)],
isGenerator: Bool) {
if isThrowingMock {
fileBuilder.appendLine("let error: HTTPClientError")
fileBuilder.appendLine("let error: \(baseName)Error")
}

// for each of the operations
for (name, _) in sortedOperations {
let variableName = name.upperToLowerCamelCase
fileBuilder.appendLine("let \(variableName)AsyncOverride: \(protocolTypeName).\(name.startingWithUppercase)AsyncType?")
fileBuilder.appendLine("let \(variableName)SyncOverride: \(protocolTypeName).\(name.startingWithUppercase)SyncType?")
fileBuilder.appendLine("let \(variableName)AsyncOverride: \(name.startingWithUppercase)AsyncType?")
fileBuilder.appendLine("let \(variableName)SyncOverride: \(name.startingWithUppercase)SyncType?")
}
fileBuilder.appendEmptyLine()

Expand All @@ -104,11 +110,11 @@ public struct MockClientDelegate: ModelClientDelegate {
if isThrowingMock {
if !sortedOperations.isEmpty {
fileBuilder.appendLine("""
public init(error: HTTPClientError,
public init(error: \(baseName)Error,
""")
} else {
fileBuilder.appendLine("""
public init(error: HTTPClientError) {
public init(error: \(baseName)Error) {
""")
}
} else {
Expand Down Expand Up @@ -156,7 +162,8 @@ public struct MockClientDelegate: ModelClientDelegate {
operationName: String,
operationDescription: OperationDescription,
functionInputType: String?,
functionOutputType: String?) {
functionOutputType: String?,
isGenerator: Bool) {
let hasInput = functionInputType != nil

if isThrowingMock {
Expand Down Expand Up @@ -240,10 +247,10 @@ public struct MockClientDelegate: ModelClientDelegate {
switch invokeType {
case .async:
customFunctionPostfix = "Async"
customFunctionParameters = hasInput ? "input, reporting, completion" : "reporting, completion"
customFunctionParameters = hasInput ? "input, completion" : "completion"
case .sync:
customFunctionPostfix = "Sync"
customFunctionParameters = hasInput ? "input, reporting" : "reporting"
customFunctionParameters = hasInput ? "input" : ""
}

fileBuilder.appendLine("""
Expand All @@ -258,7 +265,7 @@ public struct MockClientDelegate: ModelClientDelegate {
switch clientType {
case .protocol(name: let name):
return name
case .struct(name: _, conformingProtocolName: let conformingProtocolName):
case .struct(name: _, genericParameters: _, conformingProtocolName: let conformingProtocolName):
return conformingProtocolName
}
}
Expand Down
Loading

0 comments on commit 739e3d2

Please sign in to comment.