Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Modular code generator to generate Swift applications from service models.

License

NotificationsYou must be signed in to change notification settings

amzn/service-model-swift-code-generate

Build - main BranchSwift 5.6, 5.7 and 5.8 TestedJoin the Smoke Server Side community on gitterApache 2

ServiceModelSwiftCodeGenerate

ServiceModelSwiftCodeGenerate is a foundational code generation library that can be usedto generate code based on different service models. This library can be integrated intohigher level code generation applications and provides some standard generation functions that canbe called.

Getting Started

Step 1: Add the ServiceModelSwiftCodeGenerate dependency

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

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

Step 2: Use the library to generate code

The easiest way to integrate ServiceModelSwiftCodeGenerate into a higher level code generation application is to useServiceModelGenerate.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 aServiceModelCodeGeneratorto the provided function which can call any required generation functions.

extensionServiceModelCodeGeneratorwhere TargetSupportType:ModelTargetSupport&ClientTargetSupport{func generateFromModel<ModelType:ServiceModel>(serviceModel:ModelType,...)throws{letmyClientDelegate=...letmyModelErrorsDelegate=...letdefaultTraceContextType=DefaultTraceContextType(...)letoperationsReportingType=OperationsReportingType(...)letinvocationReportingType=InvocationReportingType(...)generateClient(delegate: myClientDelegate, fileType:.clientImplementation,             defaultTraceContextType: defaultTraceContextType)generateModelOperationsEnum()generateOperationsReporting(operationsReportingType: operationsReportingType)generateInvocationsReporting(invocationReportingType: invocationReportingType)generateModelOperationClientInput()generateModelOperationClientOutput()generateModelOperationHTTPInput()generateModelOperationHTTPOutput()generateModelStructures()generateModelTypes()generateModelErrors(delegate: myModelErrorsDelegate)generateDefaultInstances(generationType:.internalTypes)        // Call any custom generation functions as required}}publicstructMyCodeGeneration{publicstaticfunc generateFromModel<ModelType:ServiceModel>(        modelFilePath:String,        modelType:ModelType.Type,        customizations:CodeGenerationCustomizations,        applicationDescription:ApplicationDescription,        modelOverride:ModelOverride?,...)throws->ModelType{returntryServiceModelGenerate.generateFromModel(            modelFilePath: modelFilePath,            customizations: customizations,            applicationDescription: applicationDescription,            modelOverride: modelOverride){(codeGenerator, serviceModel)intry codeGenerator.generateFromModel(serviceModel: serviceModel,...)}}}

By default, the code generator will use\(applicationDescription.baseName)Model for the name of the model target and\(applicationDescription.baseName)Client for the name of the client target. You can override these defaults by usingtheModelAndClientTargetSupport type.

public struct MyCodeGeneration {    public static func generateFromModel<ModelType: ServiceModel>(        modelFilePath: String,        modelType: ModelType.Type,        modelTargetName: String, clientTargetName: String,        customizations: CodeGenerationCustomizations,        applicationDescription: ApplicationDescription,        modelOverride: ModelOverride?,        ...) throws     -> ModelType {        let targetSupport = ModelAndClientTargetSupport(modelTargetName: modelTargetName,                                                        clientTargetName: clientTargetName)                                                                return try ServiceModelGenerate.generateFromModel(            modelFilePath: modelFilePath,            customizations: customizations,            applicationDescription: applicationDescription,            targetSupport: targetSupport,            modelOverride: modelOverride) { (codeGenerator, serviceModel) in                try codeGenerator.generateFromModel(serviceModel: serviceModel, ...)        }    }}

Further, if you are generating additional targets, you can use a custom type that provides the name ofadditional targets. This type will have to conform to theModelTargetSupport andClientTargetSupport protocols.

extensionServiceModelCodeGeneratorwhere TargetSupportType:ModelTargetSupport&ClientTargetSupport&MyCustomTargetSupport{func generateFromModel<ModelType:ServiceModel>(serviceModel:ModelType,...)throws{letmyClientDelegate=...letmyModelErrorsDelegate=...generateClient(delegate: myClientDelegate)generateModelOperationsEnum()generateOperationsReporting()generateModelOperationClientInput()generateModelOperationClientOutput()generateModelOperationHTTPInput()generateModelOperationHTTPOutput()generateModelStructures()generateModelTypes()generateModelErrors(delegate: myModelErrorsDelegate)generateDefaultInstances(generationType:.internalTypes)        // Call any custom generation functions as required        // The `targetSupport` attribute will conform to the `MyCustomTargetSupport` protocol.}}publicprotocolMyCustomTargetSupport{varmyCustomTargetName:String{get}}publicstructMyTargetSupport:ModelTargetSupport,ClientTargetSupport,MyCustomTargetSupport{publicletmodelTargetName:StringpublicletclientTargetName:StringpublicletmyCustomTargetName:Stringpublicinit(modelTargetName:String, clientTargetName:String,                    myCustomTargetName:String){self.modelTargetName= modelTargetNameself.clientTargetName= clientTargetNameself.myCustomTargetName= myCustomTargetName}}publicstructMyCodeGeneration{publicstaticfunc generateFromModel<ModelType:ServiceModel>(        modelFilePath:String,        modelType:ModelType.Type,        modelTargetName:String, clientTargetName:String,        myCustomTargetName:String,        customizations:CodeGenerationCustomizations,        applicationDescription:ApplicationDescription,        modelOverride:ModelOverride?,...)throws->ModelType{lettargetSupport=MyTargetSupport(modelTargetName: modelTargetName,                                            clientTargetName: clientTargetName,                                            myCustomTargetName: myCustomTargetName)returntryServiceModelGenerate.generateFromModel(            modelFilePath: modelFilePath,            customizations: customizations,            applicationDescription: applicationDescription,            targetSupport: targetSupport,            modelOverride: modelOverride){(codeGenerator, serviceModel)intry codeGenerator.generateFromModel(serviceModel: serviceModel,...)}}}

Further Concepts

The ServiceModel Protocol

TheServiceModel protocol represents the parsed service model and provides access to descriptions ofthe operations, fields and errors.

The ModelClientDelegate protocol

TheModelClientDelegate protocol provides customization points for the creation of service clients.

The ModelErrorsDelegate protocol

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

The ModelOverride type

TheModelOverride type provides the opportunity to override values from the service model.

License

This library is licensed under the Apache 2.0 License.

About

Modular code generator to generate Swift applications from service models.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors10

Languages


[8]ページ先頭

©2009-2025 Movatter.jp