Skip to content

Commit

Permalink
Remove Stencil dependency and templates from gnostic-swift-generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Sep 5, 2017
1 parent afdd176 commit 123c61d
Show file tree
Hide file tree
Showing 16 changed files with 967 additions and 932 deletions.
2 changes: 0 additions & 2 deletions plugins/gnostic-swift-generator/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

all:
swift build
.build/debug/TemplateEncoder > Sources/gnostic-swift-generator/Templates.swift
swift build
cp .build/debug/gnostic-swift-generator gnostic-swift-generator
rm -f gnostic-swift-client gnostic-swift-server
Expand Down
4 changes: 1 addition & 3 deletions plugins/gnostic-swift-generator/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import PackageDescription
let package = Package(
name: "SwiftOpenAPIPlugin",
targets: [
Target(name: "gnostic-swift-generator", dependencies: ["TemplateEncoder", "Gnostic"]),
Target(name: "TemplateEncoder"),
Target(name: "gnostic-swift-generator", dependencies: ["Gnostic"]),
Target(name: "Gnostic")
],
dependencies: [
.Package(url: "https://github.com/apple/swift-protobuf.git", Version(0,9,904)),
.Package(url: "https://github.com/kylef/Stencil.git", Version(0,8,0))
]
)
43 changes: 0 additions & 43 deletions plugins/gnostic-swift-generator/Sources/TemplateEncoder/main.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import Gnostic

extension ServiceRenderer {

func renderClient() -> String {
var code = CodePrinter()
code.print(header)
code.print()
code.print("// Client code")
code.print()
code.print("import Foundation")
code.print("import Dispatch")
code.print()
code.print("""
enum ClientError: Swift.Error {
case errorWithCode(Int)
}
""")
code.print()
code.print("public class Client {")
code.indent()
code.print("var service : String")
code.print()
code.print("""
public init(service: String) {
self.service = service
}
""")
for serviceMethod in self.methods {
code.print()
code.print("// " + serviceMethod.description + " Asynchronous.")
code.print("public func " + serviceMethod.name + "(" + asyncClientParametersDeclaration(serviceMethod) + ") throws {")
code.indent()
code.print("var path = self.service")
code.print("path = path + \"" + serviceMethod.path + "\"")
for serviceTypeField in parametersTypeFields(serviceMethod) {
if serviceTypeField.position == "path" {
code.print("path = path.replacingOccurrences(of:\"{" +
serviceTypeField.name +
"}\", with:\"\\(" +
serviceTypeField.name +
")\")")
}
}
code.print("guard let url = URL(string:path) else {")
code.indent()
code.print("throw ClientError.errorWithCode(0)")
code.outdent()
code.print("}")
code.print("var request = URLRequest(url:url)")
code.print("request.httpMethod = \"" + serviceMethod.method + "\"")
for serviceTypeField in parametersTypeFields(serviceMethod) {
if serviceTypeField.position == "body" {
code.print("let jsonObject = " + serviceTypeField.name + ".jsonObject()")
code.print("request.httpBody = try JSONSerialization.data(withJSONObject:jsonObject)")
}
}
if hasResponses(serviceMethod) {
code.print("fetch(request) {(data, response, error) in")
code.indent()
code.print("if error != nil {")
code.indent()
code.print("callback(nil, ClientError.errorWithCode(0))")
code.print("return")
code.outdent()
code.print("}")
code.print("guard let httpResponse = response else {")
code.indent()
code.print("callback(nil, ClientError.errorWithCode(0))")
code.print("return")
code.outdent()
code.print("}")
code.print("if httpResponse.statusCode == 200 {")
code.indent()
code.print("if let data = data {")
code.indent()
code.print("let jsonObject = try! JSONSerialization.jsonObject(with:data)")
code.print("if let value = " + serviceMethod.resultTypeName! + "(jsonObject:jsonObject) {")
code.indent()
code.print("callback(value, nil)")
code.print("return")
code.outdent()
code.print("}")
code.outdent()
code.print("}")
code.print("callback(nil, nil)")
code.outdent()
code.print("} else {")
code.indent()
code.print(" callback(nil, ClientError.errorWithCode(httpResponse.statusCode))")
code.outdent()
code.print("}")
code.outdent()
code.print("}")
} else {
code.print("fetch(request) {(data, response, error) in")
code.print("if error != nil {")
code.indent()
code.print("callback(ClientError.errorWithCode(0))")
code.print("return")
code.outdent()
code.print("}")
code.print("guard let httpResponse = response else {")
code.indent()
code.print("callback(ClientError.errorWithCode(0))")
code.print("return")
code.outdent()
code.print("}")
code.print("if httpResponse.statusCode == 200 {")
code.indent()
code.print("callback(nil)")
code.print("} else {")
code.indent()
code.print("callback(ClientError.errorWithCode(httpResponse.statusCode))")
code.outdent()
code.print("}")
code.outdent()
code.print("}")
}
code.outdent()
code.print("}")
code.print()
code.print("// " + serviceMethod.description + " Synchronous.")
code.print("public func " + serviceMethod.name + "(" + syncClientParametersDeclaration(serviceMethod) + ") throws " + syncClientReturnDeclaration(serviceMethod) + " {")
code.indent()
code.print("let sem = DispatchSemaphore(value: 0)")
if hasResponses(serviceMethod) {
code.print("var response : " + serviceMethod.resultTypeName! + "?")
}
code.print("var error : Swift.Error?")
if hasResponses(serviceMethod) {
code.print("try " + serviceMethod.name + "(" + parameterFieldNames(serviceMethod) + ") {r, e in")
code.indent()
code.print("response = r")
} else {
code.print("try " + serviceMethod.name + "(" + parameterFieldNames(serviceMethod) + ") {e in")
code.indent()
}
code.print("error = e")
code.print("sem.signal()")
code.outdent()
code.print("}")
code.print("sem.wait()")
code.print("if let actualError = error {")
code.indent()
code.print("throw actualError")
code.outdent()
code.print("}")
if hasResponses(serviceMethod) {
code.print("if let actualResponse = response {")
code.indent()
code.print("return actualResponse")
code.outdent()
code.print("} else {")
code.indent()
code.print("throw ClientError.errorWithCode(0)")
code.outdent()
code.print("}")
}
code.outdent()
code.print("}")
code.print()
}
code.outdent()
code.print("}")
return code.content
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import Gnostic

extension ServiceRenderer {

func renderFetch() -> String {
var code = CodePrinter()
code.print(header)

code.print("""
import Foundation
import Dispatch
import KituraNet
Expand Down Expand Up @@ -89,23 +99,23 @@ public func fetch(_ urlRequest: URLRequest, callback:@escaping (Data?, HTTPURLRe
let error: Error? = nil // make this mutable when we start using it
guard let method = urlRequest.httpMethod else {
callback (data, urlResponse, error)
return
return
}
guard let url = urlRequest.url else {
callback (data, urlResponse, error)
return
return
}
guard let scheme = url.scheme else {
callback (data, urlResponse, error)
return
return
}
guard let host = url.host else {
callback (data, urlResponse, error)
return
return
}
guard let port = url.port else {
callback (data, urlResponse, error)
return
return
}
let options : [ClientRequest.Options] = [
.method(method),
Expand Down Expand Up @@ -141,3 +151,7 @@ public func fetch(_ urlRequest: URLRequest, callback:@escaping (Data?, HTTPURLRe
}
request.end() // send the request
}
""")
return code.content
}
}
Loading

0 comments on commit 123c61d

Please sign in to comment.