Skip to content

Commit 3b1247e

Browse files
committed
Make some types Codable and change visibility.
1 parent cba343a commit 3b1247e

File tree

6 files changed

+94
-30
lines changed

6 files changed

+94
-30
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

Package.resolved

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -738,37 +738,65 @@ extension Map:CustomDebugStringConvertible {
738738
extension Map {
739739
public func description(debug: Bool) -> String {
740740
var indentLevel = 0
741-
741+
742+
let escapeMapping: [Character: String] = [
743+
"\r": "\\r",
744+
"\n": "\\n",
745+
"\t": "\\t",
746+
"\\": "\\\\",
747+
"\"": "\\\"",
748+
749+
"\u{2028}": "\\u2028",
750+
"\u{2029}": "\\u2029",
751+
752+
"\r\n": "\\r\\n"
753+
]
754+
755+
func escape(_ source: String) -> String {
756+
var string = "\""
757+
758+
for character in source {
759+
if let escapedSymbol = escapeMapping[character] {
760+
string.append(escapedSymbol)
761+
} else {
762+
string.append(character)
763+
}
764+
}
765+
766+
string.append("\"")
767+
return string
768+
}
769+
742770
func serialize(map: Map) -> String {
743771
switch map {
744772
case .null:
745773
return "null"
746774
case let .number(number):
747775
return number.description
748-
case .string(let string):
749-
return string
750-
case .array(let array):
776+
case let .string(string):
777+
return escape(string)
778+
case let .array(array):
751779
return serialize(array: array)
752-
case .dictionary(let dictionary):
780+
case let .dictionary(dictionary):
753781
return serialize(dictionary: dictionary)
754782
}
755783
}
756-
784+
757785
func serialize(array: [Map]) -> String {
758786
var string = "["
759-
787+
760788
if debug {
761789
indentLevel += 1
762790
}
763-
791+
764792
for index in 0 ..< array.count {
765793
if debug {
766794
string += "\n"
767795
string += indent()
768796
}
769-
797+
770798
string += serialize(map: array[index])
771-
799+
772800
if index != array.count - 1 {
773801
if debug {
774802
string += ", "
@@ -777,43 +805,43 @@ extension Map {
777805
}
778806
}
779807
}
780-
808+
781809
if debug {
782810
indentLevel -= 1
783811
return string + "\n" + indent() + "]"
784812
} else {
785813
return string + "]"
786814
}
787815
}
788-
816+
789817
func serialize(dictionary: [String: Map]) -> String {
790818
var string = "{"
791819
var index = 0
792-
820+
793821
if debug {
794822
indentLevel += 1
795823
}
796-
824+
797825
for (key, value) in dictionary.sorted(by: {$0.0 < $1.0}) {
798826
if debug {
799827
string += "\n"
800828
string += indent()
801-
string += key + ": " + serialize(map: value)
829+
string += escape(key) + ": " + serialize(map: value)
802830
} else {
803-
string += key + ":" + serialize(map: value)
831+
string += escape(key) + ":" + serialize(map: value)
804832
}
805-
833+
806834
if index != dictionary.count - 1 {
807835
if debug {
808836
string += ", "
809837
} else {
810838
string += ","
811839
}
812840
}
813-
841+
814842
index += 1
815843
}
816-
844+
817845
if debug {
818846
indentLevel -= 1
819847
return string + "\n" + indent() + "}"

0 commit comments

Comments
 (0)