Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Commit

Permalink
Swiftlint style corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Feb 1, 2017
1 parent 4e21207 commit 1789574
Show file tree
Hide file tree
Showing 27 changed files with 107 additions and 98 deletions.
9 changes: 4 additions & 5 deletions .swiftlint.yml
@@ -1,14 +1,13 @@
disabled_rules:
- force_cast
- variable_name
- force_try
- opening_brace
- type_name
- variable_name
excluded:
- .build
- .build.tmp
- Packages
cyclomatic_complexity:
error: 50
variable_name:
severity: warning
excluded:
- to

4 changes: 2 additions & 2 deletions Package.swift
Expand Up @@ -5,9 +5,9 @@ let package = Package(
targets: [
Target(name: "CodeGenerator", dependencies: ["Lark", "SchemaParser"]),
Target(name: "SchemaParser", dependencies: ["Lark"]),
Target(name: "lark-generate-client", dependencies: ["SchemaParser", "CodeGenerator"]),
Target(name: "lark-generate-client", dependencies: ["SchemaParser", "CodeGenerator"])
],
dependencies: [
.Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4),
.Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
]
)
9 changes: 3 additions & 6 deletions Sources/CodeGenerator/Generator.swift
Expand Up @@ -40,7 +40,7 @@ extension Type {
}

extension Type: Equatable, Hashable {
public static func ==(lhs: Type, rhs: Type) -> Bool {
public static func == (lhs: Type, rhs: Type) -> Bool {
switch(lhs, rhs) {
case let (.element(lhs), .element(rhs)): return lhs == rhs
case let (.type(lhs), .type(rhs)): return lhs == rhs
Expand Down Expand Up @@ -75,7 +75,6 @@ enum ElementHierarchy {
typealias Graph = CodeGenerator.Graph<Node>
}


public func generate(webService: WebServiceDescription, service: Service) throws -> String {
// Verify service has a SOAP 1.1 port.
guard let port = service.ports.first(where: { if case .soap11 = $0.address { return true } else { return false } }) else {
Expand All @@ -101,7 +100,6 @@ public func generate(webService: WebServiceDescription, service: Service) throws
return SwiftCodeGenerator.generateCode(for: Array(types.values), clients)
}


func generateTypes(inSchema schema: Schema) throws -> Types {
var mapping: TypeMapping = baseTypes.dictionary { (Type.type($0), $1) }
var scope: Set<String> = globalScope
Expand Down Expand Up @@ -226,10 +224,9 @@ let baseTypes: [QualifiedName: Identifier] = [
QualifiedName(uri: NS_XS, localName: "dateTime"): "Date",
QualifiedName(uri: NS_XS, localName: "duration"): "TimeInterval",
QualifiedName(uri: NS_XS, localName: "QName"): "QualifiedName",
QualifiedName(uri: NS_XS, localName: "anyType"): "AnyType",
QualifiedName(uri: NS_XS, localName: "anyType"): "AnyType"
]

let globalScope: Set<String> = Set(baseTypes.values + [
"Lark",
"Lark"
])

12 changes: 9 additions & 3 deletions Sources/CodeGenerator/Models.swift
Expand Up @@ -2,7 +2,7 @@ import Foundation
import SchemaParser
import Lark

// MARK:- SOAP Types
// MARK: - SOAP Types

indirect enum SwiftType {
case identifier(Identifier)
Expand Down Expand Up @@ -31,7 +31,7 @@ indirect enum SwiftType {
}

extension SwiftType: Equatable {
static func ==(lhs: SwiftType, rhs: SwiftType) -> Bool {
static func == (lhs: SwiftType, rhs: SwiftType) -> Bool {
switch (lhs, rhs) {
case let (.identifier(lhs), .identifier(rhs)): return lhs == rhs
case let (.optional(lhs), .optional(rhs)): return lhs == rhs
Expand Down Expand Up @@ -64,7 +64,13 @@ public class SwiftTypeClass: SwiftMetaType {
let nestedTypes: [SwiftMetaType]
let members: [LinesOfCodeConvertible]

init(name: String, base: SwiftTypeClass? = nil, protocols: [String] = [], properties: [SwiftProperty] = [], nestedTypes: [SwiftMetaType] = [], members: [LinesOfCodeConvertible] = []) {
init(
name: String,
base: SwiftTypeClass? = nil,
protocols: [String] = [],
properties: [SwiftProperty] = [],
nestedTypes: [SwiftMetaType] = [],
members: [LinesOfCodeConvertible] = []) {
self.name = name
self.base = base
self.protocols = protocols
Expand Down
2 changes: 1 addition & 1 deletion Sources/CodeGenerator/NameTranslation.swift
Expand Up @@ -4,7 +4,7 @@ internal extension String {
static let caseRegex = {
return try! NSRegularExpression(pattern: "([A-Z]+(?=$|[A-Z][a-z])|[A-Z]?[a-z]+)", options: [])
}()

func toSwiftTypeName() -> String {
let name = capitalizedWithoutInvalidChars.prefixedWithUnderscoreIfNecessary
return name.isEmpty ? "DefaultStructName" : name
Expand Down
22 changes: 16 additions & 6 deletions Sources/CodeGenerator/Schema+toSwift.swift
Expand Up @@ -2,7 +2,7 @@ import Foundation
import SchemaParser
import Lark

// MARK:- SOAP Types
// MARK: - SOAP Types

extension ComplexType {
public func toSwift(name: String? = nil, mapping: TypeMapping, types: Types) -> SwiftTypeClass {
Expand Down Expand Up @@ -36,7 +36,13 @@ extension ComplexType {
return SwiftTypeClass(name: name, base: base, properties: properties, nestedTypes: nestedTypes)
}

func sequenceToSwift(name: Identifier, sequence: Content.Sequence, mapping: TypeMapping, types: Types) -> (properties: [SwiftProperty], nested: [SwiftMetaType]) {
func sequenceToSwift(
name: Identifier,
sequence: Content.Sequence,
mapping: TypeMapping,
types: Types)
-> (properties: [SwiftProperty], nested: [SwiftMetaType])
{
var properties: [SwiftProperty] = []
var nestedTypes: [SwiftMetaType] = []
for element in sequence.elements {
Expand Down Expand Up @@ -97,8 +103,7 @@ extension Element {
}
}


// MARK:- SOAP Client
// MARK: - SOAP Client

extension Service {
public func toSwift(webService: WebServiceDescription, types: Types) -> SwiftClientClass {
Expand All @@ -123,11 +128,16 @@ extension Service {

//TODO: zip operations first; combinding port.operation and binding.operation
let methods = portType.operations
.map { operation in (port: operation, binding: binding.operations.first(where: { $0.name == operation.name })!) }
.map { operation in
(port: operation, binding: binding.operations.first(where: { $0.name == operation.name })!)
}
.map { operation -> ServiceMethod in
let input = message(operation.port.inputMessage)
let output = message(operation.port.outputMessage)
return ServiceMethod(operation: operation.port, input: input, output: output, action: operation.binding.action)
return ServiceMethod(operation: operation.port,
input: input,
output: output,
action: operation.binding.action)
}

return SwiftClientClass(name: name, methods: methods, port: port)
Expand Down
22 changes: 12 additions & 10 deletions Sources/CodeGenerator/SwiftCodeGenerator.swift
@@ -1,3 +1,5 @@
// swiftlint:disable line_length

import SchemaParser

public typealias SwiftCode = String
Expand Down Expand Up @@ -94,7 +96,7 @@ extension SwiftBuiltin {
}
}

// MARK:- SOAP Types
// MARK: - SOAP Types

extension SwiftTypeClass {
public func toLinesOfCode(at indentation: Indentation) -> [LineOfCode] {
Expand Down Expand Up @@ -191,7 +193,8 @@ extension SwiftTypeClass {
case let .optional(.nillable(.identifier(identifier))):
return [
"do {",
" if let node = element.elements(forLocalName: \"\(element.localName)\", uri: \"\(element.uri)\").first, node.attribute(forLocalName: \"nil\", uri: NS_XSI)?.stringValue != \"true\" {",
" if let node = element.elements(forLocalName: \"\(element.localName)\", uri: \"\(element.uri)\").first,",
" node.attribute(forLocalName: \"nil\", uri: NS_XSI)?.stringValue != \"true\" {",
" self.\(property.name) = try \(identifier)(deserialize: node)",
" } else {",
" self.\(property.name) = nil",
Expand Down Expand Up @@ -233,7 +236,7 @@ extension SwiftTypeClass {
case .identifier:
return [
"let \(property.name)Node = element.createChildElement(localName: \"\(element.localName)\", uri: \"\(element.uri)\")",
"try \(property.name).serialize(\(property.name)Node)",
"try \(property.name).serialize(\(property.name)Node)"
]
case .optional(.identifier), .optional(.nillable(.identifier)):
return [
Expand Down Expand Up @@ -351,7 +354,7 @@ extension SwiftEnum {
}

private var sortedCases: [(String, String)] {
return cases.sorted(by: { return $0.key < $1.key } )
return cases.sorted(by: { return $0.key < $1.key })
}

private func linesOfCodeForXMLDeserializer(at indentation: Indentation) -> [LineOfCode] {
Expand Down Expand Up @@ -411,7 +414,7 @@ extension SwiftList {
}
}

// MARK:- SOAP Client
// MARK: - SOAP Client

extension SwiftClientClass {
public func toLinesOfCode(at indentation: Indentation) -> [LineOfCode] {
Expand Down Expand Up @@ -451,7 +454,7 @@ extension ServiceMethod: LinesOfCodeConvertible {
func toLinesOfCode(at indentation: Indentation) -> [LineOfCode] {
return syncCall(at: indentation) + asyncCall(at: indentation)
}

func syncCall(at indentation: Indentation) -> [LineOfCode] {
return indentation.apply(
toFirstLine: "@discardableResult func \(name)(_ parameter: \(input.type)) throws -> \(output.type) {",
Expand All @@ -469,11 +472,11 @@ extension ServiceMethod: LinesOfCodeConvertible {
" let node = envelope.body.elements(forLocalName: \"\(output.element.localName)\", uri: \"\(output.element.uri)\").first!",
" return try \(output.type)(deserialize: node)",
" })",
"return try response.result.resolve()",
"return try response.result.resolve()"
],
andLastLine: "}")
}

func asyncCall(at indentation: Indentation) -> [LineOfCode] {
return indentation.apply(
toFirstLine: "@discardableResult func \(name)Async(_ parameter: \(input.type), completionHandler: @escaping (Result<\(output.type)>) -> Void) -> DataRequest {",
Expand All @@ -491,9 +494,8 @@ extension ServiceMethod: LinesOfCodeConvertible {
" let node = envelope.body.elements(forLocalName: \"\(output.element.localName)\", uri: \"\(output.element.uri)\").first!",
" return try \(output.type)(deserialize: node)",
" },",
" completionHandler: completionHandler)",
" completionHandler: completionHandler)"
],
andLastLine: "}")
}
}

2 changes: 1 addition & 1 deletion Sources/CodeGenerator/WebServiceDescription+verify.swift
Expand Up @@ -147,7 +147,7 @@ extension WebServiceDescription {
}

extension WebServiceDescription.Node: Equatable, Hashable {
static func ==(lhs: WebServiceDescription.Node, rhs: WebServiceDescription.Node) -> Bool {
static func == (lhs: WebServiceDescription.Node, rhs: WebServiceDescription.Node) -> Bool {
switch(lhs, rhs) {
case let (.service(lhs), .service(rhs)): return lhs == rhs
case let (.binding(lhs), .binding(rhs)): return lhs == rhs
Expand Down
1 change: 0 additions & 1 deletion Sources/Lark/Alamofire+Extensions.swift
Expand Up @@ -17,4 +17,3 @@ extension Result {
}
}
}

5 changes: 1 addition & 4 deletions Sources/Lark/Client.swift
Expand Up @@ -118,7 +118,6 @@ open class Client {
}
}


struct Call: URLRequestConvertible {
let endpoint: URL
let action: URL
Expand All @@ -145,14 +144,12 @@ struct Call: URLRequestConvertible {
}
}


extension DataRequest {
@discardableResult
func responseSOAP(
queue: DispatchQueue? = nil,
completionHandler: @escaping (_ response: DataResponse<Envelope>) -> Void)
-> Self
{
-> Self {
return response(queue: queue, responseSerializer: EnvelopeDeserializer(), completionHandler: completionHandler)
}
}
3 changes: 0 additions & 3 deletions Sources/Lark/Envelope.swift
Expand Up @@ -46,7 +46,6 @@ public struct Envelope {
}
}


struct EnvelopeDeserializer: DataResponseSerializerProtocol {
typealias SerializedObject = Envelope
let serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Alamofire.Result<Envelope> = {
Expand All @@ -65,7 +64,6 @@ struct EnvelopeDeserializer: DataResponseSerializerProtocol {
}
}


/// Adopt this protocol to create a custom SOAP header.
///
/// See also `Header` which provides a basic SOAP header.
Expand All @@ -80,7 +78,6 @@ public protocol HeaderSerializable {
func serialize() throws -> XMLElement
}


/// Generic SOAP Header. SOAP provides a flexible mechanism for extending a
/// message in a decentralized and modular way without prior knowledge between
/// the communicating parties. Typical examples of extensions that can be
Expand Down
2 changes: 1 addition & 1 deletion Sources/Lark/QualifiedName.swift
Expand Up @@ -22,7 +22,7 @@ extension QualifiedName: CustomDebugStringConvertible {
}

extension QualifiedName: Equatable {
public static func ==(lhs: QualifiedName, rhs: QualifiedName) -> Bool {
public static func == (lhs: QualifiedName, rhs: QualifiedName) -> Bool {
return lhs.uri == rhs.uri && lhs.localName == rhs.localName
}
}
Expand Down
19 changes: 8 additions & 11 deletions Sources/Lark/Serialization.swift
Expand Up @@ -25,8 +25,8 @@ public protocol StringSerializable {
func serialize() throws -> String
}

//MARK: - Base type serialization
//MARK: Signed integers
// MARK: - Base type serialization
// MARK: Signed integers

extension Int8: XMLDeserializable, XMLSerializable {
public init(deserialize node: XMLElement) throws {
Expand Down Expand Up @@ -75,7 +75,7 @@ extension Int64: XMLDeserializable, XMLSerializable {
}
}

//MARK: Unsigned integers
// MARK: Unsigned integers

extension UInt8: XMLDeserializable, XMLSerializable {
public init(deserialize node: XMLElement) throws {
Expand Down Expand Up @@ -124,7 +124,7 @@ extension UInt64: XMLDeserializable, XMLSerializable {
}
}

//MARK: Numeric types
// MARK: Numeric types

extension Bool: XMLDeserializable, XMLSerializable {
public init(deserialize node: XMLElement) throws {
Expand Down Expand Up @@ -187,7 +187,7 @@ extension Decimal: XMLDeserializable, XMLSerializable {
}
}

//MARK: Other types
// MARK: Other types

extension String: XMLDeserializable, XMLSerializable {
public init(deserialize node: XMLElement) throws {
Expand Down Expand Up @@ -234,24 +234,21 @@ extension Date: XMLDeserializable, XMLSerializable {
return formatter
}()

static let fallbackDateFormatters: [DateFormatter] = [
{
static let fallbackDateFormatters: [DateFormatter] = [ {
// formatter with milliseconds
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(identifier: "UTC")!
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
return formatter
}(),
{
}(), {
// formatter without timezone identifier
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(identifier: "UTC")!
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
return formatter
}(),
{
}(), {
// formatter with milliseconds and without timezone identifier
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Lark/StringSerializableList.swift
Expand Up @@ -53,7 +53,7 @@ extension StringSerializableList {

extension StringSerializableList where Element: Equatable {
/// Returns `true` if these lists contain the same elements.
public static func ==(lhs: Self, rhs: Self) -> Bool {
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs._contents == rhs._contents
}
}

0 comments on commit 1789574

Please sign in to comment.