Skip to content

Commit

Permalink
Merge pull request #158 from swift-aws/variable-keywords
Browse files Browse the repository at this point in the history
toSwiftVariableCase() shouldn't be adding `` to variable names in the runtime.
  • Loading branch information
adam-fowler authored Jan 14, 2020
2 parents 977f2c5 + 9251f6a commit fc7f9ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 58 deletions.
2 changes: 0 additions & 2 deletions Sources/AWSSDKSwiftCore/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ extension AWSClient {
default:
body = Body(anyValue: payloadBody)
}
headers.removeValue(forKey: payload.toSwiftVariableCase())
} else {
body = .empty
}
Expand Down Expand Up @@ -463,7 +462,6 @@ extension AWSClient {
default:
body = Body(anyValue: payloadBody)
}
headers.removeValue(forKey: payload.toSwiftVariableCase())
} else {
body = .empty
}
Expand Down
57 changes: 1 addition & 56 deletions Sources/AWSSDKSwiftCore/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,6 @@

import Foundation

let swiftReservedWords: [String] = [
"protocol",
"return",
"operator",
"class",
"struct",
"break",
"continue",
"extension",
"self",
"public",
"private",
"internal",
"where",
"catch",
"try",
"default",
"case",
"static",
"switch",
"if",
"else",
"func",
"enum",
"true",
"false",
"nil",
"in",
"import",
"as",
"is",
"do",
"try",
"type",
"repeat"
]

extension String {
public func lowerFirst() -> String {
return String(self[startIndex]).lowercased() + self[index(after: startIndex)...]
Expand All @@ -61,26 +24,8 @@ extension String {
return self.replacingOccurrences(of: "-", with: "_").camelCased()
}

public func reservedwordEscaped() -> String {
if swiftReservedWords.contains(self.lowercased()) {
return "`\(self)`"
}
return self
}

public func toSwiftVariableCase() -> String {
return toSwiftLabelCase().reservedwordEscaped()
}

public func toSwiftClassCase() -> String {
if self == "Type" {
return "`\(self)`"
}

return self.replacingOccurrences(of: "-", with: "_")
.replacingOccurrences(of: ".", with: "")
.camelCased()
.upperFirst()
return toSwiftLabelCase()
}

public func camelCased(separator: String = "_") -> String {
Expand Down
32 changes: 32 additions & 0 deletions Tests/AWSSDKSwiftCoreTests/AWSClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,38 @@ class AWSClientTests: XCTestCase {
}
}

func testCreateAwsRequestWithKeywordInHeader() {
struct KeywordRequest: AWSShape {
static var _members: [AWSShapeMember] = [
AWSShapeMember(label: "repeat", location: .header(locationName: "repeat"), required: true, type: .string),
]
let `repeat`: String
}
do {
let request = KeywordRequest(repeat: "Repeat")
let awsRequest = try s3Client.createAWSRequest(operation: "Keyword", path: "/", httpMethod: "POST", input: request)
XCTAssertEqual(awsRequest.httpHeaders["repeat"] as? String, "Repeat")
} catch {
XCTFail(error.localizedDescription)
}
}

func testCreateAwsRequestWithKeywordInQuery() {
struct KeywordRequest: AWSShape {
static var _members: [AWSShapeMember] = [
AWSShapeMember(label: "self", location: .querystring(locationName: "self"), required: true, type: .string),
]
let `self`: String
}
do {
let request = KeywordRequest(self: "KeywordRequest")
let awsRequest = try s3Client.createAWSRequest(operation: "Keyword", path: "/", httpMethod: "POST", input: request)
XCTAssertEqual(awsRequest.url, URL(string:"https://s3.amazonaws.com/?self=KeywordRequest")!)
} catch {
XCTFail(error.localizedDescription)
}
}

func testCreateNIORequest() {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
Expand Down

0 comments on commit fc7f9ee

Please sign in to comment.