Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 8 additions & 6 deletions Sources/SwagGenKit/CodeFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public class CodeFormatter {
propertyNames = templateConfig.options["propertyNames"] as? [String: String] ?? [:]
}

var disallowedNames: [String] {
var escapedNames: [String] {
return []
}

var disallowedTypes: [String] {
var escapedTypes: [String] {
return []
}

Expand Down Expand Up @@ -479,12 +479,14 @@ public class CodeFormatter {
("#", "hash"),
("@", "alpha"),
("&", "and"),
("-", "_")
]
var escapedString = string
for (symbol, replacement) in replacements {
escapedString = escapedString.replacingOccurrences(of: symbol, with: replacement)
}
escapedString = String(String.UnicodeScalarView(escapedString.unicodeScalars.filter { CharacterSet.alphanumerics.contains($0) }))
let allowedCharacters = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "_"))
escapedString = String(String.UnicodeScalarView(escapedString.unicodeScalars.filter { allowedCharacters.contains($0) }))

// prepend _ strings starting with numbers
if let firstCharacter = escapedString.unicodeScalars.first,
Expand All @@ -501,7 +503,7 @@ public class CodeFormatter {
// MARK: name and types

func getName(_ name: String) -> String {
var name = name.replacingOccurrences(of: "^-(\\d)", with: "_negative$1", options: .regularExpression)
var name = name.replacingOccurrences(of: "^-(\\d)", with: "negative$1", options: .regularExpression)
name = name.lowerCamelCased()
return escapeName(name)
}
Expand Down Expand Up @@ -529,12 +531,12 @@ public class CodeFormatter {

func escapeName(_ name: String) -> String {
let string = escapeString(name)
return disallowedNames.contains(string) ? getEscapedName(string) : string
return escapedNames.contains(string) ? getEscapedName(string) : string
}

func escapeType(_ type: String) -> String {
let string = escapeString(type)
return disallowedTypes.contains(string) ? getEscapedType(string) : string
return escapedTypes.contains(string) ? getEscapedType(string) : string
}

func getEscapedType(_ type: String) -> String {
Expand Down
130 changes: 67 additions & 63 deletions Sources/SwagGenKit/SwiftFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,73 @@ import Swagger

public class SwiftFormatter: CodeFormatter {

var disallowedKeywords: [String] {
return [
"Type",
"Protocol",
"class",
"struct",
"enum",
"protocol",
"extension",
"return",
"throw",
"throws",
"rethrows",
"public",
"open",
"private",
"fileprivate",
"internal",
"let",
"var",
"where",
"guard",
"associatedtype",
"deinit",
"func",
"import",
"inout",
"operator",
"static",
"subscript",
"typealias",
"case",
"break",
"continue",
"default",
"defer",
"do",
"else",
"fallthrough",
"for",
"if",
"in",
"repeat",
"switch",
"where",
"while",
"as",
"Any",
"AnyObject",
"catch",
"false",
"true",
"is",
"nil",
"super",
"self",
"Self",
]
}
let keywords: [String] = [
"Type",
"Protocol",
"class",
"struct",
"enum",
"protocol",
"extension",
"return",
"throw",
"throws",
"rethrows",
"public",
"open",
"private",
"fileprivate",
"internal",
"let",
"var",
"where",
"guard",
"associatedtype",
"deinit",
"func",
"import",
"inout",
"operator",
"static",
"subscript",
"typealias",
"case",
"break",
"continue",
"default",
"defer",
"do",
"else",
"fallthrough",
"for",
"if",
"in",
"repeat",
"switch",
"where",
"while",
"as",
"Any",
"AnyObject",
"catch",
"false",
"true",
"is",
"nil",
"super",
"self",
"Self",
]

var inbuiltTypes: [String] = [
let inbuiltTypes: [String] = [
"Error",
"Data",
]

override var disallowedNames: [String] { return disallowedKeywords + inbuiltTypes }
override var disallowedTypes: [String] { return disallowedKeywords + inbuiltTypes }
let disallowedNames = ["self"]

override var escapedNames: [String] { return keywords + inbuiltTypes }
override var escapedTypes: [String] { return keywords + inbuiltTypes }

let fixedWidthIntegers: Bool

Expand Down Expand Up @@ -258,6 +258,10 @@ public class SwiftFormatter: CodeFormatter {
}

override func getEscapedName(_ name: String) -> String {
return "`\(name)`"
if disallowedNames.contains(name) {
return "_\(name)"
} else {
return "`\(name)`"
}
}
}
2 changes: 1 addition & 1 deletion Sources/SwagGenKit/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fileprivate let acronymStrings = ["URL"]
extension String {

private func camelCased(seperator: String) -> String {

guard !hasPrefix(seperator) else { return self }
var index = 0
let components = self.components(separatedBy: seperator)
if uppercased() == self {
Expand Down
14 changes: 14 additions & 0 deletions Specs/AppStoreConnect/generated/Swift/AppStoreConnect.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Pod::Spec.new do |s|
s.source_files = '*.swift'
s.name = 'AppStoreConnect'
s.authors = 'Yonas Kolb'
s.summary = 'A generated API'
s.version = '1.2'
s.homepage = 'https://github.com/yonaskolb/SwagGen'
s.source = { :git => 'git@github.com:https://github.com/yonaskolb/SwagGen.git' }
s.ios.deployment_target = '9.0'
s.tvos.deployment_target = '9.0'
s.osx.deployment_target = '10.9'
s.source_files = 'Sources/**/*.swift'
s.dependency 'Alamofire', '~> 4.9.0'
end
2 changes: 2 additions & 0 deletions Specs/AppStoreConnect/generated/Swift/Cartfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

github "Alamofire/Alamofire" ~> 4.9.0
24 changes: 24 additions & 0 deletions Specs/AppStoreConnect/generated/Swift/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
18 changes: 18 additions & 0 deletions Specs/AppStoreConnect/generated/Swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version:4.0

import PackageDescription

let package = Package(
name: "AppStoreConnect",
products: [
.library(name: "AppStoreConnect", targets: ["AppStoreConnect"])
],
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", .exact("4.9.0")),
],
targets: [
.target(name: "AppStoreConnect", dependencies: [
"Alamofire",
], path: "Sources")
]
)
Loading