Swiftpack.co - Package - amzn/service-model-swift-code-generate

Build - Master Branch Linux Swift 5.0 Compatible Swift 5.1 Compatible Join the Smoke Server Side community on gitter Apache 2


ServiceModelSwiftCodeGenerate is a foundational code generation library that can be used to generate code based on different service models. This library can be integrated into higher level code generation applications and provides some standard generation functions that can be called.

Getting Started

Step 1: Add the ServiceModelSwiftCodeGenerate dependency

ServiceModelSwiftCodeGenerate uses the Swift Package Manager. To use the framework, add the following dependency to your Package.swift-

dependencies: [
    .package(url: "https://github.com/amzn/service-model-swift-code-generate.git", .upToNextMajor(from: "0.1.0"))

Step 2: Use the library to generate code

The easiest way to integrate ServiceModelSwiftCodeGenerate into a higher level code generation application is to use ServiceModelGenerate.generateFromModel. This function takes a file path to a xml, json or yaml encoded service model, will attempt to parse that file into the required service model type and will then pass that model and a ServiceModelCodeGenerator to the provided function which can call any required generation functions.

extension ServiceModelCodeGenerator {
    func generateFromModel<ModelType: ServiceModel>(serviceModel: ModelType,
                                                    ...) throws {
        let myClientDelegate = ...
        let myModelErrorsDelegate = ...

        generateClient(delegate: myClientDelegate)
        generateModelErrors(delegate: myModelErrorsDelegate)
        generateDefaultInstances(generationType: .internalTypes)

        // Call any custom generation functions as required

public struct MyCodeGeneration {
    static let asyncResultType = AsyncResultType(typeName: "HTTPResult",
                                                 libraryImport: "SmokeHTTPClient")
    public static func generateFromModel<ModelType: ServiceModel>(
        modelFilePath: String,
        modelType: ModelType.Type,
        customizations: CodeGenerationCustomizations,
        applicationDescription: ApplicationDescription,
        modelOverride: ModelOverride?,
        ...) throws {
            func generatorFunction(codeGenerator: ServiceModelCodeGenerator,
                                   serviceModel: ModelType) throws {
                try codeGenerator.generateFromModel(serviceModel: serviceModel, ...)
            try ServiceModelGenerate.generateFromModel(
                    modelFilePath: modelFilePath,
                    customizations: customizations,
                    applicationDescription: applicationDescription,
                    modelOverride: modelOverride,
                    generatorFunction: generatorFunction)

Further Concepts

The ServiceModel Protocol

The ServiceModel protocol represents the parsed service model and provides access to descriptions of the operations, fields and errors. This library provides SwaggerServiceModel that conforms to this protocol and will parse a Swagger 2.0 specification file.

The ModelClientDelegate protocol

The ModelClientDelegate protocol provides customization points for the creation of service clients.

The ModelErrorsDelegate protocol

The ModelErrorsDelegate protocol provides customization points for handling errors returned from an application endpoint conforming to the service model.

The ModelOverride type

The ModelOverride type provides the opportunity to override values from the service model.


This library is licensed under the Apache 2.0 License.


Stars: 5

Used By

Total: 0


Add model override options - 2019-10-09 20:51:50

  1. Add model override options to ignore operations and response headers.
  2. Add model override option to specify the default value of an enumeration.
  3. Add Swift 5.0 and 5.1 CI Tests
  4. Fix compile issues on linux.

Support Swagger syntax for String-keyed maps - 2019-09-27 19:55:21

Support Swagger syntax for String-keyed maps.

Use variable name rather than type as a converting parameter - 2019-04-05 18:52:00

Use variable name rather than type as a parameter when converting to allow multiple parameters of the same type in a structure.

Detect errors that will normalize to the same name - 2019-04-03 23:59:30

Detect errors that will normalize to the same name.

Note: This is a breaking change as it changes the contract of the ModelErrorsDelegate protocol.

Create a stronger ModelErrorsDelegate protocol contract. - 2019-03-29 23:25:03

Create a stronger ModelErrorsDelegate protocol contract to be more flexible and to define specific customisation points.

Note: This is a breaking change as it changes the contract of the ModelErrorsDelegate protocol.

Use spelling consistent with SmokeHTTP - 2019-02-20 03:19:12

Use spelling consistent with SmokeHTTP.

Initial release of ServiceModelSwiftCodeGenerate - 2019-02-19 23:14:42

Initial release of ServiceModelSwiftCodeGenerate.