Skip to content

Commit fd54087

Browse files
authored
fix: Surrounding underscores are now correctly supported
* Camel case now preserves leading and trailing underscores to properly support the GraphQL spec * Now compiles with updated Starscream APIs
1 parent 1fcd18c commit fd54087

File tree

10 files changed

+44
-30
lines changed

10 files changed

+44
-30
lines changed

Sources/GraphQLWebSocket/Client.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public class GraphQLWebSocket: WebSocketDelegate {
7474
case connecting
7575

7676
/// WebSocket has opened.
77-
case opened(socket: WebSocket)
78-
77+
case opened(socket: WebSocketClient)
78+
7979
/// Open WebSocket connection has been acknowledged
8080
case acknowledged(payload: [String: AnyCodable]?)
8181

@@ -143,7 +143,7 @@ public class GraphQLWebSocket: WebSocketDelegate {
143143

144144
// MARK: - Internals
145145

146-
public func didReceive(event: WebSocketEvent, client: WebSocket) {
146+
public func didReceive(event: WebSocketEvent, client: WebSocketClient) {
147147
self.config.logger.debug("Received a new message from the server!")
148148

149149
switch event {
@@ -219,6 +219,8 @@ public class GraphQLWebSocket: WebSocketDelegate {
219219
self.close(code: closeCode)
220220
break
221221

222+
case .peerClosed:
223+
self.close(code: 1006)
222224
}
223225
}
224226

Sources/SwiftGraphQL/Document/Field.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public enum GraphQLField {
5252
switch self {
5353
case let .leaf(name, parent, arguments),
5454
let .composite(name, parent, _, arguments, _):
55-
return "\(name.camelCase)\(parent.camelCase)_\(arguments.hash)"
55+
return "\(name.camelCasePreservingSurroundingUnderscores)\(parent.camelCasePreservingSurroundingUnderscores)_\(arguments.hash)"
5656
case .fragment:
5757
return nil
5858
}

Sources/SwiftGraphQLCodegen/Generator/Codable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private extension Collection where Element == ObjectTypeRef {
2727
/// Returns an enumerator that we use to decode typename field.
2828
func typenamesEnum() -> String {
2929
let types = self
30-
.map { "case \($0.name.camelCase.normalize) = \"\($0.name)\"" }
30+
.map { "case \($0.name.camelCasePreservingSurroundingUnderscores.normalize) = \"\($0.name)\"" }
3131
.joined(separator: "\n")
3232

3333
return """

Sources/SwiftGraphQLCodegen/Generator/Enum.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extension EnumType {
5858
/// Mock value declaration.
5959
private var mock: String {
6060
let value = self.enumValues.first!
61-
return "public static var mockValue = Self.\(value.name.camelCase.normalize)"
61+
return "public static var mockValue = Self.\(value.name.camelCasePreservingSurroundingUnderscores.normalize)"
6262
}
6363
}
6464

@@ -70,7 +70,7 @@ extension EnumValue {
7070
fileprivate var declaration: String {
7171
"""
7272
\(docs)
73-
case \(name.camelCase.normalize) = "\(name)"
73+
case \(name.camelCasePreservingSurroundingUnderscores.normalize) = "\(name)"
7474
"""
7575
}
7676

Sources/SwiftGraphQLCodegen/Generator/Field.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ extension Field {
9898
}
9999

100100
private var fName: String {
101-
name.camelCase.normalize
101+
name.camelCasePreservingSurroundingUnderscores.normalize
102102
}
103103

104104
private func fParameters(context: Context) throws -> String {
@@ -148,7 +148,7 @@ private extension Collection where Element == InputValue {
148148
/// Returns a one-to-one argument mapping.
149149
func arguments(field: Field, context: Context) -> String {
150150
let args = self
151-
.map { $0.name.camelCase }.map { "\($0): \($0.normalize)" }
151+
.map { $0.name.camelCasePreservingSurroundingUnderscores }.map { "\($0): \($0.normalize)" }
152152
.joined(separator: ", ")
153153

154154
switch field.type.namedType {
@@ -166,7 +166,7 @@ private extension Collection where Element == InputValue {
166166
extension InputValue {
167167
/// Generates a function parameter for this input value.
168168
fileprivate func parameter(context: Context) throws -> String {
169-
"\(name.camelCase.normalize): \(try type.type(scalars: context.scalars)) \(self.default)"
169+
"\(name.camelCasePreservingSurroundingUnderscores.normalize): \(try type.type(scalars: context.scalars)) \(self.default)"
170170
}
171171

172172
/// Returns the default value of the parameter.
@@ -224,7 +224,7 @@ private extension Collection where Element == InputValue {
224224
private extension InputValue {
225225
/// Returns a SwiftGraphQL Argument definition for a given input value.
226226
var argument: String {
227-
#"Argument(name: "\#(name)", type: "\#(type.argument)", value: \#(name.camelCase.normalize))"#
227+
#"Argument(name: "\#(name)", type: "\#(type.argument)", value: \#(name.camelCasePreservingSurroundingUnderscores.normalize))"#
228228
}
229229
}
230230

Sources/SwiftGraphQLCodegen/Generator/Fragments.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ extension Collection where Element == ObjectTypeRef {
4848

4949
/// Type used to
5050
private var mock: String {
51-
self.first!.namedType.name.camelCase
51+
self.first!.namedType.name.camelCasePreservingSurroundingUnderscores
5252
}
5353
}
5454

5555
private extension ObjectTypeRef {
5656
/// Returns a parameter definition for a given type reference.
5757
var parameter: String {
58-
"\(namedType.name.camelCase): Selection<T, Objects.\(namedType.name.pascalCase)>"
58+
"\(namedType.name.camelCasePreservingSurroundingUnderscores): Selection<T, Objects.\(namedType.name.pascalCase)>"
5959
}
6060

6161
/// Returns a SwiftGraphQL Fragment selection.
6262
func fragment(interface: String) -> String {
63-
#"GraphQLField.fragment(type: "\#(namedType.name)", interface: "\#(interface)", selection: \#(namedType.name.camelCase).__selection())"#
63+
#"GraphQLField.fragment(type: "\#(namedType.name)", interface: "\#(interface)", selection: \#(namedType.name.camelCasePreservingSurroundingUnderscores).__selection())"#
6464
}
6565

6666
/// Returns a decoder for a fragment.
@@ -70,7 +70,7 @@ private extension ObjectTypeRef {
7070
let name = namedType.name
7171
return """
7272
case "\(name)":
73-
return try \(name.camelCase).__decode(data: data)
73+
return try \(name.camelCasePreservingSurroundingUnderscores).__decode(data: data)
7474
"""
7575
}
7676
}

Sources/SwiftGraphQLCodegen/Generator/InputObject.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ extension InputValue {
4545
fileprivate func declaration(context: Context) throws -> String {
4646
"""
4747
\(docs)
48-
public var \(name.camelCase.normalize): \(try type.type(scalars: context.scalars))
48+
public var \(name.camelCasePreservingSurroundingUnderscores.normalize): \(try type.type(scalars: context.scalars))
4949
"""
5050
}
5151

5252
fileprivate func initDeclaration(context: Context, isLast: Bool) throws -> String {
5353
"""
54-
\(name.camelCase.normalize): \(try type.type(scalars: context.scalars))\(self.default)
54+
\(name.camelCasePreservingSurroundingUnderscores.normalize): \(try type.type(scalars: context.scalars))\(self.default)
5555
"""
5656
}
5757

5858
fileprivate func initFields(context: Context) throws -> String {
5959
"""
60-
self.\(name.camelCase.normalize) = \(name.camelCase.normalize)
60+
self.\(name.camelCasePreservingSurroundingUnderscores.normalize) = \(name.camelCasePreservingSurroundingUnderscores.normalize)
6161
"""
6262
}
6363

@@ -138,7 +138,7 @@ private extension Collection where Element == InputValue {
138138
private extension InputValue {
139139
/// Returns an encoder for this input value.
140140
var encoder: String {
141-
let key = name.camelCase.normalize
141+
let key = name.camelCasePreservingSurroundingUnderscores.normalize
142142

143143
switch type.inverted {
144144
case .nullable:
@@ -152,6 +152,6 @@ private extension InputValue {
152152

153153
/// Returns a coding key for this input value.
154154
var codingKey: String {
155-
"case \(name.camelCase.normalize) = \"\(name)\""
155+
"case \(name.camelCasePreservingSurroundingUnderscores.normalize) = \"\(name)\""
156156
}
157157
}

Sources/SwiftGraphQLUtils/Extensions/String+Case.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,21 @@ extension String {
7373
return result
7474
}
7575

76-
/// Returns the string camelCased.
77-
public var camelCase: String {
76+
/// Returns the string – camelCased – while retaining all leading and trailing underscores
77+
///
78+
/// _foo_ // returns _foo_
79+
/// ___foo_bar___ // returns ___fooBar___
80+
///
81+
public var camelCasePreservingSurroundingUnderscores: String {
82+
let leading = prefix { $0 == "_" }
83+
let remainder = dropFirst(leading.count)
84+
let trimmed = remainder.trimmingCharacters(in: CharacterSet(["_"]))
85+
let trailing = String.SubSequence(repeating: "_", count: remainder.count - trimmed.count)
86+
7887
let pascal = pascalCase
79-
return pascal[pascal.startIndex].lowercased() + pascal.dropFirst()
88+
return String(leading)
89+
+ pascal[pascal.startIndex].lowercased()
90+
+ pascal.dropFirst()
91+
+ String(trailing)
8092
}
8193
}

Tests/SwiftGraphQLCodegenTests/Generator/EnumTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class EnumTests: XCTestCase {
3030
deprecationReason: "Was too good."
3131
),
3232
EnumValue(
33-
name: "SKYWALKER",
33+
name: "_SKYWALKER__",
3434
description: nil,
3535
isDeprecated: true,
3636
deprecationReason: nil
@@ -53,7 +53,7 @@ final class EnumTests: XCTestCase {
5353
/// Released in 1983.
5454
case jedi = "JEDI"
5555
56-
case skywalker = "SKYWALKER"
56+
case _skywalker__ = "_SKYWALKER__"
5757
}
5858
}
5959

Tests/SwiftGraphQLUtilsTests/Extensions/String+CaseTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import XCTest
33

44
final class StringExtensionsTest: XCTestCase {
55
func testCamelCase() {
6-
XCTAssertEqual("___a very peculiarNameIndeed__wouldNot.you.agree.AMAZING?____".camelCase, "aVeryPeculiarNameIndeedWouldNotYouAgreeAmazing")
7-
XCTAssertEqual("ENUM".camelCase, "enum")
8-
XCTAssertEqual("linkToURL".camelCase, "linkToUrl")
9-
XCTAssertEqual("grandfather_father.son grandson".camelCase, "grandfatherFatherSonGrandson")
10-
XCTAssertEqual("queryDBShortcuts".camelCase, "queryDbShortcuts")
6+
XCTAssertEqual("___a very peculiarNameIndeed__wouldNot.you.agree.AMAZING?____".camelCasePreservingSurroundingUnderscores, "___aVeryPeculiarNameIndeedWouldNotYouAgreeAmazing____")
7+
XCTAssertEqual("ENUM".camelCasePreservingSurroundingUnderscores, "enum")
8+
XCTAssertEqual("linkToURL".camelCasePreservingSurroundingUnderscores, "linkToUrl")
9+
XCTAssertEqual("grandfather_father.son grandson".camelCasePreservingSurroundingUnderscores, "grandfatherFatherSonGrandson")
10+
XCTAssertEqual("queryDBShortcuts".camelCasePreservingSurroundingUnderscores, "queryDbShortcuts")
1111
}
1212

1313
func testPascalCase() {

0 commit comments

Comments
 (0)