Skip to content

Commit

Permalink
Merge pull request #68 from amzn/model_overrides_type
Browse files Browse the repository at this point in the history
Associate a OverridesType with a ServiceModel type.
  • Loading branch information
tachyonics authored Nov 18, 2022
2 parents 42c354d + a098426 commit 3cacd62
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 58 deletions.
9 changes: 5 additions & 4 deletions Sources/ServiceModelCodeGeneration/ModelClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public enum ClientEntityType {
from the Service Model.
*/
public protocol ModelClientDelegate {
associatedtype ModelType: ServiceModel
associatedtype TargetSupportType

/// The type of client being generated.
Expand All @@ -93,7 +94,7 @@ public protocol ModelClientDelegate {
- delegate: the delegate being used.
- fileBuilder: The FileBuilder to output to.
*/
func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
fileType: ClientFileType)
Expand All @@ -106,7 +107,7 @@ public protocol ModelClientDelegate {
- delegate: the delegate being used.
- fileBuilder: The FileBuilder to output to.
*/
func addTypeDescription(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
func addTypeDescription(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
entityType: ClientEntityType)
Expand All @@ -120,7 +121,7 @@ public protocol ModelClientDelegate {
- fileBuilder: The FileBuilder to output to.
- sortedOperations: A list of sorted operations from the current model.
*/
func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
sortedOperations: [(String, OperationDescription)],
Expand All @@ -139,7 +140,7 @@ public protocol ModelClientDelegate {
- functionInputType: the input type to the operation.
- functionOutputType: the output type for the operation.
*/
func addOperationBody(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
func addOperationBody(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
invokeType: InvokeType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public struct ModelAndClientTargetSupport: ModelTargetSupport, ClientTargetSuppo
}

/// A code generator that uses a Service Model
public struct ServiceModelCodeGenerator<TargetSupportType> {
public let model: ServiceModel
public struct ServiceModelCodeGenerator<ModelType: ServiceModel, TargetSupportType> {
public let model: ModelType
public let applicationDescription: ApplicationDescription
public let customizations: CodeGenerationCustomizations
public let modelOverride: ModelOverride?
public let modelOverride: ModelOverride<ModelType.OverridesType>?
public let targetSupport: TargetSupportType

/**
Expand All @@ -56,10 +56,10 @@ public struct ServiceModelCodeGenerator<TargetSupportType> {
- baseFilePath: The file path of the generated package.
- applicationDescription: A description of the application being created.
*/
public init(model: ServiceModel,
public init(model: ModelType,
applicationDescription: ApplicationDescription,
customizations: CodeGenerationCustomizations,
modelOverride: ModelOverride?,
modelOverride: ModelOverride<ModelType.OverridesType>?,
targetSupport: TargetSupportType) {
self.model = model
self.applicationDescription = applicationDescription
Expand Down
12 changes: 10 additions & 2 deletions Sources/ServiceModelEntities/ModelOverride.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import Foundation

public struct NoModelTypeOverrides: Codable {

}

/// Data model for the ModelOverride input file
public struct ModelOverride: Codable {
public struct ModelOverride<ModelTypeOverridesType: Codable>: Codable {
/// attributes of these types should match the original case.
public let matchCase: Set<String>?
/// attributes of these types should match the original case.
Expand Down Expand Up @@ -50,6 +54,8 @@ public struct ModelOverride: Codable {
public let ignoreRequestHeaders: Set<String>?
/// overrides the default value used for an enumeration
public let defaultEnumerationValueOverride: [String: String]?
/// overrides that are specific to a particular model type
public let modelTypeOverrides: ModelTypeOverridesType?

public init(matchCase: Set<String>? = nil,
enumerations: EnumerationNaming? = nil,
Expand All @@ -64,7 +70,8 @@ public struct ModelOverride: Codable {
ignoreOperations: Set<String>? = nil,
ignoreResponseHeaders: Set<String>? = nil,
ignoreRequestHeaders: Set<String>? = nil,
defaultEnumerationValueOverride: [String: String]? = nil) {
defaultEnumerationValueOverride: [String: String]? = nil,
modelTypeOverrides: ModelTypeOverridesType? = nil) {
self.matchCase = matchCase
self.enumerations = enumerations
self.fieldRawTypeOverride = fieldRawTypeOverride
Expand All @@ -79,6 +86,7 @@ public struct ModelOverride: Codable {
self.ignoreResponseHeaders = ignoreResponseHeaders
self.ignoreRequestHeaders = ignoreRequestHeaders
self.defaultEnumerationValueOverride = defaultEnumerationValueOverride
self.modelTypeOverrides = modelTypeOverrides
}

public func getCodingKeyOverride(attributeName: String, inType: String?) -> String? {
Expand Down
8 changes: 5 additions & 3 deletions Sources/ServiceModelEntities/ServiceModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public enum ServiceModelError: Error {
Protocol for a Service Model description.
*/
public protocol ServiceModel {
associatedtype OverridesType: Codable = NoModelTypeOverrides

var serviceInformation: ServiceInformation? { get }
var serviceDescriptions: [String: ServiceDescription] { get }
var structureDescriptions: [String: StructureDescription] { get }
Expand All @@ -44,20 +46,20 @@ public protocol ServiceModel {
Initialize an instance of this ServiceModel type from a data instance
that represents that type.
*/
static func create(data: Data, modelFormat: ModelFormat, modelOverride: ModelOverride?) throws -> Self
static func create(data: Data, modelFormat: ModelFormat, modelOverride: ModelOverride<OverridesType>?) throws -> Self

/**
Initialize an instance of this ServiceModel type from a data instance
that represents that type.
*/
static func create(dataList: [Data], modelFormat: ModelFormat, modelOverride: ModelOverride?) throws -> Self
static func create(dataList: [Data], modelFormat: ModelFormat, modelOverride: ModelOverride<OverridesType>?) throws -> Self
}

public extension ServiceModel {
// Provide default value for backwards compatibility
var serviceInformation: ServiceInformation? { nil }

static func create(dataList: [Data], modelFormat: ModelFormat, modelOverride: ModelOverride?) throws -> Self {
static func create(dataList: [Data], modelFormat: ModelFormat, modelOverride: ModelOverride<OverridesType>?) throws -> Self {
throw ServiceModelError.notImplementedException
}
}
2 changes: 1 addition & 1 deletion Sources/ServiceModelEntities/String+nameConversions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public extension String {
The normalized name for a type; either a specified type mapping
from the provided service model or this string startingWithUppercase.
*/
func getNormalizedTypeName(forModel model: ServiceModel) -> String {
func getNormalizedTypeName<ModelType: ServiceModel>(forModel model: ModelType) -> String {
// if there is a mapping for this name
if let mappedName = model.typeMappings[self] {
return mappedName
Expand Down
10 changes: 5 additions & 5 deletions Sources/ServiceModelGenerate/ClientProtocolDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import ServiceModelEntities
A ModelClientDelegate that can be used to generate a
Client protocol from a Service Model.
*/
public struct ClientProtocolDelegate<TargetSupportType>: ModelClientDelegate
public struct ClientProtocolDelegate<ModelType: ServiceModel, TargetSupportType>: ModelClientDelegate
where TargetSupportType: ModelTargetSupport & ClientTargetSupport {
public let clientType: ClientType
public let baseName: String
Expand All @@ -50,21 +50,21 @@ where TargetSupportType: ModelTargetSupport & ClientTargetSupport {
self.minimumCompilerSupport = minimumCompilerSupport
}

public func addTypeDescription(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addTypeDescription(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
entityType: ClientEntityType) {
fileBuilder.appendLine(self.typeDescription)
}

public func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
fileType: ClientFileType) {
// no custom file header
}

public func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
sortedOperations: [(String, OperationDescription)],
Expand Down Expand Up @@ -125,7 +125,7 @@ where TargetSupportType: ModelTargetSupport & ClientTargetSupport {
}
}

public func addOperationBody(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addOperationBody(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder, invokeType: InvokeType,
operationName: String,
Expand Down
22 changes: 11 additions & 11 deletions Sources/ServiceModelGenerate/MockClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import ServiceModelEntities
A ModelClientDelegate that can be used to generate a
mock or throwing test client from a Service Model.
*/
public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
public struct MockClientDelegate<ModelType: ServiceModel, TargetSupportType>: ModelClientDelegate {
public let baseName: String
public let isThrowingMock: Bool
public let clientType: ClientType
Expand Down Expand Up @@ -77,7 +77,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
conformingProtocolNames: conformingProtocolNames)
}

public func addTypeDescription(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addTypeDescription(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
entityType: ClientEntityType) {
Expand Down Expand Up @@ -116,7 +116,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
""")
}

public func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addCustomFileHeader(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
fileType: ClientFileType) {
Expand All @@ -143,7 +143,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
fileBuilder.appendLine("\(variableName): \(name.startingWithUppercase)FunctionType? = nil\(postfix)")
}

public func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addCommonFunctions(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
sortedOperations: [(String, OperationDescription)],
Expand Down Expand Up @@ -265,7 +265,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
""")
}

public func addOperationBody(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
public func addOperationBody(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
delegate: Self,
fileBuilder: FileBuilder,
invokeType: InvokeType,
Expand All @@ -291,7 +291,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
}
}

private func delegateMockImplementationCall(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
private func delegateMockImplementationCall(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
functionPrefix: String, functionInfix: String,
fileBuilder: FileBuilder, hasInput: Bool,
functionOutputType: String?, operationName: String) {
Expand Down Expand Up @@ -338,7 +338,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
}
}

private func delegateAsyncOnlyMockImplementationCall(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
private func delegateAsyncOnlyMockImplementationCall(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
fileBuilder: FileBuilder, hasInput: Bool,
functionOutputType: String?, operationName: String) {
let variableName = operationName.upperToLowerCamelCase
Expand All @@ -363,7 +363,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
}
}

private func addMockClientOperationBody(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
private func addMockClientOperationBody(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
fileBuilder: FileBuilder, hasInput: Bool,
functionOutputType: String?, invokeType: InvokeType,
protocolTypeName: String, operationName: String) {
Expand Down Expand Up @@ -425,7 +425,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
fileBuilder.appendLine("}", preDec: true)
}

private func delegateMockThrowingImplementationCall(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
private func delegateMockThrowingImplementationCall(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
functionPrefix: String, functionInfix: String,
fileBuilder: FileBuilder, hasInput: Bool,
functionOutputType: String?, operationName: String) {
Expand Down Expand Up @@ -472,7 +472,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
}
}

private func delegateAsyncOnlyMockThrowingImplementationCall(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
private func delegateAsyncOnlyMockThrowingImplementationCall(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
fileBuilder: FileBuilder, hasInput: Bool,
functionOutputType: String?, operationName: String) {
let variableName = operationName.upperToLowerCamelCase
Expand All @@ -497,7 +497,7 @@ public struct MockClientDelegate<TargetSupportType>: ModelClientDelegate {
}
}

private func addThrowingClientOperationBody(codeGenerator: ServiceModelCodeGenerator<TargetSupportType>,
private func addThrowingClientOperationBody(codeGenerator: ServiceModelCodeGenerator<ModelType, TargetSupportType>,
fileBuilder: FileBuilder, hasInput: Bool, functionOutputType: String?,
invokeType: InvokeType, operationName: String) {
let functionPrefix: String
Expand Down
Loading

0 comments on commit 3cacd62

Please sign in to comment.