Swiftpack.co -  amzn/service-model-swift-code-generate as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
amzn/service-model-swift-code-generate
Modular code generator to generate Swift applications from service models.
.package(url: "https://github.com/amzn/service-model-swift-code-generate.git", from: "2.4.0")

Build - Master Branch Swift 5.4 Tested Join the Smoke Server Side community on gitter Apache 2

ServiceModelSwiftCodeGenerate

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)
        generateModelOperationsEnum()
        generateOperationsReporting()
        generateModelOperationClientInput()
        generateModelOperationClientOutput()
        generateModelOperationHTTPInput()
        generateModelOperationHTTPOutput()
        generateModelStructures()
        generateModelTypes()
        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.

License

This library is licensed under the Apache 2.0 License.

GitHub

link
Stars: 10
Last commit: 2 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Make invocation and operations reportings public.
8 weeks ago

This release of ServiceModelSwiftCodeGenerate provides compatibility with Swift 5.1 and Swift 5.2 using SwiftNIO 2.x and SmokeHTTP 2.x.

  1. Make invocation and operations reportings public.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API