Skip to content

Commit 53577f9

Browse files
authored
Merge branch 'master' into upgrade-nio-to-2.10
2 parents c5b58f5 + 02e6a51 commit 53577f9

File tree

6 files changed

+80
-32
lines changed

6 files changed

+80
-32
lines changed

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.0
1+
5.1

Sources/GraphQL/Error/GraphQLError.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* it also includes information about the locations in a
55
* GraphQL document and/or execution result that correspond to the error.
66
*/
7-
public struct GraphQLError : Error, Encodable {
8-
private enum CodingKeys : String, CodingKey {
7+
public struct GraphQLError : Error, Codable {
8+
enum CodingKeys : String, CodingKey {
99
case message
1010
case locations
1111
case path
@@ -41,23 +41,23 @@ public struct GraphQLError : Error, Encodable {
4141
/**
4242
* An array of GraphQL AST Nodes corresponding to this error.
4343
*/
44-
public let nodes: [Node]
44+
public private(set) var nodes: [Node] = []
4545

4646
/**
4747
* The source GraphQL document corresponding to this error.
4848
*/
49-
public let source: Source?
49+
public private(set) var source: Source? = nil
5050

5151
/**
5252
* An array of character offsets within the source GraphQL document
5353
* which correspond to this error.
5454
*/
55-
public let positions: [Int]
55+
public private(set) var positions: [Int] = []
5656

5757
/**
5858
* The original error thrown from a field resolver during execution.
5959
*/
60-
public let originalError: Error?
60+
public private(set) var originalError: Error? = nil
6161

6262
public init(
6363
message: String,
@@ -134,7 +134,7 @@ extension GraphQLError : Hashable {
134134

135135
// MARK: IndexPath
136136

137-
public struct IndexPath : Encodable {
137+
public struct IndexPath : Codable {
138138
public let elements: [IndexPathValue]
139139

140140
public init(_ elements: [IndexPathElement] = []) {
@@ -152,10 +152,22 @@ extension IndexPath : ExpressibleByArrayLiteral {
152152
}
153153
}
154154

155-
public enum IndexPathValue : Encodable {
155+
public enum IndexPathValue : Codable {
156156
case index(Int)
157157
case key(String)
158158

159+
public init(from decoder: Decoder) throws {
160+
let container = try decoder.singleValueContainer()
161+
162+
if let index = try? container.decode(Int.self) {
163+
self = .index(index)
164+
} else if let key = try? container.decode(String.self) {
165+
self = .key(key)
166+
} else {
167+
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid type.")
168+
}
169+
}
170+
159171
public func encode(to encoder: Encoder) throws {
160172
var container = encoder.singleValueContainer()
161173

Sources/GraphQL/GraphQL.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import NIO
33

4-
public struct GraphQLResult : Equatable, Encodable, CustomStringConvertible {
4+
public struct GraphQLResult : Equatable, Codable, CustomStringConvertible {
55
public var data: Map?
66
public var errors: [GraphQLError]
77

@@ -14,6 +14,12 @@ public struct GraphQLResult : Equatable, Encodable, CustomStringConvertible {
1414
case data
1515
case errors
1616
}
17+
18+
public init(from decoder: Decoder) throws {
19+
let container = try decoder.container(keyedBy: CodingKeys.self)
20+
self.data = try container.decodeIfPresent(Map.self, forKey: .data)
21+
self.errors = try container.decodeIfPresent([GraphQLError].self, forKey: .errors) ?? []
22+
}
1723

1824
public func encode(to encoder: Encoder) throws {
1925
var container = encoder.container(keyedBy: CodingKeys.self)

Sources/GraphQL/Language/Location.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
public struct SourceLocation : Encodable {
3+
public struct SourceLocation : Codable {
44
public let line: Int
55
public let column: Int
66

Sources/GraphQL/Map/Map.swift

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ public func == (lhs: Map, rhs: Map) -> Bool {
634634
switch (lhs, rhs) {
635635
case (.null, .null):
636636
return true
637+
case let (.bool(l), .bool(r)) where l == r:
638+
return true
637639
case let (.number(l), .number(r)) where l == r:
638640
return true
639641
case let (.string(l), .string(r)) where l == r:
@@ -747,7 +749,35 @@ extension Map:CustomDebugStringConvertible {
747749
extension Map {
748750
public func description(debug: Bool) -> String {
749751
var indentLevel = 0
750-
752+
753+
let escapeMapping: [Character: String] = [
754+
"\r": "\\r",
755+
"\n": "\\n",
756+
"\t": "\\t",
757+
"\\": "\\\\",
758+
"\"": "\\\"",
759+
760+
"\u{2028}": "\\u2028",
761+
"\u{2029}": "\\u2029",
762+
763+
"\r\n": "\\r\\n"
764+
]
765+
766+
func escape(_ source: String) -> String {
767+
var string = "\""
768+
769+
for character in source {
770+
if let escapedSymbol = escapeMapping[character] {
771+
string.append(escapedSymbol)
772+
} else {
773+
string.append(character)
774+
}
775+
}
776+
777+
string.append("\"")
778+
return string
779+
}
780+
751781
func serialize(map: Map) -> String {
752782
switch map {
753783
case .null:
@@ -756,30 +786,30 @@ extension Map {
756786
return value.description
757787
case let .number(number):
758788
return number.description
759-
case .string(let string):
760-
return string
761-
case .array(let array):
789+
case let .string(string):
790+
return escape(string)
791+
case let .array(array):
762792
return serialize(array: array)
763-
case .dictionary(let dictionary):
793+
case let .dictionary(dictionary):
764794
return serialize(dictionary: dictionary)
765795
}
766796
}
767-
797+
768798
func serialize(array: [Map]) -> String {
769799
var string = "["
770-
800+
771801
if debug {
772802
indentLevel += 1
773803
}
774-
804+
775805
for index in 0 ..< array.count {
776806
if debug {
777807
string += "\n"
778808
string += indent()
779809
}
780-
810+
781811
string += serialize(map: array[index])
782-
812+
783813
if index != array.count - 1 {
784814
if debug {
785815
string += ", "
@@ -788,43 +818,43 @@ extension Map {
788818
}
789819
}
790820
}
791-
821+
792822
if debug {
793823
indentLevel -= 1
794824
return string + "\n" + indent() + "]"
795825
} else {
796826
return string + "]"
797827
}
798828
}
799-
829+
800830
func serialize(dictionary: [String: Map]) -> String {
801831
var string = "{"
802832
var index = 0
803-
833+
804834
if debug {
805835
indentLevel += 1
806836
}
807-
837+
808838
for (key, value) in dictionary.sorted(by: {$0.0 < $1.0}) {
809839
if debug {
810840
string += "\n"
811841
string += indent()
812-
string += key + ": " + serialize(map: value)
842+
string += escape(key) + ": " + serialize(map: value)
813843
} else {
814-
string += key + ":" + serialize(map: value)
844+
string += escape(key) + ":" + serialize(map: value)
815845
}
816-
846+
817847
if index != dictionary.count - 1 {
818848
if debug {
819849
string += ", "
820850
} else {
821851
string += ","
822852
}
823853
}
824-
854+
825855
index += 1
826856
}
827-
857+
828858
if debug {
829859
indentLevel -= 1
830860
return string + "\n" + indent() + "}"

Sources/GraphQL/Utilities/NIO+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension Collection {
1717
}
1818

1919
extension Collection {
20-
public func flatMap<S, T>(
20+
internal func flatMap<S, T>(
2121
to type: T.Type,
2222
on eventLoopGroup: EventLoopGroup,
2323
_ callback: @escaping ([S]) throws -> Future<T>
@@ -55,7 +55,7 @@ extension Dictionary where Value : FutureType {
5555
}
5656
}
5757
extension Future {
58-
public func flatMap<T>(
58+
internal func flatMap<T>(
5959
to type: T.Type = T.self,
6060
_ callback: @escaping (Expectation) throws -> Future<T>
6161
) -> Future<T> {

0 commit comments

Comments
 (0)